Compare commits

...

7 Commits

Author SHA1 Message Date
Mads Marquart
3638fc5356 Made fetchThreadInfo able to fetch own user's info 2017-06-29 19:53:29 +02:00
Mads Marquart
0d5e4f6d3f Version up thanks to @enwar3 2017-06-29 16:03:55 +02:00
Mads Marquart
92a5ffdef8 Merge pull request #170 from OMGWINNING/master
Add extensible_attachment field to Message for fb share objects
2017-06-29 16:02:27 +02:00
Joe Lau
b3359fccdb Add last_message_timestamp to Thread objects 2017-06-28 18:08:45 -07:00
Joe Lau
d8f7366d1f Add extensible_attachment field to Message for fb share objects 2017-06-28 13:19:17 -07:00
Mads Marquart
ff94dc20af Minor cleanup 2017-06-28 16:06:13 +02:00
Mads Marquart
a8df0a548f Minor fixes 2017-06-28 14:42:11 +02:00
4 changed files with 27 additions and 22 deletions

View File

@@ -17,7 +17,7 @@ from .client import *
__copyright__ = 'Copyright 2015 - {} by Taehoon Kim'.format(datetime.now().year) __copyright__ = 'Copyright 2015 - {} by Taehoon Kim'.format(datetime.now().year)
__version__ = '1.0.7' __version__ = '1.0.11'
__license__ = 'BSD' __license__ = 'BSD'
__author__ = 'Taehoon Kim; Moreels Pieter-Jan; Mads Marquart' __author__ = 'Taehoon Kim; Moreels Pieter-Jan; Mads Marquart'
__email__ = 'carpedm20@gmail.com' __email__ = 'carpedm20@gmail.com'

View File

