Skip to content

Commit

Permalink
Merge pull request #228 from PLhery/fix/invalid-until-id-with-start-time
Browse files Browse the repository at this point in the history
fix: Auto-convert start_time to a since_id when until ID is required
  • Loading branch information
alkihis authored Mar 19, 2022
2 parents c852d05 + 92a10ca commit 2c41ad6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/paginators/tweet.paginator.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ abstract class TweetTimelineV2Paginator<
if (this._realData.meta.next_token) {
params.next_token = this._realData.meta.next_token;
} else {
if (params.start_time) {
// until_id and start_time are forbidden together for some reason, so convert start_time to a since_id.
params.since_id = this.dateStringToSnowflakeId(params.start_time as string);
delete params.start_time;
}
if (params.end_time) {
// until_id overrides end_time, so delete it
delete params.end_time;
}

params.until_id = this._realData.meta.oldest_id;
}

Expand Down Expand Up @@ -82,6 +92,18 @@ abstract class TweetTimelineV2Paginator<
return this.tweets;
}

protected dateStringToSnowflakeId(dateStr: string) {
const TWITTER_START_EPOCH = BigInt('1288834974657');
const date = new Date(dateStr);

if (isNaN(date.valueOf())) {
throw new Error('Unable to convert start_time/end_time to a valid date. A ISO 8601 DateTime is excepted, please check your input.');
}

const dateTimestamp = BigInt(date.valueOf());
return ((dateTimestamp - TWITTER_START_EPOCH) << BigInt('22')).toString();
}

/**
* Tweets returned by paginator.
*/
Expand Down

0 comments on commit 2c41ad6

Please sign in to comment.