twitter.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import {
  4. instagramIdToUrlSegment as idToUrlSegment,
  5. urlSegmentToInstagramId as urlSegmentToId
  6. } from 'instagram-id-to-url-segment';
  7. import {
  8. IgApiClient,
  9. IgClientError, IgExactUserNotFoundError, IgNetworkError, IgNotFoundError, IgResponseError,
  10. MediaInfoResponseItemsItem, UserFeedResponseItemsItem
  11. } from 'instagram-private-api';
  12. import { RequestError } from 'request-promise/errors';
  13. import { getLogger } from './loggers';
  14. import QQBot, { Message } from './koishi';
  15. import { BigNumOps } from './utils';
  16. import Webshot, { Cookies, Page } from './webshot';
  17. const parseLink = (link: string): {userName?: string, postUrlSegment?: string} => {
  18. let match =
  19. /instagram\.com\/p\/([A-Za-z0-9\-_]+)/.exec(link);
  20. if (match) return {postUrlSegment: match[1]};
  21. match =
  22. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  23. /^([^\/?#]+)$/.exec(link);
  24. if (match) return {userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0]};
  25. return;
  26. };
  27. const isValidUrlSegment = (input: string) => /^[A-Za-z0-9\-_]+$/.test(input);
  28. const linkBuilder = (config: ReturnType<typeof parseLink>): string => {
  29. if (config.userName) return `https://www.instagram.com/${config.userName}/`;
  30. if (config.postUrlSegment) return `https://www.instagram.com/p/${config.postUrlSegment}/`;
  31. };
  32. export {linkBuilder, parseLink, isValidUrlSegment, idToUrlSegment, urlSegmentToId};
  33. interface IWorkerOption {
  34. sessionLockfile: string;
  35. credentials: [string, string];
  36. lock: ILock;
  37. lockfile: string;
  38. webshotCookiesLockfile: string;
  39. bot: QQBot;
  40. workInterval: number;
  41. webshotDelay: number;
  42. mode: number;
  43. wsUrl: string;
  44. }
  45. export class SessionManager {
  46. private ig: IgApiClient;
  47. private username: string;
  48. private password: string;
  49. private lockfile: string;
  50. constructor(client: IgApiClient, file: string, credentials: [string, string]) {
  51. this.ig = client;
  52. this.lockfile = file;
  53. [this.username, this.password] = credentials;
  54. }
  55. public init = () => {
  56. this.ig.state.generateDevice(this.username);
  57. this.ig.request.end$.subscribe(() => { this.save(); });
  58. const filePath = path.resolve(this.lockfile);
  59. if (fs.existsSync(filePath)) {
  60. try {
  61. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8')) as {[key: string]: any};
  62. return this.ig.state.deserialize(serialized).then(() => {
  63. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  64. });
  65. } catch (err) {
  66. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  67. return Promise.resolve();
  68. }
  69. } else return this.login();
  70. };
  71. public login = () =>
  72. this.ig.simulate.preLoginFlow()
  73. .then(() => this.ig.account.login(this.username, this.password))
  74. .then(() => new Promise(resolve => {
  75. logger.info(`successfully logged in as ${this.username}`);
  76. process.nextTick(() => resolve(this.ig.simulate.postLoginFlow()));
  77. }));
  78. public save = () =>
  79. this.ig.state.serialize()
  80. .then((serialized: {[key: string]: any}) => {
  81. delete serialized.constants;
  82. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  83. });
  84. }
  85. export class ScreenNameNormalizer {
  86. // tslint:disable-next-line: variable-name
  87. public static _queryUser: (username: string) => Promise<string>;
  88. public static normalize = (username: string) => `${username.toLowerCase().replace(/^@/, '')}:`;
  89. public static async normalizeLive(username: string) {
  90. if (this._queryUser) {
  91. return await this._queryUser(username)
  92. .catch((err: IgClientError) => {
  93. if (!(err instanceof IgExactUserNotFoundError)) {
  94. logger.warn(`error looking up user: ${err.message}`);
  95. return `${username}:`;
  96. }
  97. return null;
  98. });
  99. }
  100. return this.normalize(username);
  101. }
  102. }
  103. export let browserLogin = (page: Page): Promise<void> => Promise.reject();
  104. export let getPostOwner = (segmentId: string): Promise<string> => Promise.reject();
  105. export let sendPost = (segmentId: string, receiver: IChat): void => {
  106. throw Error();
  107. };
  108. export type MediaItem = MediaInfoResponseItemsItem & UserFeedResponseItemsItem;
  109. const logger = getLogger('instagram');
  110. const maxTrials = 3;
  111. const retryInterval = 1500;
  112. const ordinal = (n: number) => {
  113. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  114. case 1:
  115. return `${n}st`;
  116. case 2:
  117. return `${n}nd`;
  118. case 3:
  119. return `${n}rd`;
  120. default:
  121. return `${n}th`;
  122. }
  123. };
  124. const retryOnError = <T, U>(
  125. doWork: () => Promise<T>,
  126. onRetry: (error, count: number, terminate: (defaultValue: U) => void) => void
  127. ) => new Promise<T | U>(resolve => {
  128. const retry = (reason, count: number) => {
  129. setTimeout(() => {
  130. let terminate = false;
  131. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  132. if (!terminate) doWork().then(resolve).catch(error => retry(error, count + 1));
  133. }, retryInterval);
  134. };
  135. doWork().then(resolve).catch(error => retry(error, 1));
  136. });
  137. export default class {
  138. private client: IgApiClient;
  139. private lock: ILock;
  140. private lockfile: string;
  141. private workInterval: number;
  142. private bot: QQBot;
  143. private webshotDelay: number;
  144. private webshotCookies: Cookies;
  145. private webshotCookiesLockfile: string;
  146. private webshot: Webshot;
  147. private mode: number;
  148. private wsUrl: string;
  149. public session: SessionManager;
  150. constructor(opt: IWorkerOption) {
  151. this.client = new IgApiClient();
  152. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials);
  153. this.lockfile = opt.lockfile;
  154. this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
  155. this.lock = opt.lock;
  156. this.workInterval = opt.workInterval;
  157. this.bot = opt.bot;
  158. this.webshotDelay = opt.webshotDelay;
  159. this.mode = opt.mode;
  160. this.wsUrl = opt.wsUrl;
  161. const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
  162. if (fs.existsSync(cookiesFilePath)) {
  163. try {
  164. this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8')) as Cookies;
  165. logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
  166. } catch (err) {
  167. logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err);
  168. logger.warn('cookies will be saved to this file when needed');
  169. }
  170. }
  171. browserLogin = (page) => {
  172. logger.warn('blocked by login dialog, trying to log in manually...');
  173. return page.type('input[name="username"]', opt.credentials[0])
  174. .then(() => page.type('input[name="password"]', opt.credentials[1]))
  175. .then(() => page.click('button[type="submit"]'))
  176. .then(() => page.click('button:has-text("情報を保存")'))
  177. .then(() => page.waitForSelector('img[data-testid="user-avatar"]', {timeout: this.webshotDelay}))
  178. .then(() => page.context().cookies())
  179. .then(cookies => {
  180. this.webshotCookies = cookies;
  181. logger.info('successfully logged in, saving cookies to file...');
  182. fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8');
  183. })
  184. .catch((err: Error) => {
  185. if (err.name === 'TimeoutError') logger.warn('navigation timed out, assuming login has failed');
  186. throw err;
  187. });
  188. };
  189. ScreenNameNormalizer._queryUser = this.queryUser;
  190. const parseMediaError = (err: IgClientError) => {
  191. if (!(err instanceof IgResponseError && err.text === 'Media not found or unavailable')) {
  192. logger.warn(`error retrieving instagram media: ${err.message}`);
  193. return `获取媒体时出现错误:${err.message}`;
  194. }
  195. return '找不到请求的媒体,它可能已被删除。';
  196. };
  197. getPostOwner = (segmentId) =>
  198. this.client.media.info(urlSegmentToId(segmentId))
  199. .then(media => media.items[0].user)
  200. .then(user => `${user.username}:${user.pk}`)
  201. .catch((err: IgClientError) => { throw Error(parseMediaError(err)); });
  202. sendPost = (segmentId, receiver) => {
  203. this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver))
  204. .catch((err: IgClientError) => { this.bot.sendTo(receiver, parseMediaError(err)); });
  205. };
  206. }
  207. public launch = () => {
  208. this.webshot = new Webshot(
  209. this.wsUrl,
  210. this.mode,
  211. () => this.webshotCookies,
  212. () => setTimeout(this.work, this.workInterval * 1000)
  213. );
  214. };
  215. public queryUser = (username: string) => this.client.user.searchExact(username)
  216. .then(user => `${user.username}:${user.pk}`);
  217. private workOnMedia = (
  218. mediaItems: MediaItem[],
  219. sendMedia: (msg: string, text: string, author: string) => void
  220. ) => this.webshot(mediaItems, sendMedia, this.webshotDelay);
  221. public urlSegmentToId = urlSegmentToId;
  222. public getMedia = (segmentId: string, sender: (msg: string, text: string, author: string) => void) =>
  223. this.client.media.info(urlSegmentToId(segmentId))
  224. .then(media => {
  225. const mediaItem = media.items[0] as MediaItem;
  226. logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${segmentId}`);
  227. return this.workOnMedia([mediaItem], sender);
  228. });
  229. private sendMedia = (source?: string, ...to: IChat[]) => (msg: string, text: string, author: string) => {
  230. to.forEach(subscriber => {
  231. logger.info(`pushing data${source ? ` of ${Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
  232. retryOnError(
  233. () => this.bot.sendTo(subscriber, msg),
  234. (_, count, terminate: (doNothing: Promise<void>) => void) => {
  235. if (count <= maxTrials) {
  236. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  237. } else {
  238. logger.warn(`${count - 1} consecutive failures while sending` +
  239. 'message chain, trying plain text instead...');
  240. terminate(this.bot.sendTo(subscriber, author + text));
  241. }
  242. });
  243. });
  244. };
  245. public work = () => {
  246. const lock = this.lock;
  247. if (this.workInterval < 1) this.workInterval = 1;
  248. if (lock.feed.length === 0) {
  249. setTimeout(() => {
  250. this.work();
  251. }, this.workInterval * 1000);
  252. return;
  253. }
  254. if (lock.workon >= lock.feed.length) lock.workon = 0;
  255. if (!lock.threads[lock.feed[lock.workon]] ||
  256. !lock.threads[lock.feed[lock.workon]].subscribers ||
  257. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  258. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  259. delete lock.threads[lock.feed[lock.workon]];
  260. lock.feed.splice(lock.workon, 1);
  261. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  262. this.work();
  263. return;
  264. }
  265. const currentFeed = lock.feed[lock.workon];
  266. logger.debug(`pulling feed ${currentFeed}`);
  267. const promise = new Promise<UserFeedResponseItemsItem[]>(resolve => {
  268. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  269. if (match) {
  270. const feed = this.client.feed.user(lock.threads[currentFeed].id);
  271. const newer = (item: UserFeedResponseItemsItem) =>
  272. BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  273. const fetchMore = () => new Promise<UserFeedResponseItemsItem[]>(fetch => {
  274. feed.request().then(response => {
  275. if (response.items.length === 0) return fetch([]);
  276. if (response.items.every(newer)) {
  277. fetchMore().then(fetched => fetch(response.items.concat(fetched)));
  278. } else fetch(response.items.filter(newer));
  279. }, (error: IgClientError & Partial<RequestError>) => {
  280. if (error instanceof IgNetworkError) {
  281. logger.warn(`error on fetching media for ${currentFeed}: ${JSON.stringify(error.cause)}`);
  282. if (!(error instanceof IgNotFoundError)) return;
  283. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  284. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  285. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  286. });
  287. } else {
  288. logger.error(`unhandled error on fetching media for ${currentFeed}: ${JSON.stringify(error)}`);
  289. }
  290. fetch([]);
  291. });
  292. });
  293. fetchMore().then(resolve);
  294. }
  295. });
  296. promise.then((mediaItems: MediaItem[]) => {
  297. const currentThread = lock.threads[currentFeed];
  298. const updateDate = () => currentThread.updatedAt = new Date().toString();
  299. if (!mediaItems || mediaItems.length === 0) { updateDate(); return; }
  300. const topOfFeed = mediaItems[0].pk;
  301. const updateOffset = () => currentThread.offset = topOfFeed;
  302. if (currentThread.offset === '-1') { updateOffset(); return; }
  303. if (currentThread.offset === '0') mediaItems.splice(1);
  304. return this.workOnMedia(mediaItems, this.sendMedia(`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) timeout = 1000;
  311. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  312. setTimeout(() => {
  313. this.work();
  314. }, timeout);
  315. });
  316. };
  317. }