From 99436b6ebb3a7784c478d39d742409db1854c29f Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Wed, 30 Jun 2021 15:56:53 -0500 Subject: [PATCH] Adding pagination support for longer chronological queries. --- .../instance/userMentionsFor..st | 2 +- .../instance/userTweetsFrom..st | 5 ++--- .../instance/userTweetsFrom.since.until..st | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom.since.until..st diff --git a/Datanalitica.package/TwitterAPI.class/instance/userMentionsFor..st b/Datanalitica.package/TwitterAPI.class/instance/userMentionsFor..st index 4fa1e3f..b1982aa 100644 --- a/Datanalitica.package/TwitterAPI.class/instance/userMentionsFor..st +++ b/Datanalitica.package/TwitterAPI.class/instance/userMentionsFor..st @@ -1,6 +1,6 @@ accessing userMentionsFor: username - | rawResponse queryURL parameters | + | rawResponse queryURL | "The following query gets the last tweets, that is the maximun allowed for a particular user without pagination:" queryURL := self usersBaseEndPoint, (self userIDFrom: username), '/mentions', self defaultQueryParameters. rawResponse := self rawResponseForURL:queryURL. diff --git a/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom..st b/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom..st index 9a24a9a..d102ea7 100644 --- a/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom..st +++ b/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom..st @@ -1,8 +1,7 @@ accessing userTweetsFrom: username - | rawResponse queryURL parameters | + | rawResponse queryURL | "The following query gets the last 100 tweets, that is the maximun allowed for a particular user without pagination:" - parameters := '?tweet.fields=created_at&expansions=author_id&user.fields=created_at&max_results=100'. - queryURL := self usersBaseEndPoint, (self userIDFrom: username), '/tweets', parameters. + queryURL := self usersBaseEndPoint, (self userIDFrom: username), '/tweets', self defaultQueryParameters. rawResponse := self rawResponseForURL:queryURL. ^ (STONJSON fromString: rawResponse) \ No newline at end of file diff --git a/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom.since.until..st b/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom.since.until..st new file mode 100644 index 0000000..1e360bf --- /dev/null +++ b/Datanalitica.package/TwitterAPI.class/instance/userTweetsFrom.since.until..st @@ -0,0 +1,18 @@ +accessing +userTweetsFrom: username since: startDateString until: endDateString + | nextToken queryUrl sinceDate untilDate messages response | + + sinceDate := 'start_time=',startDateString, 'T17:00:00Z'. + untilDate := 'end_time=',endDateString, 'T01:00:00Z'. + messages := OrderedCollection new. + nextToken := ''. + [ nextToken includesSubstring: 'stop' ] whileFalse: [ + queryUrl := self usersBaseEndPoint, + (self userIDFrom: username), '/tweets', self defaultQueryParameters, + '&', sinceDate, + '&', untilDate, + '&', nextToken. + response := STONJSON fromString: (self rawResponseForURL: queryUrl). + messages addAll: (response at: 'data'). + nextToken := 'pagination_token=',((response at: 'meta') at: 'next_token' ifAbsent: [ 'stop' ])]. +^ messages. \ No newline at end of file