twitter.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.resolve();
  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, () => {
  196. setTimeout(this.work, this.workInterval * 1000 / this.lock.feed.length);
  197. });
  198. };
  199. this.queryUser = (username) => this.client.user.searchExact(username)
  200. .then(user => `${user.username}:${user.pk}`);
  201. this.workOnMedia = (lazyMediaItems, sendMedia) => this.webshot(lazyMediaItems, sendMedia, this.webshotDelay);
  202. this.urlSegmentToId = instagram_id_to_url_segment_1.urlSegmentToInstagramId;
  203. this.lazyGetMediaById = (id) => ({
  204. pk: id,
  205. item: () => this.client.media.info(id).then(media => {
  206. const mediaItem = media.items[0];
  207. logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${id}`);
  208. return mediaItem;
  209. }),
  210. });
  211. this.getMedia = (segmentId, sender) => this.workOnMedia([this.lazyGetMediaById(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))], sender);
  212. this.sendMedia = (source, ...to) => (msg, text, author) => {
  213. to.forEach(subscriber => {
  214. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  215. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  216. if (count <= maxTrials) {
  217. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  218. }
  219. else {
  220. logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
  221. terminate(this.bot.sendTo(subscriber, author + text, true));
  222. }
  223. });
  224. });
  225. };
  226. this.work = () => {
  227. const lock = this.lock;
  228. if (this.workInterval < 1)
  229. this.workInterval = 1;
  230. if (this.isInactiveTime || lock.feed.length === 0) {
  231. setTimeout(this.work, this.workInterval * 1000);
  232. return;
  233. }
  234. lock.feed.forEach((feed, index) => {
  235. if (!lock.threads[feed] ||
  236. !lock.threads[feed].subscribers ||
  237. lock.threads[feed].subscribers.length === 0) {
  238. logger.warn(`nobody subscribes thread ${feed}, removing from feed`);
  239. delete lock.threads[index];
  240. lock.feed.splice(index, 1);
  241. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  242. }
  243. });
  244. utils_1.chainPromises(utils_1.Arr.chunk(lock.feed.slice(0).reverse(), 5).map((arr, i) => () => Promise.all(arr.map((currentFeed, j) => {
  245. lock.workon = (lock.feed.length - 1) - i * 5 + j;
  246. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  247. const promiseDelay = this.workInterval * (Math.random() + j) * 500 / lock.feed.length;
  248. logger.debug(`timeout for this pull job: ${promiseDelay * 2}`);
  249. const promise = util_1.promisify(setTimeout)(promiseDelay).then(() => {
  250. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  251. if (!match) {
  252. logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
  253. return [];
  254. }
  255. const feed = this.client.feed.user(lock.threads[currentFeed].id);
  256. const newer = (item) => utils_1.BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  257. const fetchMore = () => new Promise(fetch => {
  258. feed.request().then(response => {
  259. if (response.items.length === 0)
  260. return fetch([]);
  261. if (response.items.every(newer)) {
  262. fetchMore().then(fetched => fetch(response.items.concat(fetched)));
  263. }
  264. else
  265. fetch(response.items.filter(newer));
  266. }, (error) => {
  267. if (error instanceof instagram_private_api_1.IgNetworkError) {
  268. logger.warn(`error on fetching media for ${currentFeed}: ${JSON.stringify(error.cause)}`);
  269. if (!(error instanceof instagram_private_api_1.IgNotFoundError))
  270. return;
  271. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  272. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  273. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  274. });
  275. }
  276. else {
  277. logger.error(`unhandled error on fetching media for ${currentFeed}: ${JSON.stringify(error)}`);
  278. }
  279. fetch([]);
  280. });
  281. });
  282. return fetchMore().then(items => items.map(item => ({ pk: item.pk, item: () => Promise.resolve(item) })));
  283. });
  284. promise.then((mediaItems) => {
  285. const currentThread = lock.threads[currentFeed];
  286. const updateDate = () => currentThread.updatedAt = new Date().toString();
  287. if (!mediaItems || mediaItems.length === 0) {
  288. updateDate();
  289. return;
  290. }
  291. const topOfFeed = mediaItems[0].pk;
  292. const updateOffset = () => currentThread.offset = topOfFeed;
  293. if (currentThread.offset === '-1') {
  294. updateOffset();
  295. return;
  296. }
  297. if (currentThread.offset === '0')
  298. mediaItems.splice(1);
  299. return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
  300. .then(updateDate).then(updateOffset);
  301. }).then(() => fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock)));
  302. return util_1.promisify(setTimeout)(promiseDelay * 3);
  303. }))))
  304. .then(() => {
  305. let timeout = this.workInterval * 500;
  306. if (timeout < 1000)
  307. timeout = 1000;
  308. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  309. setTimeout(() => {
  310. this.work();
  311. }, timeout);
  312. });
  313. };
  314. this.client = new instagram_private_api_1.IgApiClient();
  315. if (opt.proxyUrl) {
  316. try {
  317. const url = new URL(opt.proxyUrl);
  318. if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol))
  319. throw Error();
  320. if (!url.port)
  321. url.port = '1080';
  322. this.client.request.defaults.agent = new socks_proxy_agent_1.SocksProxyAgent({
  323. hostname: url.hostname,
  324. port: url.port,
  325. userId: url.username,
  326. password: url.password,
  327. });
  328. }
  329. catch (e) {
  330. logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
  331. }
  332. }
  333. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
  334. this.lockfile = opt.lockfile;
  335. this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
  336. this.lock = opt.lock;
  337. this.inactiveHours = opt.inactiveHours;
  338. this.workInterval = opt.workInterval;
  339. this.bot = opt.bot;
  340. this.webshotDelay = opt.webshotDelay;
  341. this.mode = opt.mode;
  342. this.wsUrl = opt.wsUrl;
  343. const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
  344. try {
  345. this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
  346. logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
  347. }
  348. catch (err) {
  349. logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message);
  350. logger.warn('cookies will be saved to this file when needed');
  351. }
  352. browserLogin = page => page.fill('input[name="username"]', opt.credentials[0], { timeout: 0 })
  353. .then(() => { isWaitingForLogin = true; logger.warn('blocked by login dialog, trying to log in manually...'); })
  354. .then(() => page.fill('input[name="password"]', opt.credentials[1], { timeout: 0 }))
  355. .then(() => page.click('button[type="submit"]', { timeout: 0 }))
  356. .then(() => (next => Promise.race([
  357. page.waitForSelector('#verificationCodeDescription', { timeout: 0 }).then(handle => handle.innerText()).then(text => {
  358. logger.info(`login is requesting two-factor authentication via ${/認証アプリ/.test(text) ? 'TOTP' : 'SMS'}`);
  359. return this.session.handle2FA(code => page.fill('input[name="verificationCode"]', code, { timeout: 0 }))
  360. .then(() => page.click('button:has-text("実行")', { timeout: 0 }))
  361. .then(next);
  362. }),
  363. page.waitForResponse(res => res.status() === 429, { timeout: 0 })
  364. .then(() => { logger.error('fatal error: login restricted: code 429, exiting'); process.exit(1); }),
  365. next(),
  366. ]))(() => page.click('button:has-text("情報を保存")', { timeout: 0 }).then(() => { isWaitingForLogin = false; })));
  367. browserSaveCookies = page => page.context().cookies()
  368. .then(cookies => {
  369. this.webshotCookies = cookies;
  370. logger.info('successfully logged in, saving cookies to file...');
  371. fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8');
  372. });
  373. exports.WebshotHelpers.handleLogin = page => browserLogin(page)
  374. .then(() => page.waitForSelector('img[data-testid="user-avatar"]', { timeout: this.webshotDelay }))
  375. .then(() => browserSaveCookies(page))
  376. .catch((err) => {
  377. if (err.name === 'TimeoutError') {
  378. logger.warn('navigation timed out, assuming login has failed');
  379. isWaitingForLogin = false;
  380. }
  381. throw err;
  382. });
  383. ScreenNameNormalizer._queryUser = this.queryUser;
  384. const parseMediaError = (err) => {
  385. if (!(err instanceof instagram_private_api_1.IgResponseError && err.text === 'Media not found or unavailable')) {
  386. logger.warn(`error retrieving instagram media: ${err.message}`);
  387. return `获取媒体时出现错误:${err.message}`;
  388. }
  389. return '找不到请求的媒体,它可能已被删除。';
  390. };
  391. exports.getPostOwner = (segmentId) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))
  392. .then(media => media.items[0].user)
  393. .then(user => `${user.username}:${user.pk}`)
  394. .catch((err) => { throw Error(parseMediaError(err)); });
  395. exports.sendPost = (segmentId, receiver) => {
  396. this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver))
  397. .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); });
  398. };
  399. }
  400. get isInactiveTime() {
  401. const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
  402. return this.inactiveHours
  403. .map(rangeStr => ((start, end) => ({ start, end }))(...rangeStr.split('-', 2).map(timeStr => timeToEpoch(...timeStr.split(':', 2).map(Number)))))
  404. .some(range => (now => now >= range.start && now < range.end)(Date.now()));
  405. }
  406. }
  407. exports.default = default_1;