Skip to content
Closed
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions fbchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from .graphql import *
import time



class Client(object):
"""A client for the Facebook Chat (Messenger).

Expand Down Expand Up @@ -735,14 +733,16 @@ def fetchThreadMessages(self, thread_id=None, limit=20, before=None):
:param limit: Max. number of messages to retrieve
:param before: A timestamp, indicating from which point to retrieve messages
:type limit: int
:type before: int
:type before: datetime
:return: :class:`models.Message` objects
:rtype: list
:raises: FBchatException if request failed
"""

thread_id, thread_type = self._getThread(thread_id, None)

if before is not None:
before = int(unix_time_millis(before))

j = self.graphql_request(GraphQL(doc_id='1386147188135407', params={
'id': thread_id,
'message_limit': limit,
Expand All @@ -764,7 +764,7 @@ def fetchThreadList(self, offset=None, limit=20, thread_location=ThreadLocation.
:param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
:param before: A timestamp (in milliseconds), indicating from which point to retrieve threads
:type limit: int
:type before: int
:type before: datetime
:return: :class:`models.Thread` objects
:rtype: list
:raises: FBchatException if request failed
Expand All @@ -781,6 +781,9 @@ def fetchThreadList(self, offset=None, limit=20, thread_location=ThreadLocation.
else:
raise FBchatUserError('"thread_location" must be a value of ThreadLocation')

if before is not None:
before = unix_time_millis(before)

j = self.graphql_request(GraphQL(doc_id='1349387578499440', params={
'limit': limit,
'tags': [loc_str],
Expand Down
9 changes: 6 additions & 3 deletions fbchat/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
from .models import *
from .utils import *
from datetime import datetime

# Shameless copy from https://stackoverflow.com/a/8730674
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
Expand Down Expand Up @@ -141,7 +142,9 @@ def graphql_to_message(message):
)
rtn.uid = str(message.get('message_id'))
rtn.author = str(message.get('message_sender').get('id'))
rtn.timestamp = message.get('timestamp_precise')

rtn.timestamp = millis_to_datetime(message.get('timestamp_precise'))

if message.get('unread') is not None:
rtn.is_read = not message['unread']
rtn.reactions = {str(r['user']['id']):MessageReaction(r['reaction']) for r in message.get('message_reactions')}
Expand Down Expand Up @@ -183,7 +186,7 @@ def graphql_to_thread(thread):
user = next(p for p in participants if p['id'] == thread['thread_key']['other_user_id'])
last_message_timestamp = None
if 'last_message' in thread:
last_message_timestamp = thread['last_message']['nodes'][0]['timestamp_precise']
last_message_timestamp = millis_to_datetime(thread['last_message']['nodes'][0]['timestamp_precise'])

return User(
user['id'],
Expand Down Expand Up @@ -211,7 +214,7 @@ def graphql_to_group(group):
c_info = get_customization_info(group)
last_message_timestamp = None
if 'last_message' in group:
last_message_timestamp = group['last_message']['nodes'][0]['timestamp_precise']
last_message_timestamp = millis_to_datetime(group['last_message']['nodes'][0]['timestamp_precise'])
return Group(
group['thread_key']['thread_fbid'],
participants=set([node['messaging_actor']['id'] for node in group['all_participants']['nodes']]),
Expand Down
12 changes: 12 additions & 0 deletions fbchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import unicode_literals
import re
import json
from datetime import datetime
from time import time
from random import random
import warnings
Expand All @@ -29,6 +30,9 @@
handler = logging.StreamHandler()
log.addHandler(handler)

# Unix epoch
epoch = datetime.utcfromtimestamp(0)

#: Default list of user agents
USER_AGENTS = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
Expand Down Expand Up @@ -233,3 +237,11 @@ def get_emojisize_from_tags(tags):
except (KeyError, IndexError):
log.exception('Could not determine emoji size from {} - {}'.format(tags, tmp))
return None

def unix_time_millis(dt):
return (dt - epoch).total_seconds() * 1000.0

def millis_to_datetime(t):
if isinstance(t, str):
t = float(t)
return datetime.fromtimestamp(t / 1000.0)