twitter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.browserLogin = 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. exports.browserLogin = browserLogin;
  103. let getPostOwner = (segmentId) => Promise.reject();
  104. exports.getPostOwner = getPostOwner;
  105. let sendPost = (segmentId, receiver) => {
  106. throw Error();
  107. };
  108. exports.sendPost = sendPost;
  109. const logger = loggers_1.getLogger('instagram');
  110. const maxTrials = 3;
  111. const retryInterval = 1500;
  112. const ordinal = (n) => {
  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 = (doWork, onRetry) => new Promise(resolve => {
  125. const retry = (reason, count) => {
  126. setTimeout(() => {
  127. let terminate = false;
  128. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  129. if (!terminate)
  130. doWork().then(resolve).catch(error => retry(error, count + 1));
  131. }, retryInterval);
  132. };
  133. doWork().then(resolve).catch(error => retry(error, 1));
  134. });
  135. class default_1 {
  136. constructor(opt) {
  137. this.webshotCookies = [];
  138. this.launch = () => {
  139. this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => this.webshotCookies, () => setTimeout(this.work, this.workInterval * 1000));
  140. };
  141. this.queryUser = (username) => this.client.user.searchExact(username)
  142. .then(user => `${user.username}:${user.pk}`);
  143. this.workOnMedia = (mediaItems, sendMedia) => this.webshot(mediaItems, sendMedia, this.webshotDelay);
  144. this.urlSegmentToId = instagram_id_to_url_segment_1.urlSegmentToInstagramId;
  145. this.getMedia = (segmentId, sender) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))
  146. .then(media => {
  147. const mediaItem = media.items[0];
  148. logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${segmentId}`);
  149. return this.workOnMedia([mediaItem], sender);
  150. });
  151. this.sendMedia = (source, ...to) => (msg, text, author) => {
  152. to.forEach(subscriber => {
  153. logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
  154. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  155. if (count <= maxTrials) {
  156. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  157. }
  158. else {
  159. logger.warn(`${count - 1} consecutive failures while sending` +
  160. 'message chain, trying plain text instead...');
  161. terminate(this.bot.sendTo(subscriber, author + text));
  162. }
  163. });
  164. });
  165. };
  166. this.work = () => {
  167. const lock = this.lock;
  168. if (this.workInterval < 1)
  169. this.workInterval = 1;
  170. if (lock.feed.length === 0) {
  171. setTimeout(() => {
  172. this.work();
  173. }, this.workInterval * 1000);
  174. return;
  175. }
  176. if (lock.workon >= lock.feed.length)
  177. lock.workon = 0;
  178. if (!lock.threads[lock.feed[lock.workon]] ||
  179. !lock.threads[lock.feed[lock.workon]].subscribers ||
  180. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  181. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  182. delete lock.threads[lock.feed[lock.workon]];
  183. lock.feed.splice(lock.workon, 1);
  184. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  185. this.work();
  186. return;
  187. }
  188. const currentFeed = lock.feed[lock.workon];
  189. logger.debug(`pulling feed ${currentFeed}`);
  190. const promise = new Promise(resolve => {
  191. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  192. if (match) {
  193. const feed = this.client.feed.user(lock.threads[currentFeed].id);
  194. const newer = (item) => utils_1.BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  195. const fetchMore = () => new Promise(fetch => {
  196. feed.request().then(response => {
  197. if (response.items.length === 0)
  198. return fetch([]);
  199. if (response.items.every(newer)) {
  200. fetchMore().then(fetched => fetch(response.items.concat(fetched)));
  201. }
  202. else
  203. fetch(response.items.filter(newer));
  204. }, (error) => {
  205. if (error instanceof instagram_private_api_1.IgNetworkError) {
  206. logger.warn(`error on fetching media for ${currentFeed}: ${JSON.stringify(error.cause)}`);
  207. if (!(error instanceof instagram_private_api_1.IgNotFoundError))
  208. return;
  209. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  210. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  211. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  212. });
  213. }
  214. else {
  215. logger.error(`unhandled error on fetching media for ${currentFeed}: ${JSON.stringify(error)}`);
  216. }
  217. fetch([]);
  218. });
  219. });
  220. fetchMore().then(resolve);
  221. }
  222. });
  223. promise.then((mediaItems) => {
  224. const currentThread = lock.threads[currentFeed];
  225. const updateDate = () => currentThread.updatedAt = new Date().toString();
  226. if (!mediaItems || mediaItems.length === 0) {
  227. updateDate();
  228. return;
  229. }
  230. const topOfFeed = mediaItems[0].pk;
  231. const updateOffset = () => currentThread.offset = topOfFeed;
  232. if (currentThread.offset === '-1') {
  233. updateOffset();
  234. return;
  235. }
  236. if (currentThread.offset === '0')
  237. mediaItems.splice(1);
  238. return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
  239. .then(updateDate).then(updateOffset);
  240. })
  241. .then(() => {
  242. lock.workon++;
  243. let timeout = this.workInterval * 1000 / lock.feed.length;
  244. if (timeout < 1000)
  245. timeout = 1000;
  246. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  247. setTimeout(() => {
  248. this.work();
  249. }, timeout);
  250. });
  251. };
  252. this.client = new instagram_private_api_1.IgApiClient();
  253. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials);
  254. this.lockfile = opt.lockfile;
  255. this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
  256. this.lock = opt.lock;
  257. this.workInterval = opt.workInterval;
  258. this.bot = opt.bot;
  259. this.webshotDelay = opt.webshotDelay;
  260. this.mode = opt.mode;
  261. this.wsUrl = opt.wsUrl;
  262. const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
  263. try {
  264. this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
  265. logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
  266. }
  267. catch (err) {
  268. logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message);
  269. logger.warn('cookies will be saved to this file when needed');
  270. }
  271. exports.browserLogin = (page) => {
  272. logger.warn('blocked by login dialog, trying to log in manually...');
  273. return page.type('input[name="username"]', opt.credentials[0])
  274. .then(() => page.type('input[name="password"]', opt.credentials[1]))
  275. .then(() => page.click('button[type="submit"]'))
  276. .then(() => page.click('button:has-text("情報を保存")'))
  277. .then(() => page.waitForSelector('img[data-testid="user-avatar"]', { timeout: this.webshotDelay }))
  278. .then(() => page.context().cookies())
  279. .then(cookies => {
  280. this.webshotCookies = cookies;
  281. logger.info('successfully logged in, saving cookies to file...');
  282. fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8');
  283. })
  284. .catch((err) => {
  285. if (err.name === 'TimeoutError')
  286. logger.warn('navigation timed out, assuming login has failed');
  287. throw err;
  288. });
  289. };
  290. ScreenNameNormalizer._queryUser = this.queryUser;
  291. const parseMediaError = (err) => {
  292. if (!(err instanceof instagram_private_api_1.IgResponseError && err.text === 'Media not found or unavailable')) {
  293. logger.warn(`error retrieving instagram media: ${err.message}`);
  294. return `获取媒体时出现错误:${err.message}`;
  295. }
  296. return '找不到请求的媒体,它可能已被删除。';
  297. };
  298. exports.getPostOwner = (segmentId) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))
  299. .then(media => media.items[0].user)
  300. .then(user => `${user.username}:${user.pk}`)
  301. .catch((err) => { throw Error(parseMediaError(err)); });
  302. exports.sendPost = (segmentId, receiver) => {
  303. this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver))
  304. .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); });
  305. };
  306. }
  307. }
  308. exports.default = default_1;