twitter.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.sendAllStories = exports.ScreenNameNormalizer = exports.SessionManager = exports.parseLink = exports.linkBuilder = void 0;
  13. const crypto = require("crypto");
  14. const fs = require("fs");
  15. const http = require("http");
  16. const path = require("path");
  17. const url_1 = require("url");
  18. const util_1 = require("util");
  19. const instagram_private_api_1 = require("instagram-private-api");
  20. const socks_proxy_agent_1 = require("socks-proxy-agent");
  21. const loggers_1 = require("./loggers");
  22. const koishi_1 = require("./koishi");
  23. const utils_1 = require("./utils");
  24. const webshot_1 = require("./webshot");
  25. const parseLink = (link) => {
  26. let match = /instagram\.com\/stories\/([^\/?#]+)\/(\d+)/.exec(link);
  27. if (match)
  28. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0], storyId: match[2] };
  29. match =
  30. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  31. /^([^\/?#]+)$/.exec(link);
  32. if (match)
  33. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] };
  34. return;
  35. };
  36. exports.parseLink = parseLink;
  37. const linkBuilder = (config) => {
  38. if (!config.userName)
  39. return;
  40. if (!config.storyId)
  41. return `https://www.instagram.com/${config.userName}/`;
  42. return `https://www.instagram.com/stories/${config.userName}/${config.storyId}/`;
  43. };
  44. exports.linkBuilder = linkBuilder;
  45. class SessionManager {
  46. constructor(client, file, credentials, codeServicePort) {
  47. this.init = () => {
  48. this.ig.state.generateDevice(this.username);
  49. this.ig.request.end$.subscribe(() => { this.save(); });
  50. const filePath = path.resolve(this.lockfile);
  51. if (fs.existsSync(filePath)) {
  52. try {
  53. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  54. return this.ig.state.deserialize(serialized).then(() => {
  55. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  56. });
  57. }
  58. catch (err) {
  59. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  60. return Promise.resolve();
  61. }
  62. }
  63. else {
  64. return this.login().catch((err) => {
  65. logger.error(`error while trying to log in as user ${this.username}, error: ${err}`);
  66. logger.warn('attempting to retry after 1 minute...');
  67. if (fs.existsSync(filePath))
  68. fs.unlinkSync(filePath);
  69. util_1.promisify(setTimeout)(60000).then(this.init);
  70. });
  71. }
  72. };
  73. this.handle2FA = (submitter) => new Promise((resolve, reject) => {
  74. const token = crypto.randomBytes(20).toString('hex');
  75. logger.info('please submit the code with a one-time token from your browser with this path:');
  76. logger.info(`/confirm-2fa?code=<the code you received>&token=${token}`);
  77. let working;
  78. const server = http.createServer((req, res) => {
  79. const { pathname, query } = url_1.parse(req.url, true);
  80. if (!working && pathname === '/confirm-2fa' && query.token === token &&
  81. typeof (query.code) === 'string' && /^\d{6}$/.test(query.code)) {
  82. const code = query.code;
  83. logger.debug(`received code: ${code}`);
  84. working = true;
  85. submitter(code)
  86. .then(response => { res.write('OK'); res.end(); server.close(() => resolve(response)); })
  87. .catch(err => { res.write('Error'); res.end(); reject(err); })
  88. .finally(() => { working = false; });
  89. }
  90. });
  91. server.listen(this.codeServicePort);
  92. });
  93. this.login = () => this.ig.simulate.preLoginFlow()
  94. .then(() => this.ig.account.login(this.username, this.password))
  95. .catch((err) => {
  96. if (err instanceof instagram_private_api_1.IgLoginTwoFactorRequiredError) {
  97. const { two_factor_identifier, totp_two_factor_on } = err.response.body.two_factor_info;
  98. logger.debug(`2FA info: ${JSON.stringify(err.response.body.two_factor_info)}`);
  99. logger.info(`login is requesting two-factor authentication via ${totp_two_factor_on ? 'TOTP' : 'SMS'}`);
  100. return this.handle2FA(code => this.ig.account.twoFactorLogin({
  101. username: this.username,
  102. verificationCode: code,
  103. twoFactorIdentifier: two_factor_identifier,
  104. verificationMethod: totp_two_factor_on ? '0' : '1',
  105. }));
  106. }
  107. throw err;
  108. })
  109. .then(user => new Promise(resolve => {
  110. logger.info(`successfully logged in as ${this.username}`);
  111. process.nextTick(() => resolve(this.ig.simulate.postLoginFlow().then(() => user)));
  112. }));
  113. this.save = () => this.ig.state.serialize()
  114. .then((serialized) => {
  115. delete serialized.constants;
  116. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  117. });
  118. this.ig = client;
  119. this.lockfile = file;
  120. [this.username, this.password] = credentials;
  121. this.codeServicePort = codeServicePort;
  122. }
  123. }
  124. exports.SessionManager = SessionManager;
  125. class ScreenNameNormalizer {
  126. static normalizeLive(username) {
  127. return __awaiter(this, void 0, void 0, function* () {
  128. if (this._queryUser) {
  129. return yield this._queryUser(username)
  130. .catch((err) => {
  131. if (!(err instanceof instagram_private_api_1.IgExactUserNotFoundError)) {
  132. logger.warn(`error looking up user: ${err.message}`);
  133. return `${username}:`;
  134. }
  135. return null;
  136. });
  137. }
  138. return this.normalize(username);
  139. });
  140. }
  141. }
  142. exports.ScreenNameNormalizer = ScreenNameNormalizer;
  143. ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
  144. let sendAllStories = (segmentId, receiver) => {
  145. throw Error();
  146. };
  147. exports.sendAllStories = sendAllStories;
  148. const logger = loggers_1.getLogger('instagram');
  149. const maxTrials = 3;
  150. const retryInterval = 1500;
  151. const ordinal = (n) => {
  152. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  153. case 1:
  154. return `${n}st`;
  155. case 2:
  156. return `${n}nd`;
  157. case 3:
  158. return `${n}rd`;
  159. default:
  160. return `${n}th`;
  161. }
  162. };
  163. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  164. const retry = (reason, count) => {
  165. setTimeout(() => {
  166. let terminate = false;
  167. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  168. if (!terminate)
  169. doWork().then(resolve).catch(error => retry(error, count + 1));
  170. }, retryInterval);
  171. };
  172. doWork().then(resolve).catch(error => retry(error, 1));
  173. });
  174. class default_1 {
  175. constructor(opt) {
  176. this.launch = () => {
  177. this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => {
  178. setTimeout(this.workForAll, this.workInterval * 1000);
  179. setTimeout(() => {
  180. this.work();
  181. setInterval(this.workForAll, this.workInterval * 10000);
  182. }, this.workInterval * 1200);
  183. });
  184. };
  185. this.queryUser = (rawUserName) => {
  186. const username = ScreenNameNormalizer.normalize(rawUserName).split(':')[0];
  187. if (username in this.cache) {
  188. return Promise.resolve(`${username}:${this.cache[username].user.pk}`);
  189. }
  190. return this.client.user.searchExact(username)
  191. .then(user => {
  192. this.cache[user.username] = { user, stories: {} };
  193. return `${user.username}:${user.pk}`;
  194. });
  195. };
  196. this.workOnMedia = (mediaItems, sendMedia) => this.webshot(mediaItems, sendMedia, this.webshotDelay);
  197. this.sendStories = (source, ...to) => (msg, text, author) => {
  198. to.forEach(subscriber => {
  199. logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
  200. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  201. if (count <= maxTrials) {
  202. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  203. }
  204. else {
  205. logger.warn(`${count - 1} consecutive failures while sending` +
  206. 'message chain, trying plain text instead...');
  207. terminate(this.bot.sendTo(subscriber, author + text));
  208. }
  209. });
  210. });
  211. };
  212. this.cache = {};
  213. this.workForAll = () => {
  214. const idToUserMap = {};
  215. Promise.all(Object.entries(this.lock.threads).map(entry => {
  216. const id = entry[1].id;
  217. const userName = parseLink(entry[0]).userName;
  218. logger.debug(`preparing to add user @${userName} to next pull task...`);
  219. if (userName in this.cache)
  220. return Promise.resolve(idToUserMap[id] = this.cache[userName].user);
  221. return this.client.user.info(id).then(user => {
  222. logger.debug(`initialized cache item for user ${user.full_name} (@${userName})`);
  223. this.cache[userName] = { user, stories: {} };
  224. return idToUserMap[id] = user;
  225. });
  226. }))
  227. .then(() => {
  228. logger.debug(`pulling stories for users: ${Object.values(idToUserMap).map(user => user.username)}`);
  229. this.client.feed.reelsMedia({
  230. userIds: Object.keys(idToUserMap),
  231. }).items()
  232. .then(storyItems => storyItems.forEach(item => {
  233. if (!(item.pk in this.cache[idToUserMap[item.user.pk].username].stories)) {
  234. this.cache[idToUserMap[item.user.pk].username].stories[item.pk] = item;
  235. }
  236. }))
  237. .catch((error) => {
  238. if (error instanceof instagram_private_api_1.IgNetworkError) {
  239. logger.warn(`error on fetching stories for all: ${JSON.stringify(error.cause)}`);
  240. }
  241. else if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
  242. logger.warn('login required, logging in again...');
  243. this.session.login().then(this.workForAll);
  244. }
  245. else {
  246. logger.error(`unhandled error on fetching media for all: ${error}`);
  247. }
  248. });
  249. });
  250. };
  251. this.work = () => {
  252. const lock = this.lock;
  253. logger.debug(`current cache: ${JSON.stringify(this.cache)}`);
  254. if (this.workInterval < 1)
  255. this.workInterval = 1;
  256. if (lock.feed.length === 0) {
  257. setTimeout(this.work, this.workInterval * 1000);
  258. return;
  259. }
  260. if (lock.workon >= lock.feed.length)
  261. lock.workon = 0;
  262. if (!lock.threads[lock.feed[lock.workon]] ||
  263. !lock.threads[lock.feed[lock.workon]].subscribers ||
  264. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  265. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  266. delete lock.threads[lock.feed[lock.workon]];
  267. lock.feed.splice(lock.workon, 1);
  268. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  269. this.work();
  270. return;
  271. }
  272. const currentFeed = lock.feed[lock.workon];
  273. logger.debug(`searching for new items from ${currentFeed} in cache`);
  274. const promise = new Promise(resolve => {
  275. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  276. if (match) {
  277. const cachedFeed = this.cache[match[1]];
  278. if (!cachedFeed) {
  279. setTimeout(this.work, this.workInterval * 1000);
  280. resolve([]);
  281. }
  282. const newer = (item) => utils_1.BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  283. resolve(Object.values(cachedFeed.stories)
  284. .filter(newer)
  285. .map(story => (Object.assign(Object.assign({}, story), { user: cachedFeed.user })))
  286. .sort((i1, i2) => utils_1.BigNumOps.compare(i2.pk, i1.pk)));
  287. }
  288. });
  289. promise.then((mediaItems) => {
  290. const currentThread = lock.threads[currentFeed];
  291. const updateDate = () => currentThread.updatedAt = new Date().toString();
  292. if (!mediaItems || mediaItems.length === 0) {
  293. updateDate();
  294. return;
  295. }
  296. const topOfFeed = mediaItems[0].pk;
  297. const updateOffset = () => currentThread.offset = topOfFeed;
  298. if (currentThread.offset === '-1') {
  299. updateOffset();
  300. return;
  301. }
  302. if (currentThread.offset === '0')
  303. mediaItems.splice(1);
  304. return this.workOnMedia(mediaItems, this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers))
  305. .then(updateDate).then(updateOffset);
  306. })
  307. .then(() => {
  308. lock.workon++;
  309. let timeout = this.workInterval * 1000 / lock.feed.length;
  310. if (timeout < 1000)
  311. timeout = 1000;
  312. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  313. setTimeout(() => {
  314. this.work();
  315. }, timeout);
  316. });
  317. };
  318. this.client = new instagram_private_api_1.IgApiClient();
  319. if (opt.proxyUrl) {
  320. try {
  321. const url = new URL(opt.proxyUrl);
  322. if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol))
  323. throw Error();
  324. if (!url.port)
  325. url.port = '1080';
  326. this.client.request.defaults.agent = new socks_proxy_agent_1.SocksProxyAgent({
  327. hostname: url.hostname,
  328. port: url.port,
  329. userId: url.username,
  330. password: url.password,
  331. });
  332. }
  333. catch (e) {
  334. logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
  335. }
  336. }
  337. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
  338. this.lockfile = opt.lockfile;
  339. this.lock = opt.lock;
  340. this.workInterval = opt.workInterval;
  341. this.bot = opt.bot;
  342. this.webshotDelay = opt.webshotDelay;
  343. this.mode = opt.mode;
  344. this.wsUrl = opt.wsUrl;
  345. ScreenNameNormalizer._queryUser = this.queryUser;
  346. exports.sendAllStories = (rawUserName, receiver) => {
  347. const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
  348. this.queryUser(rawUserName)
  349. .then(userNameId => {
  350. const [userName, userId] = userNameId.split(':');
  351. if (userName in this.cache && Object.keys(this.cache[userName].stories).length > 0) {
  352. return Promise.resolve(Object.values(this.cache[userName].stories)
  353. .map(story => (Object.assign(Object.assign({}, story), { user: this.cache[userName].user })))
  354. .sort((i1, i2) => utils_1.BigNumOps.compare(i2.pk, i1.pk)));
  355. }
  356. return this.client.feed.reelsMedia({ userIds: [userId] }).items()
  357. .then(storyItems => {
  358. storyItems = storyItems.map(story => (Object.assign(Object.assign({}, story), { user: this.cache[userName].user })));
  359. storyItems.forEach(item => {
  360. if (!(item.pk in this.cache[userName].stories)) {
  361. this.cache[userName].stories[item.pk] = item;
  362. }
  363. });
  364. if (storyItems.length === 0)
  365. this.bot.sendTo(receiver, `当前用户 (@${userName}) 没有可用的推特故事。`);
  366. return storyItems;
  367. });
  368. })
  369. .then(storyItems => this.workOnMedia(storyItems, sender))
  370. .catch((error) => {
  371. if (error instanceof instagram_private_api_1.IgNetworkError) {
  372. logger.warn(`error on fetching stories for ${rawUserName}: ${JSON.stringify(error.cause)}`);
  373. this.bot.sendTo(receiver, `获取 Fleets 时出现错误:原因: ${error.cause}`);
  374. }
  375. else if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
  376. logger.warn('login required, logging in again...');
  377. this.session.login().then(() => exports.sendAllStories(rawUserName, receiver));
  378. }
  379. else {
  380. logger.error(`unhandled error on fetching media for ${rawUserName}: ${error}`);
  381. this.bot.sendTo(receiver, `获取 Fleets 时发生未知错误: ${error}`);
  382. }
  383. });
  384. };
  385. }
  386. }
  387. exports.default = default_1;