@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import requests import requests
import urllib import urllib
import traceback
from uuid import uuid1 from uuid import uuid1
from random import choice from random import choice
from datetime import datetime from datetime import datetime
@@ -72,6 +71,9 @@ class Client(object):
# If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login # If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login
if not session_cookies or not self.setSession(session_cookies) or not self.isLoggedIn(): if not session_cookies or not self.setSession(session_cookies) or not self.isLoggedIn():
self.login(email, password, max_tries) self.login(email, password, max_tries)
else:
self.email = email
self.password = password
""" """
INTERNAL REQUEST METHODS INTERNAL REQUEST METHODS
@@ -1279,7 +1281,6 @@ class Client(object):
thread_id = str(metadata['threadKey']['threadFbId']) thread_id = str(metadata['threadKey']['threadFbId'])
self.onPeopleAdded(mid=mid, added_ids=added_ids, author_id=author_id, thread_id=thread_id, self.onPeopleAdded(mid=mid, added_ids=added_ids, author_id=author_id, thread_id=thread_id,
ts=ts, msg=m) ts=ts, msg=m)
continue
# Left/removed participants # Left/removed participants
elif 'leftParticipantFbId' in delta: elif 'leftParticipantFbId' in delta:
@@ -1287,7 +1288,6 @@ class Client(object):
thread_id = str(metadata['threadKey']['threadFbId']) thread_id = str(metadata['threadKey']['threadFbId'])
self.onPersonRemoved(mid=mid, removed_id=removed_id, author_id=author_id, thread_id=thread_id, self.onPersonRemoved(mid=mid, removed_id=removed_id, author_id=author_id, thread_id=thread_id,
ts=ts, msg=m) ts=ts, msg=m)
continue
# Color change # Color change
elif delta_type == "change_thread_theme": elif delta_type == "change_thread_theme":
@@ -1295,7 +1295,6 @@ class Client(object):
thread_id, thread_type = getThreadIdAndThreadType(metadata) thread_id, thread_type = getThreadIdAndThreadType(metadata)
self.onColorChange(mid=mid, author_id=author_id, new_color=new_color, thread_id=thread_id, self.onColorChange(mid=mid, author_id=author_id, new_color=new_color, thread_id=thread_id,
thread_type=thread_type, ts=ts, metadata=metadata, msg=m) thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Emoji change # Emoji change
elif delta_type == "change_thread_icon": elif delta_type == "change_thread_icon":
@@ -1303,7 +1302,6 @@ class Client(object):
thread_id, thread_type = getThreadIdAndThreadType(metadata) thread_id, thread_type = getThreadIdAndThreadType(metadata)
self.onEmojiChange(mid=mid, author_id=author_id, new_emoji=new_emoji, thread_id=thread_id, self.onEmojiChange(mid=mid, author_id=author_id, new_emoji=new_emoji, thread_id=thread_id,
thread_type=thread_type, ts=ts, metadata=metadata, msg=m) thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Thread title change # Thread title change
elif delta.get("class") == "ThreadName": elif delta.get("class") == "ThreadName":
@@ -1311,7 +1309,6 @@ class Client(object):
thread_id, thread_type = getThreadIdAndThreadType(metadata) thread_id, thread_type = getThreadIdAndThreadType(metadata)
self.onTitleChange(mid=mid, author_id=author_id, new_title=new_title, thread_id=thread_id, self.onTitleChange(mid=mid, author_id=author_id, new_title=new_title, thread_id=thread_id,
thread_type=thread_type, ts=ts, metadata=metadata, msg=m) thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Nickname change # Nickname change
elif delta_type == "change_thread_nickname": elif delta_type == "change_thread_nickname":
@@ -1321,7 +1318,6 @@ class Client(object):
self.onNicknameChange(mid=mid, author_id=author_id, changed_for=changed_for, self.onNicknameChange(mid=mid, author_id=author_id, changed_for=changed_for,
new_nickname=new_nickname, new_nickname=new_nickname,
thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m) thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Message delivered # Message delivered
elif delta.get("class") == "DeliveryReceipt": elif delta.get("class") == "DeliveryReceipt":
@@ -1331,7 +1327,6 @@ class Client(object):
thread_id, thread_type = getThreadIdAndThreadType(delta) thread_id, thread_type = getThreadIdAndThreadType(delta)
self.onMessageDelivered(msg_ids=message_ids, delivered_for=delivered_for, self.onMessageDelivered(msg_ids=message_ids, delivered_for=delivered_for,
thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m) thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Message seen # Message seen
elif delta.get("class") == "ReadReceipt": elif delta.get("class") == "ReadReceipt":
@@ -1341,7 +1336,6 @@ class Client(object):
thread_id, thread_type = getThreadIdAndThreadType(delta) thread_id, thread_type = getThreadIdAndThreadType(delta)
self.onMessageSeen(seen_by=seen_by, thread_id=thread_id, thread_type=thread_type, self.onMessageSeen(seen_by=seen_by, thread_id=thread_id, thread_type=thread_type,
seen_ts=seen_ts, ts=delivered_ts, metadata=metadata, msg=m) seen_ts=seen_ts, ts=delivered_ts, metadata=metadata, msg=m)
continue
# Messages marked as seen # Messages marked as seen
elif delta.get("class") == "MarkRead": elif delta.get("class") == "MarkRead":
@@ -1354,18 +1348,20 @@ class Client(object):
# thread_id, thread_type = getThreadIdAndThreadType(delta) # thread_id, thread_type = getThreadIdAndThreadType(delta)
self.onMarkedSeen(threads=threads, seen_ts=seen_ts, ts=delivered_ts, metadata=delta, msg=m) self.onMarkedSeen(threads=threads, seen_ts=seen_ts, ts=delivered_ts, metadata=delta, msg=m)
continue
# New message # New message
elif delta.get("class") == "NewMessage": elif delta.get("class") == "NewMessage":
message = delta.get('body', '') message = delta.get('body', '')
thread_id, thread_type = getThreadIdAndThreadType(metadata) thread_id, thread_type = getThreadIdAndThreadType(metadata)
self.onMessage(mid=mid, author_id=author_id, message=message, self.onMessage(mid=mid, author_id=author_id, message=message,
thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=m, msg=m) thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
continue
# Unknown message type
else:
self.onUnknownMesssageType(msg=m)
# Inbox # Inbox
if mtype == "inbox": elif mtype == "inbox":
self.onInbox(unseen=m["unseen"], unread=m["unread"], recent_unread=m["recent_unread"], msg=m) self.onInbox(unseen=m["unseen"], unread=m["unread"], recent_unread=m["recent_unread"], msg=m)
# Typing # Typing
@@ -1493,7 +1489,7 @@ class Client(object):
:param exception: The exception that was encountered :param exception: The exception that was encountered
""" """
traceback.print_exc() log.exception('Got exception while listening')
def onMessage(self, mid=None, author_id=None, message=None, thread_id=None, thread_type=ThreadType.USER, ts=None, metadata=None, msg={}): def onMessage(self, mid=None, author_id=None, message=None, thread_id=None, thread_type=ThreadType.USER, ts=None, metadata=None, msg={}):

View File

@@ -30,7 +30,7 @@ def graphql_color_to_enum(color):
return ThreadColor.MESSENGER_BLUE return ThreadColor.MESSENGER_BLUE
try: try:
return ThreadColor('#{}'.format(color[2:].lower())) return ThreadColor('#{}'.format(color[2:].lower()))
except KeyError, ValueError: except ValueError:
raise Exception('Could not get ThreadColor from color: {}'.format(color)) raise Exception('Could not get ThreadColor from color: {}'.format(color))
def get_customization_info(thread): def get_customization_info(thread):
@@ -44,16 +44,18 @@ def get_customization_info(thread):
} }
if thread.get('thread_type') == 'GROUP' or thread.get('is_group_thread') or thread.get('thread_key', {}).get('thread_fbid'): if thread.get('thread_type') == 'GROUP' or thread.get('is_group_thread') or thread.get('thread_key', {}).get('thread_fbid'):
rtn['nicknames'] = {} rtn['nicknames'] = {}
for k in info['participant_customizations']: for k in info.get('participant_customizations', []):
rtn['nicknames'][k['participant_id']] = k.get('nickname') rtn['nicknames'][k['participant_id']] = k.get('nickname')
elif info.get('participant_customizations'): elif info.get('participant_customizations'):
_id = thread.get('thread_key', {}).get('other_user_id') or thread.get('id') _id = thread.get('thread_key', {}).get('other_user_id') or thread.get('id')
if info['participant_customizations'][0]['participant_id'] == _id: if info['participant_customizations'][0]['participant_id'] == _id:
rtn['nickname'] = info['participant_customizations'][0] rtn['nickname'] = info['participant_customizations'][0]
rtn['own_nickname'] = info['participant_customizations'][1] if len(info['participant_customizations']) > 1:
rtn['own_nickname'] = info['participant_customizations'][1]
elif info['participant_customizations'][1]['participant_id'] == _id: elif info['participant_customizations'][1]['participant_id'] == _id:
rtn['nickname'] = info['participant_customizations'][1] rtn['nickname'] = info['participant_customizations'][1]
rtn['own_nickname'] = info['participant_customizations'][0] if len(info['participant_customizations']) > 1:
rtn['own_nickname'] = info['participant_customizations'][0]
else: else:
raise Exception('No participant matching the user {} found: {}'.format(_id, info['participant_customizations'])) raise Exception('No participant matching the user {} found: {}'.format(_id, info['participant_customizations']))
return rtn return rtn
@@ -75,7 +77,8 @@ def graphql_to_message(message):
text=message.get('message').get('text'), text=message.get('message').get('text'),
mentions=[Mention(m.get('entity', {}).get('id'), offset=m.get('offset'), length=m.get('length')) for m in message.get('message').get('ranges', [])], mentions=[Mention(m.get('entity', {}).get('id'), offset=m.get('offset'), length=m.get('length')) for m in message.get('message').get('ranges', [])],
sticker=message.get('sticker'), sticker=message.get('sticker'),
attachments=message.get('blob_attachments') attachments=message.get('blob_attachments'),
extensible_attachment=message.get('extensible_attachment')
) )
def graphql_to_user(user): def graphql_to_user(user):

View File

@@ -13,13 +13,16 @@ class Thread(object):
photo = str photo = str
#: The name of the thread #: The name of the thread
name = str name = str
#: Timestamp of last message
last_message_timestamp = str
def __init__(self, _type, uid, photo=None, name=None): def __init__(self, _type, uid, photo=None, name=None, last_message_timestamp=None):
"""Represents a Facebook thread""" """Represents a Facebook thread"""
self.uid = str(uid) self.uid = str(uid)
self.type = _type self.type = _type
self.photo = photo self.photo = photo
self.name = name self.name = name
self.last_message_timestamp = last_message_timestamp
def __repr__(self): def __repr__(self):
return self.__unicode__() return self.__unicode__()
@@ -125,8 +128,10 @@ class Message(object):
sticker = str sticker = str
#: A list of attachments #: A list of attachments
attachments = list attachments = list
#: An extensible attachment, e.g. share object
extensible_attachment = dict
def __init__(self, uid, author=None, timestamp=None, is_read=None, reactions=[], text=None, mentions=[], sticker=None, attachments=[]): def __init__(self, uid, author=None, timestamp=None, is_read=None, reactions=[], text=None, mentions=[], sticker=None, attachments=[], extensible_attachment={}):
"""Represents a Facebook message""" """Represents a Facebook message"""
self.uid = uid self.uid = uid
self.author = author self.author = author
@@ -137,6 +142,7 @@ class Message(object):
self.mentions = mentions self.mentions = mentions
self.sticker = sticker self.sticker = sticker
self.attachments = attachments self.attachments = attachments
self.extensible_attachment = extensible_attachment
class Mention(object): class Mention(object):