Make Client fetching methods use datetime objects

On:
- Client.fetchThreads after and before arguments
- Client.fetchThreadMessages before argument
- Client.fetchThreadList before argument
This commit is contained in:
Mads Marquart
2019-09-08 14:49:49 +02:00
parent d12f9fd645
commit ba088d45a7

View File

@@ -289,8 +289,10 @@ class Client:
Args: Args:
thread_location (ThreadLocation): INBOX, PENDING, ARCHIVED or OTHER thread_location (ThreadLocation): INBOX, PENDING, ARCHIVED or OTHER
before: Fetch only thread before this epoch (in ms) (default all threads) before (datetime.datetime): Fetch only threads before this (default all
after: Fetch only thread after this epoch (in ms) (default all threads) threads). Must be timezone-aware!
after (datetime.datetime): Fetch only threads after this (default all
threads). Must be timezone-aware!
limit: The max. amount of threads to fetch (default all threads) limit: The max. amount of threads to fetch (default all threads)
Returns: Returns:
@@ -301,15 +303,15 @@ class Client:
""" """
threads = [] threads = []
last_thread_timestamp = None last_thread_dt = None
while True: while True:
# break if limit is exceeded # break if limit is exceeded
if limit and len(threads) >= limit: if limit and len(threads) >= limit:
break break
# fetchThreadList returns at max 20 threads before last_thread_timestamp (included) # fetchThreadList returns at max 20 threads before last_thread_dt (included)
candidates = self.fetchThreadList( candidates = self.fetchThreadList(
before=last_thread_timestamp, thread_location=thread_location before=last_thread_dt, thread_location=thread_location
) )
if len(candidates) > 1: if len(candidates) > 1:
@@ -317,20 +319,22 @@ class Client:
else: # End of threads else: # End of threads
break break
last_thread_timestamp = threads[-1].last_message_timestamp last_thread_dt = _util.millis_to_datetime(
threads[-1].last_message_timestamp
)
# FB returns a sorted list of threads # FB returns a sorted list of threads
if (before is not None and int(last_thread_timestamp) > before) or ( if (before is not None and last_thread_dt > before) or (
after is not None and int(last_thread_timestamp) < after after is not None and last_thread_dt < after
): ):
break break
# Return only threads between before and after (if set) # Return only threads between before and after (if set)
if before is not None or after is not None: if before is not None or after is not None:
for t in threads: for t in threads:
last_message_timestamp = int(t.last_message_timestamp) last_message_dt = _util.millis_to_datetime(t.last_message_timestamp)
if (before is not None and last_message_timestamp > before) or ( if (before is not None and last_message_dt > before) or (
after is not None and last_message_timestamp < after after is not None and last_message_dt < after
): ):
threads.remove(t) threads.remove(t)
@@ -746,7 +750,7 @@ class Client:
Args: Args:
thread_id: User/Group ID to get messages from. See :ref:`intro_threads` thread_id: User/Group ID to get messages from. See :ref:`intro_threads`
limit (int): Max. number of messages to retrieve limit (int): Max. number of messages to retrieve
before (int): A timestamp, indicating from which point to retrieve messages before (datetime.datetime): The point from which to retrieve messages
Returns: Returns:
list: :class:`Message` objects list: :class:`Message` objects
@@ -761,7 +765,7 @@ class Client:
"message_limit": limit, "message_limit": limit,
"load_messages": True, "load_messages": True,
"load_read_receipts": True, "load_read_receipts": True,
"before": before, "before": _util.datetime_to_millis(before) if before else None,
} }
j, = self.graphql_requests(_graphql.from_doc_id("1860982147341344", params)) j, = self.graphql_requests(_graphql.from_doc_id("1860982147341344", params))
@@ -792,7 +796,7 @@ class Client:
offset: Deprecated. Do not use! offset: Deprecated. Do not use!
limit (int): Max. number of threads to retrieve. Capped at 20 limit (int): Max. number of threads to retrieve. Capped at 20
thread_location (ThreadLocation): INBOX, PENDING, ARCHIVED or OTHER thread_location (ThreadLocation): INBOX, PENDING, ARCHIVED or OTHER
before (int): A timestamp (in milliseconds), indicating from which point to retrieve threads before (datetime.datetime): The point from which to retrieve threads
Returns: Returns:
list: :class:`Thread` objects list: :class:`Thread` objects
@@ -818,7 +822,7 @@ class Client:
params = { params = {
"limit": limit, "limit": limit,
"tags": [loc_str], "tags": [loc_str],
"before": before, "before": _util.datetime_to_millis(before) if before else None,
"includeDeliveryReceipts": True, "includeDeliveryReceipts": True,
"includeSeqID": False, "includeSeqID": False,
} }