Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4f947cdbb5 | ||
|
ec6c29052a | ||
|
6b117502f3 | ||
|
3e7b20c379 | ||
|
f4a997c0ef | ||
|
84fa15e44c | ||
|
7b8ecf8fe3 | ||
|
95989b6da7 | ||
|
22e57f99a1 | ||
|
b9d29c0417 | ||
|
45d8b45d96 | ||
|
b6a6d7dc68 | ||
|
c57b84cd0b | ||
|
78e7841b5e | ||
|
e41d981449 | ||
|
381227af66 | ||
|
2f8d0728ba | ||
|
13bfc5f2f9 | ||
|
d4446280c7 | ||
|
861f17bc4d | ||
|
41bbe18e3d | ||
|
d32b7b612a | ||
|
8739318101 | ||
|
1ac569badd | ||
|
b7ea8e6001 | ||
|
b0bf5ba8e0 | ||
|
8169a5f776 | ||
|
c6dc432d06 | ||
|
9e8fe7bc1e | ||
|
90813c959d | ||
|
940a65954c | ||
|
9b4e753a79 | ||
|
e0be9029e4 |
@@ -15,7 +15,7 @@ from __future__ import unicode_literals
|
|||||||
from .client import *
|
from .client import *
|
||||||
|
|
||||||
__title__ = 'fbchat'
|
__title__ = 'fbchat'
|
||||||
__version__ = '1.4.2'
|
__version__ = '1.5.0'
|
||||||
__description__ = 'Facebook Chat (Messenger) for Python'
|
__description__ = 'Facebook Chat (Messenger) for Python'
|
||||||
|
|
||||||
__copyright__ = 'Copyright 2015 - 2018 by Taehoon Kim'
|
__copyright__ = 'Copyright 2015 - 2018 by Taehoon Kim'
|
||||||
|
299
fbchat/client.py
299
fbchat/client.py
@@ -502,7 +502,7 @@ class Client(object):
|
|||||||
|
|
||||||
return users
|
return users
|
||||||
|
|
||||||
def searchForUsers(self, name, limit=1):
|
def searchForUsers(self, name, limit=10):
|
||||||
"""
|
"""
|
||||||
Find and get user by his/her name
|
Find and get user by his/her name
|
||||||
|
|
||||||
@@ -517,7 +517,7 @@ class Client(object):
|
|||||||
|
|
||||||
return [graphql_to_user(node) for node in j[name]['users']['nodes']]
|
return [graphql_to_user(node) for node in j[name]['users']['nodes']]
|
||||||
|
|
||||||
def searchForPages(self, name, limit=1):
|
def searchForPages(self, name, limit=10):
|
||||||
"""
|
"""
|
||||||
Find and get page by its name
|
Find and get page by its name
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ class Client(object):
|
|||||||
|
|
||||||
return [graphql_to_page(node) for node in j[name]['pages']['nodes']]
|
return [graphql_to_page(node) for node in j[name]['pages']['nodes']]
|
||||||
|
|
||||||
def searchForGroups(self, name, limit=1):
|
def searchForGroups(self, name, limit=10):
|
||||||
"""
|
"""
|
||||||
Find and get group thread by its name
|
Find and get group thread by its name
|
||||||
|
|
||||||
@@ -546,7 +546,7 @@ class Client(object):
|
|||||||
|
|
||||||
return [graphql_to_group(node) for node in j['viewer']['groups']['nodes']]
|
return [graphql_to_group(node) for node in j['viewer']['groups']['nodes']]
|
||||||
|
|
||||||
def searchForThreads(self, name, limit=1):
|
def searchForThreads(self, name, limit=10):
|
||||||
"""
|
"""
|
||||||
Find and get a thread by its name
|
Find and get a thread by its name
|
||||||
|
|
||||||
@@ -927,14 +927,14 @@ class Client(object):
|
|||||||
:type image_id: str
|
:type image_id: str
|
||||||
:return: An url where you can download the original image
|
:return: An url where you can download the original image
|
||||||
:rtype: str
|
:rtype: str
|
||||||
:raises: FBChatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
image_id = str(image_id)
|
image_id = str(image_id)
|
||||||
j = check_request(self._get(ReqUrl.ATTACHMENT_PHOTO, query={'photo_id': str(image_id)}))
|
j = check_request(self._get(ReqUrl.ATTACHMENT_PHOTO, query={'photo_id': str(image_id)}))
|
||||||
|
|
||||||
url = get_jsmods_require(j, 3)
|
url = get_jsmods_require(j, 3)
|
||||||
if url is None:
|
if url is None:
|
||||||
raise FBChatException('Could not fetch image url from: {}'.format(j))
|
raise FBchatException('Could not fetch image url from: {}'.format(j))
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def fetchMessageInfo(self, mid, thread_id=None):
|
def fetchMessageInfo(self, mid, thread_id=None):
|
||||||
@@ -945,7 +945,7 @@ class Client(object):
|
|||||||
:param thread_id: User/Group ID to get message info from. See :ref:`intro_threads`
|
:param thread_id: User/Group ID to get message info from. See :ref:`intro_threads`
|
||||||
:return: :class:`models.Message` object
|
:return: :class:`models.Message` object
|
||||||
:rtype: models.Message
|
:rtype: models.Message
|
||||||
:raises: FBChatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
thread_id, thread_type = self._getThread(thread_id, None)
|
thread_id, thread_type = self._getThread(thread_id, None)
|
||||||
message_info = self._forcedFetch(thread_id, mid).get("message")
|
message_info = self._forcedFetch(thread_id, mid).get("message")
|
||||||
@@ -958,7 +958,7 @@ class Client(object):
|
|||||||
|
|
||||||
:param poll_id: Poll ID to fetch from
|
:param poll_id: Poll ID to fetch from
|
||||||
:rtype: list
|
:rtype: list
|
||||||
:raises: FBChatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {
|
||||||
"question_id": poll_id
|
"question_id": poll_id
|
||||||
@@ -975,7 +975,7 @@ class Client(object):
|
|||||||
:param plan_id: Plan ID to fetch from
|
:param plan_id: Plan ID to fetch from
|
||||||
:return: :class:`models.Plan` object
|
:return: :class:`models.Plan` object
|
||||||
:rtype: models.Plan
|
:rtype: models.Plan
|
||||||
:raises: FBChatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {
|
||||||
"event_reminder_id": plan_id
|
"event_reminder_id": plan_id
|
||||||
@@ -1111,7 +1111,56 @@ class Client(object):
|
|||||||
data['specific_to_list[0]'] = "fbid:{}".format(thread_id)
|
data['specific_to_list[0]'] = "fbid:{}".format(thread_id)
|
||||||
return self._doSendRequest(data)
|
return self._doSendRequest(data)
|
||||||
|
|
||||||
def _upload(self, files):
|
def unsend(self, mid):
|
||||||
|
"""
|
||||||
|
Unsends a message (removes for everyone)
|
||||||
|
|
||||||
|
:param mid: :ref:`Message ID <intro_message_ids>` of the message to unsend
|
||||||
|
"""
|
||||||
|
data = {
|
||||||
|
'message_id': mid,
|
||||||
|
}
|
||||||
|
r = self._post(self.req_url.UNSEND, data)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def _sendLocation(self, location, current=True, thread_id=None, thread_type=None):
|
||||||
|
thread_id, thread_type = self._getThread(thread_id, thread_type)
|
||||||
|
data = self._getSendData(thread_id=thread_id, thread_type=thread_type)
|
||||||
|
data['action_type'] = 'ma-type:user-generated-message'
|
||||||
|
data['location_attachment[coordinates][latitude]'] = location.latitude
|
||||||
|
data['location_attachment[coordinates][longitude]'] = location.longitude
|
||||||
|
data['location_attachment[is_current_location]'] = current
|
||||||
|
return self._doSendRequest(data)
|
||||||
|
|
||||||
|
def sendLocation(self, location, thread_id=None, thread_type=None):
|
||||||
|
"""
|
||||||
|
Sends a given location to a thread as the user's current location
|
||||||
|
|
||||||
|
:param location: Location to send
|
||||||
|
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: See :ref:`intro_threads`
|
||||||
|
:type location: models.LocationAttachment
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
:return: :ref:`Message ID <intro_message_ids>` of the sent message
|
||||||
|
:raises: FBchatException if request failed
|
||||||
|
"""
|
||||||
|
self._sendLocation(location=location, current=True, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
|
def sendPinnedLocation(self, location, thread_id=None, thread_type=None):
|
||||||
|
"""
|
||||||
|
Sends a given location to a thread as a pinned location
|
||||||
|
|
||||||
|
:param location: Location to send
|
||||||
|
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: See :ref:`intro_threads`
|
||||||
|
:type location: models.LocationAttachment
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
:return: :ref:`Message ID <intro_message_ids>` of the sent message
|
||||||
|
:raises: FBchatException if request failed
|
||||||
|
"""
|
||||||
|
self._sendLocation(location=location, current=False, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
|
def _upload(self, files, voice_clip=False):
|
||||||
"""
|
"""
|
||||||
Uploads files to Facebook
|
Uploads files to Facebook
|
||||||
|
|
||||||
@@ -1121,7 +1170,12 @@ class Client(object):
|
|||||||
Returns a list of tuples with a file's ID and mimetype
|
Returns a list of tuples with a file's ID and mimetype
|
||||||
"""
|
"""
|
||||||
file_dict = {'upload_{}'.format(i): f for i, f in enumerate(files)}
|
file_dict = {'upload_{}'.format(i): f for i, f in enumerate(files)}
|
||||||
j = self._postFile(self.req_url.UPLOAD, files=file_dict, fix_request=True, as_json=True)
|
|
||||||
|
data = {
|
||||||
|
"voice_clip": voice_clip,
|
||||||
|
}
|
||||||
|
|
||||||
|
j = self._postFile(self.req_url.UPLOAD, files=file_dict, query=data, fix_request=True, as_json=True)
|
||||||
|
|
||||||
if len(j['payload']['metadata']) != len(files):
|
if len(j['payload']['metadata']) != len(files):
|
||||||
raise FBchatException("Some files could not be uploaded: {}, {}".format(j, files))
|
raise FBchatException("Some files could not be uploaded: {}, {}".format(j, files))
|
||||||
@@ -1165,7 +1219,7 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
Sends local files to a thread
|
Sends local files to a thread
|
||||||
|
|
||||||
:param file_path: Paths of files to upload and send
|
:param file_paths: Paths of files to upload and send
|
||||||
:param message: Additional message
|
:param message: Additional message
|
||||||
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
||||||
:param thread_type: See :ref:`intro_threads`
|
:param thread_type: See :ref:`intro_threads`
|
||||||
@@ -1178,6 +1232,39 @@ class Client(object):
|
|||||||
files = self._upload(x)
|
files = self._upload(x)
|
||||||
return self._sendFiles(files=files, message=message, thread_id=thread_id, thread_type=thread_type)
|
return self._sendFiles(files=files, message=message, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
|
def sendRemoteVoiceClips(self, clip_urls, message=None, thread_id=None, thread_type=ThreadType.USER):
|
||||||
|
"""
|
||||||
|
Sends voice clips from URLs to a thread
|
||||||
|
|
||||||
|
:param clip_urls: URLs of clips to upload and send
|
||||||
|
:param message: Additional message
|
||||||
|
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: See :ref:`intro_threads`
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
:return: :ref:`Message ID <intro_message_ids>` of the sent files
|
||||||
|
:raises: FBchatException if request failed
|
||||||
|
"""
|
||||||
|
clip_urls = require_list(clip_urls)
|
||||||
|
files = self._upload(get_files_from_urls(clip_urls), voice_clip=True)
|
||||||
|
return self._sendFiles(files=files, message=message, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
|
def sendLocalVoiceClips(self, clip_paths, message=None, thread_id=None, thread_type=ThreadType.USER):
|
||||||
|
"""
|
||||||
|
Sends local voice clips to a thread
|
||||||
|
|
||||||
|
:param clip_paths: Paths of clips to upload and send
|
||||||
|
:param message: Additional message
|
||||||
|
:param thread_id: User/Group ID to send to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: See :ref:`intro_threads`
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
:return: :ref:`Message ID <intro_message_ids>` of the sent files
|
||||||
|
:raises: FBchatException if request failed
|
||||||
|
"""
|
||||||
|
clip_paths = require_list(clip_paths)
|
||||||
|
with get_files_from_paths(clip_paths) as x:
|
||||||
|
files = self._upload(x, voice_clip=True)
|
||||||
|
return self._sendFiles(files=files, message=message, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
def sendImage(self, image_id, message=None, thread_id=None, thread_type=ThreadType.USER, is_gif=False):
|
def sendImage(self, image_id, message=None, thread_id=None, thread_type=ThreadType.USER, is_gif=False):
|
||||||
"""
|
"""
|
||||||
Deprecated. Use :func:`fbchat.Client._sendFiles` instead
|
Deprecated. Use :func:`fbchat.Client._sendFiles` instead
|
||||||
@@ -1482,27 +1569,26 @@ class Client(object):
|
|||||||
|
|
||||||
def reactToMessage(self, message_id, reaction):
|
def reactToMessage(self, message_id, reaction):
|
||||||
"""
|
"""
|
||||||
Reacts to a message
|
Reacts to a message, or removes reaction
|
||||||
|
|
||||||
:param message_id: :ref:`Message ID <intro_message_ids>` to react to
|
:param message_id: :ref:`Message ID <intro_message_ids>` to react to
|
||||||
:param reaction: Reaction emoji to use
|
:param reaction: Reaction emoji to use, if None removes reaction
|
||||||
:type reaction: models.MessageReaction
|
:type reaction: models.MessageReaction or None
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
full_data = {
|
data = {
|
||||||
"doc_id": 1491398900900362,
|
"doc_id": 1491398900900362,
|
||||||
"variables": json.dumps({
|
"variables": json.dumps({
|
||||||
"data": {
|
"data": {
|
||||||
"action": "ADD_REACTION",
|
"action": "ADD_REACTION" if reaction else "REMOVE_REACTION",
|
||||||
"client_mutation_id": "1",
|
"client_mutation_id": "1",
|
||||||
"actor_id": self.uid,
|
"actor_id": self.uid,
|
||||||
"message_id": str(message_id),
|
"message_id": str(message_id),
|
||||||
"reaction": reaction.value
|
"reaction": reaction.value if reaction else None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
self._post(self.req_url.MESSAGE_REACTION, data, fix_request=True, as_json=True)
|
||||||
j = self._post(self.req_url.MESSAGE_REACTION, full_data, fix_request=True, as_json=True)
|
|
||||||
|
|
||||||
def createPlan(self, plan, thread_id=None):
|
def createPlan(self, plan, thread_id=None):
|
||||||
"""
|
"""
|
||||||
@@ -2019,6 +2105,7 @@ class Client(object):
|
|||||||
|
|
||||||
delta = m["delta"]
|
delta = m["delta"]
|
||||||
delta_type = delta.get("type")
|
delta_type = delta.get("type")
|
||||||
|
delta_class = delta.get("class")
|
||||||
metadata = delta.get("messageMetadata")
|
metadata = delta.get("messageMetadata")
|
||||||
|
|
||||||
if metadata:
|
if metadata:
|
||||||
@@ -2055,14 +2142,14 @@ class Client(object):
|
|||||||
thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
|
thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
|
||||||
|
|
||||||
# Thread title change
|
# Thread title change
|
||||||
elif delta.get("class") == "ThreadName":
|
elif delta_class == "ThreadName":
|
||||||
new_title = delta["name"]
|
new_title = delta["name"]
|
||||||
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)
|
||||||
|
|
||||||
# Forced fetch
|
# Forced fetch
|
||||||
elif delta.get("class") == "ForcedFetch":
|
elif delta_class == "ForcedFetch":
|
||||||
mid = delta.get("messageId")
|
mid = delta.get("messageId")
|
||||||
if mid is None:
|
if mid is None:
|
||||||
self.onUnknownMesssageType(msg=m)
|
self.onUnknownMesssageType(msg=m)
|
||||||
@@ -2108,7 +2195,7 @@ class Client(object):
|
|||||||
thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m)
|
thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
|
||||||
# Message delivered
|
# Message delivered
|
||||||
elif delta.get("class") == "DeliveryReceipt":
|
elif delta_class == "DeliveryReceipt":
|
||||||
message_ids = delta["messageIds"]
|
message_ids = delta["messageIds"]
|
||||||
delivered_for = str(delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"])
|
delivered_for = str(delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"])
|
||||||
ts = int(delta["deliveredWatermarkTimestampMs"])
|
ts = int(delta["deliveredWatermarkTimestampMs"])
|
||||||
@@ -2118,7 +2205,7 @@ class Client(object):
|
|||||||
metadata=metadata, msg=m)
|
metadata=metadata, msg=m)
|
||||||
|
|
||||||
# Message seen
|
# Message seen
|
||||||
elif delta.get("class") == "ReadReceipt":
|
elif delta_class == "ReadReceipt":
|
||||||
seen_by = str(delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"])
|
seen_by = str(delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"])
|
||||||
seen_ts = int(delta["actionTimestampMs"])
|
seen_ts = int(delta["actionTimestampMs"])
|
||||||
delivered_ts = int(delta["watermarkTimestampMs"])
|
delivered_ts = int(delta["watermarkTimestampMs"])
|
||||||
@@ -2127,7 +2214,7 @@ class Client(object):
|
|||||||
seen_ts=seen_ts, ts=delivered_ts, metadata=metadata, msg=m)
|
seen_ts=seen_ts, ts=delivered_ts, metadata=metadata, msg=m)
|
||||||
|
|
||||||
# Messages marked as seen
|
# Messages marked as seen
|
||||||
elif delta.get("class") == "MarkRead":
|
elif delta_class == "MarkRead":
|
||||||
seen_ts = int(delta.get("actionTimestampMs") or delta.get("actionTimestamp"))
|
seen_ts = int(delta.get("actionTimestampMs") or delta.get("actionTimestamp"))
|
||||||
delivered_ts = int(delta.get("watermarkTimestampMs") or delta.get("watermarkTimestamp"))
|
delivered_ts = int(delta.get("watermarkTimestampMs") or delta.get("watermarkTimestamp"))
|
||||||
|
|
||||||
@@ -2227,6 +2314,61 @@ class Client(object):
|
|||||||
self.onPlanParticipation(mid=mid, plan=plan, take_part=take_part, author_id=author_id,
|
self.onPlanParticipation(mid=mid, plan=plan, take_part=take_part, author_id=author_id,
|
||||||
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)
|
||||||
|
|
||||||
|
# Client payload (that weird numbers)
|
||||||
|
elif delta_class == "ClientPayload":
|
||||||
|
payload = json.loads("".join(chr(z) for z in delta['payload']))
|
||||||
|
ts = m.get("ofd_ts")
|
||||||
|
for d in payload.get('deltas', []):
|
||||||
|
|
||||||
|
# Message reaction
|
||||||
|
if d.get('deltaMessageReaction'):
|
||||||
|
i = d['deltaMessageReaction']
|
||||||
|
thread_id, thread_type = getThreadIdAndThreadType(i)
|
||||||
|
mid = i["messageId"]
|
||||||
|
author_id = str(i["userId"])
|
||||||
|
reaction = MessageReaction(i["reaction"]) if i.get("reaction") else None
|
||||||
|
add_reaction = not bool(i["action"])
|
||||||
|
if add_reaction:
|
||||||
|
self.onReactionAdded(mid=mid, reaction=reaction, author_id=author_id,
|
||||||
|
thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
else:
|
||||||
|
self.onReactionRemoved(mid=mid, author_id=author_id, thread_id=thread_id,
|
||||||
|
thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
|
||||||
|
# Viewer status change
|
||||||
|
elif d.get('deltaChangeViewerStatus'):
|
||||||
|
i = d['deltaChangeViewerStatus']
|
||||||
|
thread_id, thread_type = getThreadIdAndThreadType(i)
|
||||||
|
author_id = str(i["actorFbid"])
|
||||||
|
reason = i["reason"]
|
||||||
|
can_reply = i["canViewerReply"]
|
||||||
|
if reason == 2:
|
||||||
|
if can_reply:
|
||||||
|
self.onUnblock(author_id=author_id, thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
else:
|
||||||
|
self.onBlock(author_id=author_id, thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
|
||||||
|
# Live location info
|
||||||
|
elif d.get('liveLocationData'):
|
||||||
|
i = d['liveLocationData']
|
||||||
|
thread_id, thread_type = getThreadIdAndThreadType(i)
|
||||||
|
for l in i['messageLiveLocations']:
|
||||||
|
mid = l["messageId"]
|
||||||
|
author_id = str(l["senderId"])
|
||||||
|
location = graphql_to_live_location(l)
|
||||||
|
self.onLiveLocation(mid=mid, location=location, author_id=author_id, thread_id=thread_id,
|
||||||
|
thread_type=thread_type, ts=ts, msg=m)
|
||||||
|
|
||||||
|
# Message deletion
|
||||||
|
elif d.get('deltaRecallMessageData'):
|
||||||
|
i = d['deltaRecallMessageData']
|
||||||
|
thread_id, thread_type = getThreadIdAndThreadType(i)
|
||||||
|
mid = i['messageID']
|
||||||
|
ts = i['deletionTimestamp']
|
||||||
|
author_id = str(i['senderID'])
|
||||||
|
self.onMessageUnsent(mid=mid, author_id=author_id, thread_id=thread_id, thread_type=thread_type,
|
||||||
|
ts=ts, msg=m)
|
||||||
|
|
||||||
# New message
|
# New message
|
||||||
elif delta.get("class") == "NewMessage":
|
elif delta.get("class") == "NewMessage":
|
||||||
mentions = []
|
mentions = []
|
||||||
@@ -2238,6 +2380,7 @@ class Client(object):
|
|||||||
|
|
||||||
sticker = None
|
sticker = None
|
||||||
attachments = []
|
attachments = []
|
||||||
|
unsent = False
|
||||||
if delta.get('attachments'):
|
if delta.get('attachments'):
|
||||||
try:
|
try:
|
||||||
for a in delta['attachments']:
|
for a in delta['attachments']:
|
||||||
@@ -2245,17 +2388,23 @@ class Client(object):
|
|||||||
if mercury.get('blob_attachment'):
|
if mercury.get('blob_attachment'):
|
||||||
image_metadata = a.get('imageMetadata', {})
|
image_metadata = a.get('imageMetadata', {})
|
||||||
attach_type = mercury['blob_attachment']['__typename']
|
attach_type = mercury['blob_attachment']['__typename']
|
||||||
attachment = graphql_to_attachment(mercury.get('blob_attachment', {}))
|
attachment = graphql_to_attachment(mercury['blob_attachment'])
|
||||||
|
|
||||||
if attach_type == ['MessageFile', 'MessageVideo', 'MessageAudio']:
|
if attach_type in ['MessageFile', 'MessageVideo', 'MessageAudio']:
|
||||||
# TODO: Add more data here for audio files
|
# TODO: Add more data here for audio files
|
||||||
attachment.size = int(a['fileSize'])
|
attachment.size = int(a['fileSize'])
|
||||||
attachments.append(attachment)
|
attachments.append(attachment)
|
||||||
|
|
||||||
elif mercury.get('sticker_attachment'):
|
elif mercury.get('sticker_attachment'):
|
||||||
sticker = graphql_to_sticker(a['mercury']['sticker_attachment'])
|
sticker = graphql_to_sticker(mercury['sticker_attachment'])
|
||||||
|
|
||||||
elif mercury.get('extensible_attachment'):
|
elif mercury.get('extensible_attachment'):
|
||||||
# TODO: Add more data here for shared stuff (URLs, events and so on)
|
attachment = graphql_to_extensible_attachment(mercury['extensible_attachment'])
|
||||||
pass
|
if isinstance(attachment, UnsentMessage):
|
||||||
|
unsent = True
|
||||||
|
elif attachment:
|
||||||
|
attachments.append(attachment)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception('An exception occured while reading attachments: {}'.format(delta['attachments']))
|
log.exception('An exception occured while reading attachments: {}'.format(delta['attachments']))
|
||||||
|
|
||||||
@@ -2267,12 +2416,13 @@ class Client(object):
|
|||||||
mentions=mentions,
|
mentions=mentions,
|
||||||
emoji_size=emoji_size,
|
emoji_size=emoji_size,
|
||||||
sticker=sticker,
|
sticker=sticker,
|
||||||
attachments=attachments
|
attachments=attachments,
|
||||||
)
|
)
|
||||||
message.uid = mid
|
message.uid = mid
|
||||||
message.author = author_id
|
message.author = author_id
|
||||||
message.timestamp = ts
|
message.timestamp = ts
|
||||||
#message.reactions = {}
|
#message.reactions = {}
|
||||||
|
message.unsent = unsent
|
||||||
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
||||||
self.onMessage(mid=mid, author_id=author_id, message=delta.get('body', ''), message_object=message,
|
self.onMessage(mid=mid, author_id=author_id, message=delta.get('body', ''), message_object=message,
|
||||||
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)
|
||||||
@@ -2628,6 +2778,19 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
log.info("Marked messages as seen in threads {} at {}s".format([(x[0], x[1].name) for x in threads], seen_ts/1000))
|
log.info("Marked messages as seen in threads {} at {}s".format([(x[0], x[1].name) for x in threads], seen_ts/1000))
|
||||||
|
|
||||||
|
def onMessageUnsent(self, mid=None, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening, and someone unsends (deletes for everyone) a message
|
||||||
|
|
||||||
|
:param mid: ID of the unsent message
|
||||||
|
:param author_id: The ID of the person who unsent the message
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} unsent the message {} in {} ({}) at {}s".format(author_id, repr(mid), thread_id, thread_type.name, ts/1000))
|
||||||
|
|
||||||
def onPeopleAdded(self, mid=None, added_ids=None, author_id=None, thread_id=None, ts=None, msg=None):
|
def onPeopleAdded(self, mid=None, added_ids=None, author_id=None, thread_id=None, ts=None, msg=None):
|
||||||
"""
|
"""
|
||||||
@@ -2709,6 +2872,79 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
log.info("{} played \"{}\" in {} ({})".format(author_id, game_name, thread_id, thread_type.name))
|
log.info("{} played \"{}\" in {} ({})".format(author_id, game_name, thread_id, thread_type.name))
|
||||||
|
|
||||||
|
def onReactionAdded(self, mid=None, reaction=None, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening, and somebody reacts to a message
|
||||||
|
|
||||||
|
:param mid: Message ID, that user reacted to
|
||||||
|
:param reaction: Reaction
|
||||||
|
:param add_reaction: Whether user added or removed reaction
|
||||||
|
:param author_id: The ID of the person who reacted to the message
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type reaction: models.MessageReaction
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} reacted to message {} with {} in {} ({})".format(author_id, mid, reaction.name, thread_id, thread_type.name))
|
||||||
|
|
||||||
|
def onReactionRemoved(self, mid=None, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening, and somebody removes reaction from a message
|
||||||
|
|
||||||
|
:param mid: Message ID, that user reacted to
|
||||||
|
:param author_id: The ID of the person who removed reaction
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} removed reaction from {} message in {} ({})".format(author_id, mid, thread_id, thread_type))
|
||||||
|
|
||||||
|
def onBlock(self, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening, and somebody blocks client
|
||||||
|
|
||||||
|
:param author_id: The ID of the person who blocked
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} blocked {} ({}) thread".format(author_id, thread_id, thread_type.name))
|
||||||
|
|
||||||
|
def onUnblock(self, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening, and somebody blocks client
|
||||||
|
|
||||||
|
:param author_id: The ID of the person who unblocked
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} unblocked {} ({}) thread".format(author_id, thread_id, thread_type.name))
|
||||||
|
|
||||||
|
def onLiveLocation(self, mid=None, location=None, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None):
|
||||||
|
"""
|
||||||
|
Called when the client is listening and somebody sends live location info
|
||||||
|
|
||||||
|
:param mid: The action ID
|
||||||
|
:param location: Sent location info
|
||||||
|
:param author_id: The ID of the person who sent location info
|
||||||
|
:param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads`
|
||||||
|
:param ts: A timestamp of the action
|
||||||
|
:param msg: A full set of the data recieved
|
||||||
|
:type location: models.LiveLocationAttachment
|
||||||
|
:type thread_type: models.ThreadType
|
||||||
|
"""
|
||||||
|
log.info("{} sent live location info in {} ({}) with latitude {} and longitude {}".format(author_id, thread_id, thread_type, location.latitude, location.longitude))
|
||||||
|
|
||||||
def onQprimer(self, ts=None, msg=None):
|
def onQprimer(self, ts=None, msg=None):
|
||||||
"""
|
"""
|
||||||
Called when the client just started listening
|
Called when the client just started listening
|
||||||
@@ -2914,6 +3150,7 @@ class Client(object):
|
|||||||
:param metadata: Extra metadata about the action
|
:param metadata: Extra metadata about the action
|
||||||
:param msg: A full set of the data recieved
|
:param msg: A full set of the data recieved
|
||||||
:type plan: models.Plan
|
:type plan: models.Plan
|
||||||
|
:type take_part: bool
|
||||||
:type thread_type: models.ThreadType
|
:type thread_type: models.ThreadType
|
||||||
"""
|
"""
|
||||||
if take_part:
|
if take_part:
|
||||||
|
@@ -26,12 +26,11 @@ class ConcatJSONDecoder(json.JSONDecoder):
|
|||||||
def graphql_color_to_enum(color):
|
def graphql_color_to_enum(color):
|
||||||
if color is None:
|
if color is None:
|
||||||
return None
|
return None
|
||||||
if len(color) == 0:
|
if not color:
|
||||||
return ThreadColor.MESSENGER_BLUE
|
return ThreadColor.MESSENGER_BLUE
|
||||||
try:
|
color = color[2:] # Strip the alpha value
|
||||||
return ThreadColor('#{}'.format(color[2:].lower()))
|
color_value = '#{}'.format(color.lower())
|
||||||
except ValueError:
|
return enum_extend_if_invalid(ThreadColor, color_value)
|
||||||
raise FBchatException('Could not get ThreadColor from color: {}'.format(color))
|
|
||||||
|
|
||||||
def get_customization_info(thread):
|
def get_customization_info(thread):
|
||||||
if thread is None or thread.get('customization_info') is None:
|
if thread is None or thread.get('customization_info') is None:
|
||||||
@@ -128,9 +127,84 @@ def graphql_to_attachment(a):
|
|||||||
uid=a.get('legacy_attachment_id')
|
uid=a.get('legacy_attachment_id')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def graphql_to_extensible_attachment(a):
|
||||||
|
story = a.get('story_attachment')
|
||||||
|
if story:
|
||||||
|
target = story.get('target')
|
||||||
|
if target:
|
||||||
|
_type = target['__typename']
|
||||||
|
if _type == 'MessageLocation':
|
||||||
|
latitude, longitude = get_url_parameter(get_url_parameter(story['url'], 'u'), 'where1').split(", ")
|
||||||
|
rtn = LocationAttachment(
|
||||||
|
uid=int(story['deduplication_key']),
|
||||||
|
latitude=float(latitude),
|
||||||
|
longitude=float(longitude),
|
||||||
|
)
|
||||||
|
if story['media']:
|
||||||
|
rtn.image_url = story['media']['image']['uri']
|
||||||
|
rtn.image_width = story['media']['image']['width']
|
||||||
|
rtn.image_height = story['media']['image']['height']
|
||||||
|
rtn.url = story['url']
|
||||||
|
return rtn
|
||||||
|
elif _type == 'MessageLiveLocation':
|
||||||
|
rtn = LiveLocationAttachment(
|
||||||
|
uid=int(story['target']['live_location_id']),
|
||||||
|
latitude=story['target']['coordinate']['latitude'] if story['target'].get('coordinate') else None,
|
||||||
|
longitude=story['target']['coordinate']['longitude'] if story['target'].get('coordinate') else None,
|
||||||
|
name=story['title_with_entities']['text'],
|
||||||
|
expiration_time=story['target']['expiration_time'] if story['target'].get('expiration_time') else None,
|
||||||
|
is_expired=story['target']['is_expired'],
|
||||||
|
)
|
||||||
|
if story['media']:
|
||||||
|
rtn.image_url = story['media']['image']['uri']
|
||||||
|
rtn.image_width = story['media']['image']['width']
|
||||||
|
rtn.image_height = story['media']['image']['height']
|
||||||
|
rtn.url = story['url']
|
||||||
|
return rtn
|
||||||
|
elif _type in ['ExternalUrl', 'Story']:
|
||||||
|
return ShareAttachment(
|
||||||
|
uid=a.get('legacy_attachment_id'),
|
||||||
|
author=story['target']['actors'][0]['id'] if story['target'].get('actors') else None,
|
||||||
|
url=story['url'],
|
||||||
|
original_url=get_url_parameter(story['url'], 'u') if "/l.php?u=" in story['url'] else story['url'],
|
||||||
|
title=story['title_with_entities'].get('text'),
|
||||||
|
description=story['description'].get('text'),
|
||||||
|
source=story['source']['text'],
|
||||||
|
image_url=story['media']['image']['uri'] if story.get('media') else None,
|
||||||
|
original_image_url=(get_url_parameter(story['media']['image']['uri'], 'url') if "/safe_image.php" in story['media']['image']['uri'] else story['media']['image']['uri']) if story.get('media') else None,
|
||||||
|
image_width=story['media']['image']['width'] if story.get('media') else None,
|
||||||
|
image_height=story['media']['image']['height'] if story.get('media') else None,
|
||||||
|
attachments=[graphql_to_subattachment(attachment) for attachment in story.get('subattachments')],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return UnsentMessage(
|
||||||
|
uid=a.get('legacy_attachment_id'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_to_subattachment(a):
|
||||||
|
_type = a['target']['__typename']
|
||||||
|
if _type == 'Video':
|
||||||
|
return VideoAttachment(
|
||||||
|
duration=a['media'].get('playable_duration_in_ms'),
|
||||||
|
preview_url=a['media'].get('playable_url'),
|
||||||
|
medium_image=a['media'].get('image'),
|
||||||
|
uid=a['target'].get('video_id'),
|
||||||
|
)
|
||||||
|
|
||||||
|
def graphql_to_live_location(a):
|
||||||
|
return LiveLocationAttachment(
|
||||||
|
uid=a['id'],
|
||||||
|
latitude=a['coordinate']['latitude'] / (10 ** 8) if not a.get('stopReason') else None,
|
||||||
|
longitude=a['coordinate']['longitude'] / (10 ** 8) if not a.get('stopReason') else None,
|
||||||
|
name=a.get('locationTitle'),
|
||||||
|
expiration_time=a['expirationTime'],
|
||||||
|
is_expired=bool(a.get('stopReason')),
|
||||||
|
)
|
||||||
|
|
||||||
def graphql_to_poll(a):
|
def graphql_to_poll(a):
|
||||||
rtn = Poll(
|
rtn = Poll(
|
||||||
title=a.get('title') if a.get('title') else a.get("text"),
|
title=a.get('title') if a.get('title') else a.get('text'),
|
||||||
options=[graphql_to_poll_option(m) for m in a.get('options')]
|
options=[graphql_to_poll_option(m) for m in a.get('options')]
|
||||||
)
|
)
|
||||||
rtn.uid = int(a["id"])
|
rtn.uid = int(a["id"])
|
||||||
@@ -207,13 +281,21 @@ def graphql_to_message(message):
|
|||||||
rtn.uid = str(message.get('message_id'))
|
rtn.uid = str(message.get('message_id'))
|
||||||
rtn.author = str(message.get('message_sender').get('id'))
|
rtn.author = str(message.get('message_sender').get('id'))
|
||||||
rtn.timestamp = message.get('timestamp_precise')
|
rtn.timestamp = message.get('timestamp_precise')
|
||||||
|
rtn.unsent = False
|
||||||
if message.get('unread') is not None:
|
if message.get('unread') is not None:
|
||||||
rtn.is_read = not message['unread']
|
rtn.is_read = not message['unread']
|
||||||
rtn.reactions = {str(r['user']['id']):MessageReaction(r['reaction']) for r in message.get('message_reactions')}
|
rtn.reactions = {
|
||||||
|
str(r['user']['id']): enum_extend_if_invalid(MessageReaction, r['reaction'])
|
||||||
|
for r in message.get('message_reactions')
|
||||||
|
}
|
||||||
if message.get('blob_attachments') is not None:
|
if message.get('blob_attachments') is not None:
|
||||||
rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']]
|
rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']]
|
||||||
# TODO: This is still missing parsing:
|
if message.get('extensible_attachment') is not None:
|
||||||
# message.get('extensible_attachment')
|
attachment = graphql_to_extensible_attachment(message['extensible_attachment'])
|
||||||
|
if isinstance(attachment, UnsentMessage):
|
||||||
|
rtn.unsent = True
|
||||||
|
elif attachment:
|
||||||
|
rtn.attachments.append(attachment)
|
||||||
return rtn
|
return rtn
|
||||||
|
|
||||||
def graphql_to_user(user):
|
def graphql_to_user(user):
|
||||||
@@ -477,7 +559,7 @@ class GraphQL(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
SEARCH_USER = """
|
SEARCH_USER = """
|
||||||
Query SearchUser(<search> = '', <limit> = 1) {
|
Query SearchUser(<search> = '', <limit> = 10) {
|
||||||
entities_named(<search>) {
|
entities_named(<search>) {
|
||||||
search_results.of_type(user).first(<limit>) as users {
|
search_results.of_type(user).first(<limit>) as users {
|
||||||
nodes {
|
nodes {
|
||||||
@@ -489,7 +571,7 @@ class GraphQL(object):
|
|||||||
""" + FRAGMENT_USER
|
""" + FRAGMENT_USER
|
||||||
|
|
||||||
SEARCH_GROUP = """
|
SEARCH_GROUP = """
|
||||||
Query SearchGroup(<search> = '', <limit> = 1, <pic_size> = 32) {
|
Query SearchGroup(<search> = '', <limit> = 10, <pic_size> = 32) {
|
||||||
viewer() {
|
viewer() {
|
||||||
message_threads.with_thread_name(<search>).last(<limit>) as groups {
|
message_threads.with_thread_name(<search>).last(<limit>) as groups {
|
||||||
nodes {
|
nodes {
|
||||||
@@ -501,7 +583,7 @@ class GraphQL(object):
|
|||||||
""" + FRAGMENT_GROUP
|
""" + FRAGMENT_GROUP
|
||||||
|
|
||||||
SEARCH_PAGE = """
|
SEARCH_PAGE = """
|
||||||
Query SearchPage(<search> = '', <limit> = 1) {
|
Query SearchPage(<search> = '', <limit> = 10) {
|
||||||
entities_named(<search>) {
|
entities_named(<search>) {
|
||||||
search_results.of_type(page).first(<limit>) as pages {
|
search_results.of_type(page).first(<limit>) as pages {
|
||||||
nodes {
|
nodes {
|
||||||
@@ -513,7 +595,7 @@ class GraphQL(object):
|
|||||||
""" + FRAGMENT_PAGE
|
""" + FRAGMENT_PAGE
|
||||||
|
|
||||||
SEARCH_THREAD = """
|
SEARCH_THREAD = """
|
||||||
Query SearchThread(<search> = '', <limit> = 1) {
|
Query SearchThread(<search> = '', <limit> = 10) {
|
||||||
entities_named(<search>) {
|
entities_named(<search>) {
|
||||||
search_results.first(<limit>) as threads {
|
search_results.first(<limit>) as threads {
|
||||||
nodes {
|
nodes {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import enum
|
import aenum
|
||||||
|
|
||||||
|
|
||||||
class FBchatException(Exception):
|
class FBchatException(Exception):
|
||||||
@@ -190,6 +190,8 @@ class Message(object):
|
|||||||
sticker = None
|
sticker = None
|
||||||
#: A list of attachments
|
#: A list of attachments
|
||||||
attachments = None
|
attachments = None
|
||||||
|
#: Whether the message is unsent (deleted for everyone)
|
||||||
|
unsent = None
|
||||||
|
|
||||||
def __init__(self, text=None, mentions=None, emoji_size=None, sticker=None, attachments=None):
|
def __init__(self, text=None, mentions=None, emoji_size=None, sticker=None, attachments=None):
|
||||||
"""Represents a Facebook message"""
|
"""Represents a Facebook message"""
|
||||||
@@ -204,6 +206,7 @@ class Message(object):
|
|||||||
self.attachments = attachments
|
self.attachments = attachments
|
||||||
self.reactions = {}
|
self.reactions = {}
|
||||||
self.read_by = []
|
self.read_by = []
|
||||||
|
self.deleted = False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__unicode__()
|
return self.__unicode__()
|
||||||
@@ -219,6 +222,12 @@ class Attachment(object):
|
|||||||
"""Represents a Facebook attachment"""
|
"""Represents a Facebook attachment"""
|
||||||
self.uid = uid
|
self.uid = uid
|
||||||
|
|
||||||
|
class UnsentMessage(Attachment):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
"""Represents an unsent message attachment"""
|
||||||
|
super(UnsentMessage, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
class Sticker(Attachment):
|
class Sticker(Attachment):
|
||||||
#: The sticker-pack's ID
|
#: The sticker-pack's ID
|
||||||
pack = None
|
pack = None
|
||||||
@@ -251,9 +260,79 @@ class Sticker(Attachment):
|
|||||||
super(Sticker, self).__init__(*args, **kwargs)
|
super(Sticker, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
class ShareAttachment(Attachment):
|
class ShareAttachment(Attachment):
|
||||||
def __init__(self, **kwargs):
|
#: ID of the author of the shared post
|
||||||
"""Represents a shared item (eg. URL) that has been sent as a Facebook attachment - *Currently Incomplete!*"""
|
author = None
|
||||||
|
#: Target URL
|
||||||
|
url = None
|
||||||
|
#: Original URL if Facebook redirects the URL
|
||||||
|
original_url = None
|
||||||
|
#: Title of the attachment
|
||||||
|
title = None
|
||||||
|
#: Description of the attachment
|
||||||
|
description = None
|
||||||
|
#: Name of the source
|
||||||
|
source = None
|
||||||
|
#: URL of the attachment image
|
||||||
|
image_url = None
|
||||||
|
#: URL of the original image if Facebook uses `safe_image`
|
||||||
|
original_image_url = None
|
||||||
|
#: Width of the image
|
||||||
|
image_width = None
|
||||||
|
#: Height of the image
|
||||||
|
image_height = None
|
||||||
|
#: List of additional attachments
|
||||||
|
attachments = None
|
||||||
|
|
||||||
|
def __init__(self, author=None, url=None, original_url=None, title=None, description=None, source=None, image_url=None, original_image_url=None, image_width=None, image_height=None, attachments=None, **kwargs):
|
||||||
|
"""Represents a shared item (eg. URL) that has been sent as a Facebook attachment"""
|
||||||
super(ShareAttachment, self).__init__(**kwargs)
|
super(ShareAttachment, self).__init__(**kwargs)
|
||||||
|
self.author = author
|
||||||
|
self.url = url
|
||||||
|
self.original_url = original_url
|
||||||
|
self.title = title
|
||||||
|
self.description = description
|
||||||
|
self.source = source
|
||||||
|
self.image_url = image_url
|
||||||
|
self.original_image_url = original_image_url
|
||||||
|
self.image_width = image_width
|
||||||
|
self.image_height = image_height
|
||||||
|
if attachments is None:
|
||||||
|
attachments = []
|
||||||
|
self.attachments = attachments
|
||||||
|
|
||||||
|
class LocationAttachment(Attachment):
|
||||||
|
#: Latidute of the location
|
||||||
|
latitude = None
|
||||||
|
#: Longitude of the location
|
||||||
|
longitude = None
|
||||||
|
#: URL of image showing the map of the location
|
||||||
|
image_url = None
|
||||||
|
#: Width of the image
|
||||||
|
image_width = None
|
||||||
|
#: Height of the image
|
||||||
|
image_height = None
|
||||||
|
#: URL to Bing maps with the location
|
||||||
|
url = None
|
||||||
|
|
||||||
|
def __init__(self, latitude=None, longitude=None, **kwargs):
|
||||||
|
"""Represents a user location"""
|
||||||
|
super(LocationAttachment, self).__init__(**kwargs)
|
||||||
|
self.latitude = latitude
|
||||||
|
self.longitude = longitude
|
||||||
|
|
||||||
|
class LiveLocationAttachment(LocationAttachment):
|
||||||
|
#: Name of the location
|
||||||
|
name = None
|
||||||
|
#: Timestamp when live location expires
|
||||||
|
expiration_time = None
|
||||||
|
#: True if live location is expired
|
||||||
|
is_expired = None
|
||||||
|
|
||||||
|
def __init__(self, name=None, expiration_time=None, is_expired=None, **kwargs):
|
||||||
|
"""Represents a live user location"""
|
||||||
|
super(LiveLocationAttachment, self).__init__(**kwargs)
|
||||||
|
self.expiration_time = expiration_time
|
||||||
|
self.is_expired = is_expired
|
||||||
|
|
||||||
class FileAttachment(Attachment):
|
class FileAttachment(Attachment):
|
||||||
#: Url where you can download the file
|
#: Url where you can download the file
|
||||||
@@ -523,7 +602,7 @@ class Plan(object):
|
|||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return '<Plan ({}): {} time={}, location={}, location_id={}>'.format(self.uid, repr(self.title), self.time, repr(self.location), repr(self.location_id))
|
return '<Plan ({}): {} time={}, location={}, location_id={}>'.format(self.uid, repr(self.title), self.time, repr(self.location), repr(self.location_id))
|
||||||
|
|
||||||
class Enum(enum.Enum):
|
class Enum(aenum.Enum):
|
||||||
"""Used internally by fbchat to support enumerations"""
|
"""Used internally by fbchat to support enumerations"""
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
# For documentation:
|
# For documentation:
|
||||||
|
@@ -11,13 +11,15 @@ from os.path import basename
|
|||||||
import warnings
|
import warnings
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
import aenum
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode, parse_qs, urlparse
|
||||||
basestring = (str, bytes)
|
basestring = (str, bytes)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
|
from urlparse import parse_qs, urlparse
|
||||||
basestring = basestring
|
basestring = basestring
|
||||||
|
|
||||||
# Python 2's `input` executes the input, whereas `raw_input` just returns the input
|
# Python 2's `input` executes the input, whereas `raw_input` just returns the input
|
||||||
@@ -140,6 +142,7 @@ class ReqUrl(object):
|
|||||||
GET_POLL_OPTIONS = "https://www.facebook.com/ajax/mercury/get_poll_options"
|
GET_POLL_OPTIONS = "https://www.facebook.com/ajax/mercury/get_poll_options"
|
||||||
SEARCH_MESSAGES = "https://www.facebook.com/ajax/mercury/search_snippets.php?dpr=1"
|
SEARCH_MESSAGES = "https://www.facebook.com/ajax/mercury/search_snippets.php?dpr=1"
|
||||||
MARK_SPAM = "https://www.facebook.com/ajax/mercury/mark_spam.php?dpr=1"
|
MARK_SPAM = "https://www.facebook.com/ajax/mercury/mark_spam.php?dpr=1"
|
||||||
|
UNSEND = "https://www.facebook.com/messaging/unsend_message/?dpr=1"
|
||||||
|
|
||||||
pull_channel = 0
|
pull_channel = 0
|
||||||
|
|
||||||
@@ -297,3 +300,18 @@ def get_files_from_paths(filenames):
|
|||||||
yield files
|
yield files
|
||||||
for fn, fp, ft in files:
|
for fn, fp, ft in files:
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
|
def enum_extend_if_invalid(enumeration, value):
|
||||||
|
try:
|
||||||
|
return enumeration(value)
|
||||||
|
except ValueError:
|
||||||
|
log.warning("Failed parsing {.__name__}({!r}). Extending enum.".format(enumeration, value))
|
||||||
|
aenum.extend_enum(enumeration, "UNKNOWN_{}".format(value).upper(), value)
|
||||||
|
return enumeration(value)
|
||||||
|
|
||||||
|
def get_url_parameters(url, *args):
|
||||||
|
params = parse_qs(urlparse(url).query)
|
||||||
|
return [params[arg][0] for arg in args if params.get(arg)]
|
||||||
|
|
||||||
|
def get_url_parameter(url, param):
|
||||||
|
return get_url_parameters(url, param)[0]
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
requests
|
requests
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
enum34; python_version < '3.4'
|
aenum
|
||||||
|
@@ -43,9 +43,6 @@ include_package_data = True
|
|||||||
packages = find:
|
packages = find:
|
||||||
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4.0
|
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4.0
|
||||||
install_requires =
|
install_requires =
|
||||||
|
aenum
|
||||||
requests
|
requests
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
# May not work in pip with bdist_wheel
|
|
||||||
# See https://wheel.readthedocs.io/en/latest/#defining-conditional-dependencies
|
|
||||||
# It is therefore defined in setup.py
|
|
||||||
# enum34; python_version < '3.4'
|
|
||||||
|
Reference in New Issue
Block a user