twitter.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.sendPost = exports.getPostOwner = exports.WebshotHelpers = exports.ScreenNameNormalizer = exports.SessionManager = exports.urlSegmentToId = exports.idToUrlSegment = exports.isValidUrlSegment = 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_id_to_url_segment_1 = require("instagram-id-to-url-segment");
  20. Object.defineProperty(exports, "idToUrlSegment", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.instagramIdToUrlSegment; } });
  21. Object.defineProperty(exports, "urlSegmentToId", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.urlSegmentToInstagramId; } });
  22. const instagram_private_api_1 = require("instagram-private-api");
  23. const socks_proxy_agent_1 = require("socks-proxy-agent");
  24. const loggers_1 = require("./loggers");
  25. const utils_1 = require("./utils");
  26. const webshot_1 = require("./webshot");
  27. const parseLink = (link) => {
  28. let match = /instagram\.com\/p\/([A-Za-z0-9\-_]+)/.exec(link);
  29. if (match)
  30. return { postUrlSegment: match[1] };
  31. match =
  32. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  33. /^([^\/?#]+)$/.exec(link);
  34. if (match)
  35. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] };
  36. return;
  37. };
  38. exports.parseLink = parseLink;
  39. const isValidUrlSegment = (input) => /^[A-Za-z0-9\-_]+$/.test(input);
  40. exports.isValidUrlSegment = isValidUrlSegment;
  41. const linkBuilder = (config) => {
  42. if (config.userName)
  43. return `https://www.instagram.com/${config.userName}/`;
  44. if (config.postUrlSegment)
  45. return `https://www.instagram.com/p/${config.postUrlSegment}/`;
  46. };
  47. exports.linkBuilder = linkBuilder;
  48. class SessionManager {
  49. constructor(client, file, credentials, codeServicePort) {
  50. this.init = () => {
  51. this.ig.state.generateDevice(this.username);
  52. this.ig.request.end$.subscribe(() => { this.save(); });
  53. const filePath = path.resolve(this.lockfile);
  54. if (fs.existsSync(filePath)) {
  55. try {
  56. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  57. return this.ig.state.deserialize(serialized).then(() => {
  58. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  59. });
  60. }
  61. catch (err) {
  62. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  63. return Promise.resolve();
  64. }
  65. }
  66. else {
  67. return this.login().catch((err) => {
  68. logger.error(`error while trying to log in as user ${this.username}, error: ${err}`);
  69. logger.warn('attempting to retry after 1 minute...');
  70. if (fs.existsSync(filePath))
  71. fs.unlinkSync(filePath);
  72. util_1.promisify(setTimeout)(60000).then(this.init);
  73. });
  74. }
  75. };
  76. this.handle2FA = (submitter) => new Promise((resolve, reject) => {
  77. const token = crypto.randomBytes(20).toString('hex');
  78. logger.info('please submit the code with a one-time token from your browser with this path:');
  79. logger.info(`/confirm-2fa?code=<the code you received>&token=${token}`);
  80. let working;
  81. const server = http.createServer((req, res) => {
  82. const { pathname, query } = url_1.parse(req.url, true);
  83. if (!working && pathname === '/confirm-2fa' && query.token === token &&
  84. typeof (query.code) === 'string' && /^\d{6}$/.test(query.code)) {
  85. const code = query.code;
  86. logger.debug(`received code: ${code}`);
  87. working = true;
  88. submitter(code)
  89. .then(response => { res.write('OK'); res.end(); server.close(() => resolve(response)); })
  90. .catch(err => { res.write('Error'); res.end(); reject(err); })
  91. .finally(() => { working = false; });
  92. }
  93. });
  94. server.listen(this.codeServicePort);
  95. });
  96. this.login = () => this.ig.simulate.preLoginFlow()
  97. .then(() => this.ig.account.login(this.username, this.password))
  98. .catch((err) => {
  99. if (err instanceof instagram_private_api_1.IgLoginTwoFactorRequiredError) {
  100. const { two_factor_identifier, totp_two_factor_on } = err.response.body.two_factor_info;
  101. logger.debug(`2FA info: ${JSON.stringify(err.response.body.two_factor_info)}`);
  102. logger.info(`login is requesting two-factor authentication via ${totp_two_factor_on ? 'TOTP' : 'SMS'}`);
  103. return this.handle2FA(code => this.ig.account.twoFactorLogin({
  104. username: this.username,
  105. verificationCode: code,
  106. twoFactorIdentifier: two_factor_identifier,
  107. verificationMethod: totp_two_factor_on ? '0' : '1',
  108. }));
  109. }
  110. throw err;
  111. })
  112. .then(user => new Promise(resolve => {
  113. logger.info(`successfully logged in as ${this.username}`);
  114. process.nextTick(() => resolve(this.ig.simulate.postLoginFlow().then(() => user)));
  115. }));
  116. this.save = () => this.ig.state.serialize()
  117. .then((serialized) => {
  118. delete serialized.constants;
  119. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  120. });
  121. this.ig = client;
  122. this.lockfile = file;
  123. [this.username, this.password] = credentials;
  124. this.codeServicePort = codeServicePort;
  125. }
  126. }
  127. exports.SessionManager = SessionManager;
  128. class ScreenNameNormalizer {
  129. static normalizeLive(username) {
  130. return __awaiter(this, void 0, void 0, function* () {
  131. if (this._queryUser) {
  132. return yield this._queryUser(username)
  133. .catch((err) => {
  134. if (!(err instanceof instagram_private_api_1.IgExactUserNotFoundError)) {
  135. logger.warn(`error looking up user: ${err.message}`);
  136. return `${username}:`;
  137. }
  138. return null;
  139. });
  140. }
  141. return this.normalize(username);
  142. });
  143. }
  144. }
  145. exports.ScreenNameNormalizer = ScreenNameNormalizer;
  146. ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
  147. let browserLogin = (page) => Promise.reject();
  148. let browserSaveCookies = browserLogin;
  149. let isWaitingForLogin = false;
  150. const acceptCookieConsent = (page) => page.click('button:has-text("すべて許可")', { timeout: 5000 })
  151. .then(() => logger.info('accepted cookie consent'))
  152. .catch((err) => { if (err.name !== 'TimeoutError')
  153. throw err; });
  154. exports.WebshotHelpers = {
  155. handleLogin: browserLogin,
  156. handleCookieConsent: acceptCookieConsent,
  157. get isWaitingForLogin() { return isWaitingForLogin; },
  158. };
  159. let getPostOwner = (segmentId) => Promise.reject();
  160. exports.getPostOwner = getPostOwner;
  161. let sendPost = (segmentId, receiver) => {
  162. throw Error();
  163. };
  164. exports.sendPost = sendPost;
  165. const logger = loggers_1.getLogger('instagram');
  166. const maxTrials = 3;
  167. const retryInterval = 1500;
  168. const ordinal = (n) => {
  169. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  170. case 1:
  171. return `${n}st`;
  172. case 2:
  173. return `${n}nd`;
  174. case 3:
  175. return `${n}rd`;
  176. default:
  177. return `${n}th`;
  178. }
  179. };
  180. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  181. const retry = (reason, count) => {
  182. setTimeout(() => {
  183. let terminate = false;
  184. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  185. if (!terminate)
  186. doWork().then(resolve).catch(error => retry(error, count + 1));
  187. }, retryInterval);
  188. };
  189. doWork().then(resolve).catch(error => retry(error, 1));
  190. });
  191. class default_1 {
  192. constructor(opt) {
  193. this.webshotCookies = [];
  194. this.launch = () => {
  195. this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => this.webshotCookies, doOnNewPage => {
  196. this.queryUserMedia = ((userName, targetId) => {
  197. let page;
  198. const url = linkBuilder({ userName });
  199. logger.debug(`pulling ${targetId !== '0' ? `feed ${url} up to ${targetId}` : `top of feed ${url}`}...`);
  200. return doOnNewPage(newPage => {
  201. page = newPage;
  202. let timeout = this.workInterval * 1000;
  203. const startTime = new Date().getTime();
  204. const getTimerTime = () => new Date().getTime() - startTime;
  205. const getTimeout = () => isWaitingForLogin ? 0 : Math.max(90000, timeout - getTimerTime());
  206. return page.context().addCookies(this.webshotCookies)
  207. .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
  208. .then(response => {
  209. if (response.status() !== 200) {
  210. const err = new Error(`error navigating to user page, error was: ${response.status()} ${response.statusText()}`);
  211. throw Object.defineProperty(err, 'name', {
  212. value: 'ResponseError',
  213. });
  214. }
  215. }).then(() => acceptCookieConsent(page))
  216. .then(() => (next => Promise.race([
  217. browserLogin(page)
  218. .catch((err) => {
  219. if (err.name === 'TimeoutError') {
  220. logger.warn('navigation timed out, assuming login has failed');
  221. isWaitingForLogin = false;
  222. }
  223. throw err;
  224. })
  225. .then(() => browserSaveCookies(page))
  226. .then(() => page.goto(url)).then(next),
  227. next(),
  228. ]))(() => util_1.promisify(setTimeout)(2000).then(() => page.waitForSelector('article', { timeout: getTimeout() })))).then(handle => {
  229. const postHandler = () => {
  230. const toId = (href) => { var _a; return instagram_id_to_url_segment_1.urlSegmentToInstagramId(((_a = /\/p\/(.*)\/$/.exec(href)) !== null && _a !== void 0 ? _a : [, ''])[1]); };
  231. if (targetId === '0') {
  232. return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]'))[0].href).then(href => Number(toId(href)) > 0 ? [toId(href)] : []);
  233. }
  234. return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]')).map(a => a.href)).then(hrefs => {
  235. let id;
  236. const itemIds = [];
  237. for (const href of hrefs) {
  238. id = toId(href);
  239. if (id && utils_1.BigNumOps.compare(id, targetId) > 0)
  240. itemIds.push(id);
  241. else
  242. return itemIds;
  243. }
  244. logger.info('unable to find a smaller id than target, trying on next page...');
  245. return null;
  246. });
  247. };
  248. return postHandler().then(itemIds => {
  249. if (itemIds)
  250. return itemIds;
  251. timeout += this.workInterval * 500;
  252. return handle.$$('a')
  253. .then(as => { as.pop().scrollIntoViewIfNeeded(); return as.length + 1; })
  254. .then(loadedCount => page.waitForFunction(count => document.querySelectorAll('article a').length > count, loadedCount))
  255. .then(postHandler);
  256. });
  257. }).catch((err) => {
  258. if (err.name !== 'TimeoutError' && err.name !== 'ResponseError')
  259. throw err;
  260. if (err.name === 'ResponseError') {
  261. logger.warn(`error while fetching tweets for ${userName}: ${err.message}`);
  262. }
  263. else
  264. logger.warn(`navigation timed out at ${getTimerTime()} ms`);
  265. return [];
  266. }).then(itemIds => util_1.promisify(setTimeout)(getTimeout()).then(() => itemIds.map(id => this.lazyGetMediaById(id))));
  267. }).finally(() => { page.close(); });
  268. });
  269. setTimeout(this.work, this.workInterval * 1000);
  270. });
  271. };
  272. this.queryUser = (username) => this.client.user.searchExact(username)
  273. .then(user => `${user.username}:${user.pk}`);
  274. this.workOnMedia = (lazyMediaItems, sendMedia) => this.webshot(lazyMediaItems, sendMedia, this.webshotDelay);
  275. this.urlSegmentToId = instagram_id_to_url_segment_1.urlSegmentToInstagramId;
  276. this.lazyGetMediaById = (id) => ({
  277. pk: id,
  278. item: () => this.client.media.info(id).then(media => {
  279. const mediaItem = media.items[0];
  280. logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${id}`);
  281. return mediaItem;
  282. }),
  283. });
  284. this.getMedia = (segmentId, sender) => this.workOnMedia([this.lazyGetMediaById(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))], sender);
  285. this.sendMedia = (source, ...to) => (msg, text, author) => {
  286. to.forEach(subscriber => {
  287. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  288. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  289. if (count <= maxTrials) {
  290. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  291. }
  292. else {
  293. logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
  294. terminate(this.bot.sendTo(subscriber, author + text, true));
  295. }
  296. });
  297. });
  298. };
  299. this.work = () => {
  300. const lock = this.lock;
  301. if (this.workInterval < 1)
  302. this.workInterval = 1;
  303. if (lock.feed.length === 0) {
  304. setTimeout(() => {
  305. this.work();
  306. }, this.workInterval * 1000);
  307. return;
  308. }
  309. if (lock.workon >= lock.feed.length)
  310. lock.workon = 0;
  311. if (!lock.threads[lock.feed[lock.workon]] ||
  312. !lock.threads[lock.feed[lock.workon]].subscribers ||
  313. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  314. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  315. delete lock.threads[lock.feed[lock.workon]];
  316. lock.feed.splice(lock.workon, 1);
  317. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  318. this.work();
  319. return;
  320. }
  321. const currentFeed = lock.feed[lock.workon];
  322. const promise = new Promise(resolve => {
  323. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  324. if (!match) {
  325. logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
  326. return resolve([]);
  327. }
  328. this.queryUserMedia(match[1], this.lock.threads[currentFeed].offset)
  329. .then(resolve)
  330. .catch((error) => {
  331. logger.error(`error scraping media off profile page of ${match[1]}, error: ${error}`);
  332. resolve([]);
  333. });
  334. });
  335. promise.then((mediaItems) => {
  336. const currentThread = lock.threads[currentFeed];
  337. const updateDate = () => currentThread.updatedAt = new Date().toString();
  338. if (!mediaItems || mediaItems.length === 0) {
  339. updateDate();
  340. return;
  341. }
  342. const topOfFeed = mediaItems[0].pk;
  343. const updateOffset = () => currentThread.offset = topOfFeed;
  344. if (currentThread.offset === '-1') {
  345. updateOffset();
  346. return;
  347. }
  348. if (currentThread.offset === '0')
  349. mediaItems.splice(1);
  350. return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
  351. .then(updateDate).then(updateOffset);
  352. })
  353. .then(() => {
  354. lock.workon++;
  355. let timeout = this.workInterval * 1000 / lock.feed.length;
  356. if (timeout < 1000)
  357. timeout = 1000;
  358. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  359. setTimeout(() => {
  360. this.work();
  361. }, timeout);
  362. });
  363. };
  364. this.client = new instagram_private_api_1.IgApiClient();
  365. if (opt.proxyUrl) {
  366. try {
  367. const url = new URL(opt.proxyUrl);
  368. if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol))
  369. throw Error();
  370. if (!url.port)
  371. url.port = '1080';
  372. this.client.request.defaults.agent = new socks_proxy_agent_1.SocksProxyAgent({
  373. hostname: url.hostname,
  374. port: url.port,
  375. userId: url.username,
  376. password: url.password,
  377. });
  378. }
  379. catch (e) {
  380. logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
  381. }
  382. }
  383. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
  384. this.lockfile = opt.lockfile;
  385. this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
  386. this.lock = opt.lock;
  387. this.workInterval = opt.workInterval;
  388. this.bot = opt.bot;
  389. this.webshotDelay = opt.webshotDelay;
  390. this.mode = opt.mode;
  391. this.wsUrl = opt.wsUrl;
  392. const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
  393. try {
  394. this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
  395. logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
  396. }
  397. catch (err) {
  398. logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message);
  399. logger.warn('cookies will be saved to this file when needed');
  400. }
  401. browserLogin = page => page.fill('input[name="username"]', opt.credentials[0], { timeout: 0 })
  402. .then(() => { isWaitingForLogin = true; logger.warn('blocked by login dialog, trying to log in manually...'); })
  403. .then(() => page.fill('input[name="password"]', opt.credentials[1], { timeout: 0 }))
  404. .then(() => page.click('button[type="submit"]', { timeout: 0 }))
  405. .then(() => (next => Promise.race([
  406. page.waitForSelector('#verificationCodeDescription', { timeout: 0 }).then(handle => handle.innerText()).then(text => {
  407. logger.info(`login is requesting two-factor authentication via ${/認証アプリ/.test(text) ? 'TOTP' : 'SMS'}`);
  408. return this.session.handle2FA(code => page.fill('input[name="verificationCode"]', code, { timeout: 0 }))
  409. .then(() => page.click('button:has-text("実行")', { timeout: 0 }))
  410. .then(next);
  411. }),
  412. page.waitForResponse(res => res.status() === 429, { timeout: 0 })
  413. .then(() => { logger.error('fatal error: login restricted: code 429, exiting'); process.exit(1); }),
  414. next(),
  415. ]))(() => page.click('button:has-text("情報を保存")', { timeout: 0 }).then(() => { isWaitingForLogin = false; })));
  416. browserSaveCookies = page => page.context().cookies()
  417. .then(cookies => {
  418. this.webshotCookies = cookies;
  419. logger.info('successfully logged in, saving cookies to file...');
  420. fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8');
  421. });
  422. exports.WebshotHelpers.handleLogin = page => browserLogin(page)
  423. .then(() => page.waitForSelector('img[data-testid="user-avatar"]', { timeout: this.webshotDelay }))
  424. .then(() => browserSaveCookies(page))
  425. .catch((err) => {
  426. if (err.name === 'TimeoutError') {
  427. logger.warn('navigation timed out, assuming login has failed');
  428. isWaitingForLogin = false;
  429. }
  430. throw err;
  431. });
  432. ScreenNameNormalizer._queryUser = this.queryUser;
  433. const parseMediaError = (err) => {
  434. if (!(err instanceof instagram_private_api_1.IgResponseError && err.text === 'Media not found or unavailable')) {
  435. logger.warn(`error retrieving instagram media: ${err.message}`);
  436. return `获取媒体时出现错误:${err.message}`;
  437. }
  438. return '找不到请求的媒体,它可能已被删除。';
  439. };
  440. exports.getPostOwner = (segmentId) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))
  441. .then(media => media.items[0].user)
  442. .then(user => `${user.username}:${user.pk}`)
  443. .catch((err) => { throw Error(parseMediaError(err)); });
  444. exports.sendPost = (segmentId, receiver) => {
  445. this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver))
  446. .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); });
  447. };
  448. }
  449. }
  450. exports.default = default_1;