Skip to content

Commit fd3a708

Browse files
vdboormarclove
authored andcommitted
Add since/until parameters to insights()
1 parent 654a1f8 commit fd3a708

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

src/pythreads/api.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,12 @@ async def manage_reply(self, reply_id: str, hide: bool):
898898

899899
return await self._post(url)
900900

901-
async def insights(self, thread_id: str):
901+
async def insights(
902+
self,
903+
thread_id: str,
904+
since: Optional[datetime] = None,
905+
until: Optional[datetime] = None,
906+
):
902907
"""Retrieve the available insights metrics
903908
904909
Returned metrics do not capture nested replies' metrics.
@@ -907,6 +912,8 @@ async def insights(self, thread_id: str):
907912
908913
Args:
909914
thread_id: The thread media id whose metrics you're requesting
915+
since: [optional] The starting datetime of the time window you are requesting
916+
until: [optional] The ending datetime of the time window you are requesting
910917
911918
Returns:
912919
The JSON response as a dict. Requests all available metrics.
@@ -917,20 +924,27 @@ async def insights(self, thread_id: str):
917924
"""
918925

919926
access_token = self._access_token()
927+
params = {
928+
PARAMS__METRIC: ",".join(
929+
[
930+
FIELD__LIKES,
931+
FIELD__QUOTES,
932+
FIELD__REPLIES,
933+
FIELD__REPOSTS,
934+
FIELD__VIEWS,
935+
]
936+
)
937+
}
938+
939+
if since:
940+
params["since"] = int(since.timestamp())
941+
942+
if until:
943+
params["until"] = int(until.timestamp())
920944

921945
url = Threads.build_graph_api_url(
922946
f"{thread_id}/insights",
923-
{
924-
PARAMS__METRIC: ",".join(
925-
[
926-
FIELD__LIKES,
927-
FIELD__QUOTES,
928-
FIELD__REPLIES,
929-
FIELD__REPOSTS,
930-
FIELD__VIEWS,
931-
]
932-
)
933-
},
947+
params,
934948
access_token,
935949
)
936950

0 commit comments

Comments
 (0)