1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| 'use strict'; const pushmodel = require('./pushmodel'); const request = require('request'); const COS = require('cos-nodejs-sdk-v5'); const tencentcloud = require("tencentcloud-sdk-nodejs"); const cos = new COS({ SecretId: 'SecretId', SecretKey: 'SecretKey' }); const OAuth = require('oauth'); const oauth = new OAuth.OAuth( 'https://api.twitter.com/oauth/request_token', 'https://api.twitter.com/oauth/access_token', 'SecretId', 'SecretKey', '1.0A', null, 'HMAC-SHA1' );
exports.main_handler = async (event, context, callback) => { try { let [PQ,config,cosjson] = await Promise.all([ new pushmodel.PushQQ('http://*******:****/', 'J*************5'), require('./config'), JSON.parse((await getFileData('TwitterToQQ/cos.json')).Body.toString()), ]); let cosjsonSaveFlag = false; await Promise.all(config.TwitterId.map(async item=>{ const twitterRes = await getTwitterRes({since_id: cosjson[item.userid] , user_id: item.userid}); if (twitterRes && twitterRes.length){ cosjsonSaveFlag = true; cosjson[item.userid] = twitterRes[0].id_str; await Promise.all(twitterRes.map(async item1=>{ const sendData = await processTweet(item1); if (item.model.indexOf('qq')!== -1) { await Promise.all(item.qq.map(async item2 => { await PQ.push_message(item2.qq_model, item2.qq_num, sendData); })); } })); } })); if (cosjsonSaveFlag) { await saveFileData('TwitterToQQ/cos.json',cosjson); } return ; } catch (e) { console.log(e); } };
async function processTweet(data) { let result = ""; result+="[" + data.user.name + "]\n"; result+="[原推特链接 " + await processShortUrl('https://twitter.com/' + data.user.id_str + '/status/' + data.id_str) + "]\n"; result+="[" + (new Date(Date.parse(data.created_at))).toString() + "]\n\n"; result+=data.text ; if (data.quoted_status) { result+="\n\n[转发自"+data.quoted_status.user.name+"]\n"; result+=data.quoted_status.text; } if (["997************92","294**********17"].indexOf(data.user.id_str)!== -1){ result+= "\n\n【日文推特翻译】\n"; result+=await processTranslate({ "data": data.text, "Source": 'jp', "Target": 'zh', }); } return result; }
async function processTranslate(data) { const result = new Promise(resolve => { const TmtClient = tencentcloud.tmt.v20180321.Client; const models = tencentcloud.tmt.v20180321.Models;
const Credential = tencentcloud.common.Credential; const ClientProfile = tencentcloud.common.ClientProfile; const HttpProfile = tencentcloud.common.HttpProfile;
let cred = new Credential("SecretId", "SecretKey"); let httpProfile = new HttpProfile(); httpProfile.endpoint = "tmt.tencentcloudapi.com"; let clientProfile = new ClientProfile(); clientProfile.httpProfile = httpProfile; let client = new TmtClient(cred, "ap-hongkong", clientProfile);
let req = new models.TextTranslateRequest(); let params = JSON.stringify({ "SourceText": data.data, "Source":data.Source, "Target":data.Target, "ProjectId":0 }); req.from_json_string(params);
client.TextTranslate(req, function(errMsg, response) {
if (errMsg) { console.log(errMsg); return; }
resolve(JSON.parse(response.to_json_string()).TargetText); });
}); return result; }
async function processShortUrl(Longurl) { const result = new Promise(resolve => { const sendData = { "LongUrl": Longurl, "TermOfValidity": "1-year", } request({ url: "http://t.ipoi.bid/v1/create", method: "POST", json: true, headers: { "Token": "s********************9", "content-type": "application/json", }, body: sendData }, function (error, response, body) { if (!error && response.statusCode == 200) { resolve(body.ShortUrl); }; }); }); return result; }
async function getTwitterRes(data) { const result = new Promise(resolve => { const url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?since_id=' + data.since_id + '&user_id=' + data.user_id; oauth.get( url, 'user token', 'user secret', function (e, data, res){ if (e) console.error(e); resolve(JSON.parse(data)); } ); }) return result; }
async function getFileData(remotePath) { let result = new Promise(res=>{ cos.getObject({ Bucket: 's**********82', Region: 'ap-hongkong', Key: remotePath, }, function(err, data) { res(data); }); }); return result; } async function saveFileData(remotePath, data) { let result = new Promise(res=>{ cos.putObject({ Bucket: 's***********82', Region: 'ap-hongkong', Key: remotePath, Body: JSON.stringify(data), }, function (err, data) { res(data); console.log(err || data); }); }); return result; }
|