twitter.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 fs = require("fs");
  14. const path = require("path");
  15. const instagram_id_to_url_segment_1 = require("instagram-id-to-url-segment");
  16. Object.defineProperty(exports, "idToUrlSegment", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.instagramIdToUrlSegment; } });
  17. Object.defineProperty(exports, "urlSegmentToId", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.urlSegmentToInstagramId; } });
  18. const instagram_private_api_1 = require("instagram-private-api");
  19. const loggers_1 = require("./loggers");
  20. const koishi_1 = require("./koishi");
  21. const utils_1 = require("./utils");
  22. const webshot_1 = require("./webshot");
  23. const parseLink = (link) => {
  24. let match = /instagram\.com\/p\/([A-Za-z0-9\-_]+)/.exec(link);
  25. if (match)
  26. return { postUrlSegment: match[1] };
  27. match =
  28. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  29. /^([^\/?#]+)$/.exec(link);
  30. if (match)
  31. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] };
  32. return;
  33. };
  34. exports.parseLink = parseLink;
  35. const isValidUrlSegment = (input) => /^[A-Za-z0-9\-_]+$/.test(input);
  36. exports.isValidUrlSegment = isValidUrlSegment;
  37. const linkBuilder = (config) => {
  38. if (config.userName)
  39. return `https://www.instagram.com/${config.userName}/`;
  40. if (config.postUrlSegment)
  41. return `https://www.instagram.com/p/${config.postUrlSegment}/`;
  42. };
  43. exports.linkBuilder = linkBuilder;
  44. class SessionManager {
  45. constructor(client, file, credentials) {
  46. this.init = () => {
  47. this.ig.state.generateDevice(this.username);
  48. this.ig.request.end$.subscribe(() => { this.save(); });
  49. const filePath = path.resolve(this.lockfile);
  50. if (fs.existsSync(filePath)) {
  51. try {
  52. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  53. return this.ig.state.deserialize(serialized).then(() => {
  54. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  55. });
  56. }
  57. catch (err) {
  58. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  59. return Promise.resolve();
  60. }
  61. }
  62. else
  63. return this.login();
  64. };
  65. this.login = () => this.ig.simulate.preLoginFlow()
  66. .then(() => this.ig.account.login(this.username, this.password))
  67. .then(() => new Promise(resolve => {
  68. logger.info(`successfully logged in as ${this.username}`);
  69. process.nextTick(() => resolve(this.ig.simulate.postLoginFlow()));
  70. }));
  71. this.save = () => this.ig.state.serialize()
  72. .then((serialized) => {
  73. delete serialized.constants;
  74. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  75. });
  76. this.ig = client;
  77. this.lockfile = file;
  78. [this.username, this.password] = credentials;
  79. }
  80. }
  81. exports.SessionManager = SessionManager;
  82. class ScreenNameNormalizer {
  83. static normalizeLive(username) {
  84. return __awaiter(this, void 0, void 0, function* () {
  85. if (this._queryUser) {
  86. return yield this._queryUser(username)
  87. .catch((err) => {
  88. if (!(err instanceof instagram_private_api_1.IgExactUserNotFoundError)) {
  89. logger.warn(`error looking up user: ${err.message}`);
  90. return `${username}:`;
  91. }
  92. return null;
  93. });
  94. }
  95. return this.normalize(username);
  96. });
  97. }
  98. }
  99. exports.ScreenNameNormalizer = ScreenNameNormalizer;
  100. ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
  101. let browserLogin = (page) => Promise.reject();
  102. let browserSaveCookies = browserLogin;
  103. const acceptCookieConsent = (page) => page.click('button:has-text("すべて許可")', { timeout: 5000 })
  104. .then(() => logger.info('accepted cookie consent'))
  105. .catch((err) => { if (err.name !== 'TimeoutError')
  106. throw err; });
  107. exports.WebshotHelpers = {
  108. handleLogin: browserLogin,
  109. handleCookieConsent: acceptCookieConsent,
  110. };
  111. let getPostOwner = (segmentId) => Promise.reject();
  112. exports.getPostOwner = getPostOwner;
  113. let sendPost = (segmentId, receiver) => {
  114. throw Error();
  115. };
  116. exports.sendPost = sendPost;
  117. const logger = loggers_1.getLogger('instagram');
  118. const maxTrials = 3;
  119. const retryInterval = 1500;
  120. const ordinal = (n) => {
  121. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  122. case 1:
  123. return `${n}st`;
  124. case 2:
  125. return `${n}nd`;
  126. case 3:
  127. return `${n}rd`;
  128. default:
  129. return `${n}th`;
  130. }
  131. };
  132. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  133. const retry = (reason, count) => {
  134. setTimeout(() => {
  135. let terminate = false;
  136. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  137. if (!terminate)
  138. doWork().then(resolve).catch(error => retry(error, count + 1));
  139. }, retryInterval);
  140. };
  141. doWork().then(resolve).catch(error => retry(error, 1));
  142. });
  143. class default_1 {
  144. constructor(opt) {
  145. this.webshotCookies = [];
  146. this.launch = () => {
  147. this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => this.webshotCookies, doOnNewPage => {
  148. this.queryUserMedia = ((userName, targetId) => {
  149. let page;
  150. const url = linkBuilder({ userName });
  151. logger.debug(`pulling ${targetId !== '0' ? `feed ${url} up to ${targetId}` : `top of feed ${url}`}...`);
  152. return doOnNewPage(newPage => {
  153. page = newPage;
  154. let timeout = this.webshotDelay;
  155. const startTime = new Date().getTime();
  156. const getTimerTime = () => new Date().getTime() - startTime;
  157. const getTimeout = () => Math.max(500, timeout - getTimerTime());
  158. return page.context().addCookies(this.webshotCookies)
  159. .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
  160. .then(response => {
  161. if (response.status() !== 200) {
  162. const err = new Error(`error navigating to user page, error was: ${response.status()} ${response.statusText()}`);
  163. throw Object.defineProperty(err, 'name', {
  164. value: 'ResponseError',
  165. });
  166. }
  167. }).then(() => acceptCookieConsent(page))
  168. .then(() => (next => Promise.race([
  169. browserLogin(page)
  170. .catch((err) => {
  171. if (err.name === 'TimeoutError')
  172. logger.warn('navigation timed out, assuming login has failed');
  173. throw err;
  174. })
  175. .then(() => browserSaveCookies(page))
  176. .then(() => page.goto(url)).then(next),
  177. next(),
  178. ]))(() => page.waitForSelector('article', { timeout: getTimeout() }))).then(handle => {
  179. const postHandler = () => {
  180. const toId = (href) => { var _a; return instagram_id_to_url_segment_1.urlSegmentToInstagramId(((_a = /\/p\/(.*)\/$/.exec(href)) !== null && _a !== void 0 ? _a : [])[1]); };
  181. if (targetId === '0') {
  182. return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]'))[0].href).then(href => href ? [toId(href)] : null);
  183. }
  184. return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]')).map(a => a.href)).then(hrefs => {
  185. let id;
  186. const itemIds = [];
  187. for (const href of hrefs) {
  188. id = toId(href);
  189. if (id && utils_1.BigNumOps.compare(id, targetId) > 0)
  190. itemIds.push(id);
  191. else
  192. return itemIds;
  193. }
  194. logger.info('unable to find a smaller id than target, trying on next page...');
  195. return null;
  196. });
  197. };
  198. return postHandler().then(itemIds => {
  199. if (itemIds)
  200. return itemIds;
  201. timeout += this.webshotDelay / 2;
  202. return handle.$$('a')
  203. .then(as => { as.pop().scrollIntoViewIfNeeded(); return as.length + 1; })
  204. .then(loadedCount => page.waitForFunction(count => document.querySelectorAll('article a').length > count, loadedCount))
  205. .then(postHandler);
  206. });
  207. }).catch((err) => {
  208. if (err.name !== 'TimeoutError' && err.name !== 'ResponseError')
  209. throw err;
  210. if (err.name === 'ResponseError') {
  211. logger.warn(`error while fetching tweets for ${userName}: ${err.message}`);
  212. }
  213. else
  214. logger.warn(`navigation timed out at ${getTimerTime()} ms`);
  215. return [];
  216. }).then(itemIds => itemIds.map(id => this.lazyGetMediaById(id)));
  217. }).finally(() => { page.close(); });
  218. });
  219. setTimeout(this.work, this.workInterval * 1000);
  220. });
  221. };
  222. this.queryUser = (username) => this.client.user.searchExact(username)
  223. .then(user => `${user.username}:${user.pk}`);
  224. this.workOnMedia = (lazyMediaItems, sendMedia) => this.webshot(lazyMediaItems, sendMedia, this.webshotDelay);
  225. this.urlSegmentToId = instagram_id_to_url_segment_1.urlSegmentToInstagramId;
  226. this.lazyGetMediaById = (id) => ({
  227. pk: id,
  228. item: () => this.client.media.info(id).then(media => {
  229. const mediaItem = media.items[0];
  230. logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${id}`);
  231. return mediaItem;
  232. }),
  233. });
  234. this.getMedia = (segmentId, sender) => this.workOnMedia([this.lazyGetMediaById(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))], sender);
  235. this.sendMedia = (source, ...to) => (msg, text, author) => {
  236. to.forEach(subscriber => {
  237. logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
  238. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  239. if (count <= maxTrials) {
  240. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  241. }
  242. else {
  243. logger.warn(`${count - 1} consecutive failures while sending` +
  244. 'message chain, trying plain text instead...');
  245. terminate(this.bot.sendTo(subscriber, author + text));
  246. }
  247. });
  248. });
  249. };
  250. this.work = () => {
  251. const lock = this.lock;
  252. if (this.workInterval < 1)
  253. this.workInterval = 1;
  254. if (lock.feed.length === 0) {
  255. setTimeout(() => {
  256. this.work();
  257. }, 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. const promise = new Promise(resolve => {
  274. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  275. if (match) {
  276. resolve(this.queryUserMedia(match[1], this.lock.threads[currentFeed].offset));
  277. }
  278. resolve([]);
  279. });
  280. promise.then((mediaItems) => {
  281. const currentThread = lock.threads[currentFeed];
  282. const updateDate = () => currentThread.updatedAt = new Date().toString();
  283. if (!mediaItems || mediaItems.length === 0) {
  284. updateDate();
  285. return;
  286. }
  287. const topOfFeed = mediaItems[0].pk;
  288. const updateOffset = () => currentThread.offset = topOfFeed;
  289. if (currentThread.offset === '-1') {
  290. updateOffset();
  291. return;
  292. }
  293. if (currentThread.offset === '0')
  294. mediaItems.splice(1);
  295. return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
  296. .then(updateDate).then(updateOffset);
  297. })
  298. .then(() => {
  299. lock.workon++;
  300. let timeout = this.workInterval * 1000 / lock.feed.length;
  301. if (timeout < 1000)
  302. timeout = 1000;
  303. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  304. setTimeout(() => {
  305. this.work();
  306. }, timeout);
  307. });
  308. };
  309. this.client = new instagram_private_api_1.IgApiClient();
  310. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials);
  311. this.lockfile = opt.lockfile;
  312. this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
  313. this.lock = opt.lock;
  314. this.workInterval = opt.workInterval;
  315. this.bot = opt.bot;
  316. this.webshotDelay = opt.webshotDelay;
  317. this.mode = opt.mode;
  318. this.wsUrl = opt.wsUrl;
  319. const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
  320. try {
  321. this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
  322. logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
  323. }
  324. catch (err) {
  325. logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message);
  326. logger.warn('cookies will be saved to this file when needed');
  327. }
  328. browserLogin = page => page.fill('input[name="username"]', opt.credentials[0])
  329. .then(() => logger.warn('blocked by login dialog, trying to log in manually...'))
  330. .then(() => page.fill('input[name="password"]', opt.credentials[1]))
  331. .then(() => page.click('button[type="submit"]'))
  332. .then(() => page.click('button:has-text("情報を保存")'));
  333. browserSaveCookies = page => page.context().cookies()
  334. .then(cookies => {
  335. this.webshotCookies = cookies;
  336. logger.info('successfully logged in, saving cookies to file...');
  337. fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8');
  338. });
  339. exports.WebshotHelpers.handleLogin = page => browserLogin(page)
  340. .then(() => page.waitForSelector('img[data-testid="user-avatar"]', { timeout: this.webshotDelay }))
  341. .then(() => browserSaveCookies(page))
  342. .catch((err) => {
  343. if (err.name === 'TimeoutError')
  344. logger.warn('navigation timed out, assuming login has failed');
  345. throw err;
  346. });
  347. ScreenNameNormalizer._queryUser = this.queryUser;
  348. const parseMediaError = (err) => {
  349. if (!(err instanceof instagram_private_api_1.IgResponseError && err.text === 'Media not found or unavailable')) {
  350. logger.warn(`error retrieving instagram media: ${err.message}`);
  351. return `获取媒体时出现错误:${err.message}`;
  352. }
  353. return '找不到请求的媒体,它可能已被删除。';
  354. };
  355. exports.getPostOwner = (segmentId) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))
  356. .then(media => media.items[0].user)
  357. .then(user => `${user.username}:${user.pk}`)
  358. .catch((err) => { throw Error(parseMediaError(err)); });
  359. exports.sendPost = (segmentId, receiver) => {
  360. this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver))
  361. .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); });
  362. };
  363. }
  364. }
  365. exports.default = default_1;