Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Auto-convert start_time to a since_id when until ID is required #228

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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