|
@@ -33,7 +33,6 @@ interface ITweet {
|
|
|
extended_entities: ExtendedEntities;
|
|
|
full_text: string;
|
|
|
display_text_range: [number, number];
|
|
|
- id: number;
|
|
|
id_str: string;
|
|
|
retweeted_status?: Tweet;
|
|
|
}
|
|
@@ -98,10 +97,11 @@ export default class {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
|
|
|
+ const currentFeed = lock.feed[lock.workon];
|
|
|
+ logger.debug(`pulling feed ${currentFeed}`);
|
|
|
|
|
|
const promise = new Promise(resolve => {
|
|
|
- let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
|
|
|
+ let match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
|
|
|
let config: any;
|
|
|
let endpoint: string;
|
|
|
if (match) {
|
|
@@ -112,7 +112,7 @@ export default class {
|
|
|
};
|
|
|
endpoint = 'lists/statuses';
|
|
|
} else {
|
|
|
- match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
|
|
|
+ match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)/);
|
|
|
if (match) {
|
|
|
config = {
|
|
|
screen_name: match[1],
|
|
@@ -124,18 +124,18 @@ export default class {
|
|
|
}
|
|
|
|
|
|
if (endpoint) {
|
|
|
- const offset = lock.threads[lock.feed[lock.workon]].offset;
|
|
|
+ const offset = lock.threads[currentFeed].offset as unknown as number;
|
|
|
if (offset > 0) config.since_id = offset;
|
|
|
this.client.get(endpoint, config, (error, tweets, response) => {
|
|
|
if (error) {
|
|
|
if (error instanceof Array && error.length > 0 && error[0].code === 34) {
|
|
|
- logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
|
|
|
- lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
|
|
|
- logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
|
|
|
- this.bot.sendTo(subscriber, `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`).catch();
|
|
|
+ logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
|
|
|
+ lock.threads[currentFeed].subscribers.forEach(subscriber => {
|
|
|
+ logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
|
|
|
+ this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
|
|
|
});
|
|
|
} else {
|
|
|
- logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
|
|
|
+ logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
|
|
|
}
|
|
|
resolve();
|
|
|
} else resolve(tweets);
|
|
@@ -143,17 +143,18 @@ export default class {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- promise.then((tweets: any) => {
|
|
|
- logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
|
|
|
- if (!tweets || tweets.length === 0) {
|
|
|
- lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
|
|
|
- return;
|
|
|
- }
|
|
|
- if (lock.threads[lock.feed[lock.workon]].offset === -1) {
|
|
|
- lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
|
|
|
- return;
|
|
|
- }
|
|
|
- if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
|
|
|
+ promise.then((tweets: Tweets) => {
|
|
|
+ logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
|
|
|
+ const currentThread = lock.threads[currentFeed];
|
|
|
+
|
|
|
+ const updateDate = () => currentThread.updatedAt = new Date().toString();
|
|
|
+ if (!tweets || tweets.length === 0) { updateDate(); return; }
|
|
|
+
|
|
|
+ const topOfFeed = tweets[0].id_str;
|
|
|
+ const updateOffset = () => currentThread.offset = topOfFeed;
|
|
|
+
|
|
|
+ if (currentThread.offset === '-1') { updateOffset(); return; }
|
|
|
+ if (currentThread.offset === '0') tweets.splice(1);
|
|
|
|
|
|
const maxCount = 3;
|
|
|
let sendTimeout = 10000;
|
|
@@ -171,8 +172,8 @@ export default class {
|
|
|
}
|
|
|
};
|
|
|
const sendTweets = (msg: MessageChain, text: string, author: string) => {
|
|
|
- lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
|
|
|
- logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
|
|
|
+ currentThread.subscribers.forEach(subscriber => {
|
|
|
+ logger.info(`pushing data of thread ${currentFeed} to ${JSON.stringify(subscriber)}`);
|
|
|
const retry = (reason, count: number) => { // workaround for https://github.com/mamoe/mirai/issues/194
|
|
|
if (count <= maxCount) sendTimeout *= (count + 2) / (count + 1);
|
|
|
setTimeout(() => {
|
|
@@ -193,11 +194,7 @@ export default class {
|
|
|
this.bot.sendTo(subscriber, msg, sendTimeout).catch(error => retry(error, 1));
|
|
|
});
|
|
|
};
|
|
|
- return (this.webshot as any)(tweets, sendTweets, this.webshotDelay)
|
|
|
- .then(() => {
|
|
|
- lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
|
|
|
- lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
|
|
|
- });
|
|
|
+ return this.webshot(tweets, sendTweets, this.webshotDelay).then(updateDate).then(updateOffset);
|
|
|
})
|
|
|
.then(() => {
|
|
|
lock.workon++;
|