Compare commits
80 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e1e988272b | ||
|
b159f04a6b | ||
|
d91a7ea9e3 | ||
|
8056f3399e | ||
|
fd9aa7ee90 | ||
|
53c19f473b | ||
|
78b5f05729 | ||
|
f689376830 | ||
|
d244856b41 | ||
|
3cd0f3a9a7 | ||
|
f480d68b57 | ||
|
db2bda1f9b | ||
|
f834c01921 | ||
|
f945fa80b3 | ||
|
70faa86e34 | ||
|
61502ed32a | ||
|
bfca20bb12 | ||
|
0fd86d05a1 | ||
|
c688d64062 | ||
|
2f973f129d | ||
|
9b81365b0a | ||
|
a079797fca | ||
|
6ab298f6e8 | ||
|
a159999879 | ||
|
a71835a5b8 | ||
|
86a6e07804 | ||
|
73c6be1969 | ||
|
7db7868d2b | ||
|
18ec1f5680 | ||
|
8e65074b11 | ||
|
d720438aef | ||
|
ec0e3a91d1 | ||
|
48e7203ca6 | ||
|
4f76b79629 | ||
|
1eeae78a9f | ||
|
bc27f756ed | ||
|
6302d5fb8b | ||
|
24e238c425 | ||
|
070f57fcc4 | ||
|
a4ce45e9b0 | ||
|
a3efa7702a | ||
|
d7a5d00439 | ||
|
6636d49cc0 | ||
|
8e6ee4636e | ||
|
71f19dd3c7 | ||
|
e166b472c5 | ||
|
28c867a115 | ||
|
f20a04b2a0 | ||
|
1f961b2ca7 | ||
|
e579e0c767 | ||
|
6693ec9c36 | ||
|
53856a3622 | ||
|
0b99238676 | ||
|
cb2c68e25a | ||
|
fd5553a9f5 | ||
|
60ebbd87d8 | ||
|
3a5185fcc8 | ||
|
ce469d5e5a | ||
|
4f0f126e48 | ||
|
94c30a2440 | ||
|
968223690e | ||
|
789d9d8ca1 | ||
|
2ce99a2c44 | ||
|
ee207e994f | ||
|
c374aca890 | ||
|
c28ca58537 | ||
|
0578ea2c3c | ||
|
e51ce99c1a | ||
|
3440039610 | ||
|
279f637c75 | ||
|
d940b64517 | ||
|
403870e39e | ||
|
0383d613e6 | ||
|
40e9825ee0 | ||
|
ab9ca94181 | ||
|
0f99a23af7 | ||
|
bc5163adaf | ||
|
0561718917 | ||
|
c1861627fb | ||
|
e5eccab871 |
@@ -40,7 +40,7 @@ jobs:
|
|||||||
file_glob: true
|
file_glob: true
|
||||||
file: dist/*
|
file: dist/*
|
||||||
skip_cleanup: true
|
skip_cleanup: true
|
||||||
draft: true
|
draft: false
|
||||||
on:
|
on:
|
||||||
tags: true
|
tags: true
|
||||||
|
|
||||||
|
11
docs/api.rst
11
docs/api.rst
@@ -31,14 +31,3 @@ A good tip is to write ``from fbchat.models import *`` at the start of your sour
|
|||||||
.. automodule:: fbchat.models
|
.. automodule:: fbchat.models
|
||||||
:members:
|
:members:
|
||||||
:undoc-members:
|
:undoc-members:
|
||||||
|
|
||||||
|
|
||||||
.. _api_utils:
|
|
||||||
|
|
||||||
Utils
|
|
||||||
-----
|
|
||||||
|
|
||||||
These functions and values are used internally by fbchat, and are subject to change. Do **NOT** rely on these to be backwards compatible!
|
|
||||||
|
|
||||||
.. automodule:: fbchat.utils
|
|
||||||
:members:
|
|
||||||
|
@@ -4,14 +4,16 @@
|
|||||||
:copyright: (c) 2015 - 2019 by Taehoon Kim
|
:copyright: (c) 2015 - 2019 by Taehoon Kim
|
||||||
:license: BSD 3-Clause, see LICENSE for more details.
|
:license: BSD 3-Clause, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
# These imports are far too general, but they're needed for backwards compatbility.
|
||||||
from .models import *
|
from .models import *
|
||||||
from .client import *
|
|
||||||
|
from ._client import Client
|
||||||
|
from ._util import log # TODO: Remove this (from examples too)
|
||||||
|
|
||||||
__title__ = "fbchat"
|
__title__ = "fbchat"
|
||||||
__version__ = "1.6.4"
|
__version__ = "1.7.0"
|
||||||
__description__ = "Facebook Chat (Messenger) for Python"
|
__description__ = "Facebook Chat (Messenger) for Python"
|
||||||
|
|
||||||
__copyright__ = "Copyright 2015 - 2019 by Taehoon Kim"
|
__copyright__ = "Copyright 2015 - 2019 by Taehoon Kim"
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
from . import _util
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
@@ -46,3 +47,40 @@ class ShareAttachment(Attachment):
|
|||||||
|
|
||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
from . import _file
|
||||||
|
|
||||||
|
url = data.get("url")
|
||||||
|
rtn = cls(
|
||||||
|
uid=data.get("deduplication_key"),
|
||||||
|
author=data["target"]["actors"][0]["id"]
|
||||||
|
if data["target"].get("actors")
|
||||||
|
else None,
|
||||||
|
url=url,
|
||||||
|
original_url=_util.get_url_parameter(url, "u")
|
||||||
|
if "/l.php?u=" in url
|
||||||
|
else url,
|
||||||
|
title=data["title_with_entities"].get("text"),
|
||||||
|
description=data["description"].get("text")
|
||||||
|
if data.get("description")
|
||||||
|
else None,
|
||||||
|
source=data["source"].get("text") if data.get("source") else None,
|
||||||
|
attachments=[
|
||||||
|
_file.graphql_to_subattachment(attachment)
|
||||||
|
for attachment in data.get("subattachments")
|
||||||
|
],
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.original_image_url = (
|
||||||
|
_util.get_url_parameter(rtn.image_url, "url")
|
||||||
|
if "/safe_image.php" in rtn.image_url
|
||||||
|
else rtn.image_url
|
||||||
|
)
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
return rtn
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,11 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import logging
|
||||||
import aenum
|
import aenum
|
||||||
|
|
||||||
|
log = logging.getLogger("client")
|
||||||
|
|
||||||
|
|
||||||
class Enum(aenum.Enum):
|
class Enum(aenum.Enum):
|
||||||
"""Used internally by fbchat to support enumerations"""
|
"""Used internally by fbchat to support enumerations"""
|
||||||
@@ -10,3 +13,14 @@ class Enum(aenum.Enum):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
# For documentation:
|
# For documentation:
|
||||||
return "{}.{}".format(type(self).__name__, self.name)
|
return "{}.{}".format(type(self).__name__, self.name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _extend_if_invalid(cls, value):
|
||||||
|
try:
|
||||||
|
return cls(value)
|
||||||
|
except ValueError:
|
||||||
|
log.warning(
|
||||||
|
"Failed parsing {.__name__}({!r}). Extending enum.".format(cls, value)
|
||||||
|
)
|
||||||
|
aenum.extend_enum(cls, "UNKNOWN_{}".format(value).upper(), value)
|
||||||
|
return cls(value)
|
||||||
|
@@ -21,6 +21,15 @@ class FileAttachment(Attachment):
|
|||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
return cls(
|
||||||
|
url=data.get("url"),
|
||||||
|
name=data.get("filename"),
|
||||||
|
is_malicious=data.get("is_malicious"),
|
||||||
|
uid=data.get("message_file_fbid"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
class AudioAttachment(Attachment):
|
class AudioAttachment(Attachment):
|
||||||
@@ -38,6 +47,15 @@ class AudioAttachment(Attachment):
|
|||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
return cls(
|
||||||
|
filename=data.get("filename"),
|
||||||
|
url=data.get("playable_url"),
|
||||||
|
duration=data.get("playable_duration_in_ms"),
|
||||||
|
audio_type=data.get("audio_type"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
class ImageAttachment(Attachment):
|
class ImageAttachment(Attachment):
|
||||||
@@ -122,6 +140,21 @@ class ImageAttachment(Attachment):
|
|||||||
self.animated_preview_width = animated_preview.get("width")
|
self.animated_preview_width = animated_preview.get("width")
|
||||||
self.animated_preview_height = animated_preview.get("height")
|
self.animated_preview_height = animated_preview.get("height")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
return cls(
|
||||||
|
original_extension=data.get("original_extension")
|
||||||
|
or (data["filename"].split("-")[0] if data.get("filename") else None),
|
||||||
|
width=data.get("original_dimensions", {}).get("width"),
|
||||||
|
height=data.get("original_dimensions", {}).get("height"),
|
||||||
|
is_animated=data["__typename"] == "MessageAnimatedImage",
|
||||||
|
thumbnail_url=data.get("thumbnail", {}).get("uri"),
|
||||||
|
preview=data.get("preview") or data.get("preview_image"),
|
||||||
|
large_preview=data.get("large_preview"),
|
||||||
|
animated_preview=data.get("animated_image"),
|
||||||
|
uid=data.get("legacy_attachment_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
class VideoAttachment(Attachment):
|
class VideoAttachment(Attachment):
|
||||||
@@ -195,3 +228,50 @@ class VideoAttachment(Attachment):
|
|||||||
self.large_image_url = large_image.get("uri")
|
self.large_image_url = large_image.get("uri")
|
||||||
self.large_image_width = large_image.get("width")
|
self.large_image_width = large_image.get("width")
|
||||||
self.large_image_height = large_image.get("height")
|
self.large_image_height = large_image.get("height")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
return cls(
|
||||||
|
width=data.get("original_dimensions", {}).get("width"),
|
||||||
|
height=data.get("original_dimensions", {}).get("height"),
|
||||||
|
duration=data.get("playable_duration_in_ms"),
|
||||||
|
preview_url=data.get("playable_url"),
|
||||||
|
small_image=data.get("chat_image"),
|
||||||
|
medium_image=data.get("inbox_image"),
|
||||||
|
large_image=data.get("large_image"),
|
||||||
|
uid=data.get("legacy_attachment_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_subattachment(cls, data):
|
||||||
|
media = data["media"]
|
||||||
|
return cls(
|
||||||
|
duration=media.get("playable_duration_in_ms"),
|
||||||
|
preview_url=media.get("playable_url"),
|
||||||
|
medium_image=media.get("image"),
|
||||||
|
uid=data["target"].get("video_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_to_attachment(data):
|
||||||
|
_type = data["__typename"]
|
||||||
|
if _type in ["MessageImage", "MessageAnimatedImage"]:
|
||||||
|
return ImageAttachment._from_graphql(data)
|
||||||
|
elif _type == "MessageVideo":
|
||||||
|
return VideoAttachment._from_graphql(data)
|
||||||
|
elif _type == "MessageAudio":
|
||||||
|
return AudioAttachment._from_graphql(data)
|
||||||
|
elif _type == "MessageFile":
|
||||||
|
return FileAttachment._from_graphql(data)
|
||||||
|
|
||||||
|
return Attachment(uid=data.get("legacy_attachment_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_to_subattachment(data):
|
||||||
|
target = data.get("target")
|
||||||
|
type_ = target.get("__typename") if target else None
|
||||||
|
|
||||||
|
if type_ == "Video":
|
||||||
|
return VideoAttachment._from_subattachment(data)
|
||||||
|
|
||||||
|
return None
|
||||||
|
232
fbchat/_graphql.py
Normal file
232
fbchat/_graphql.py
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from . import _util
|
||||||
|
from ._exception import FBchatException, FBchatUserError
|
||||||
|
|
||||||
|
# Shameless copy from https://stackoverflow.com/a/8730674
|
||||||
|
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
|
||||||
|
WHITESPACE = re.compile(r"[ \t\n\r]*", FLAGS)
|
||||||
|
|
||||||
|
|
||||||
|
class ConcatJSONDecoder(json.JSONDecoder):
|
||||||
|
def decode(self, s, _w=WHITESPACE.match):
|
||||||
|
s_len = len(s)
|
||||||
|
|
||||||
|
objs = []
|
||||||
|
end = 0
|
||||||
|
while end != s_len:
|
||||||
|
obj, end = self.raw_decode(s, idx=_w(s, end).end())
|
||||||
|
end = _w(s, end).end()
|
||||||
|
objs.append(obj)
|
||||||
|
return objs
|
||||||
|
|
||||||
|
|
||||||
|
# End shameless copy
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_queries_to_json(*queries):
|
||||||
|
"""
|
||||||
|
Queries should be a list of GraphQL objects
|
||||||
|
"""
|
||||||
|
rtn = {}
|
||||||
|
for i, query in enumerate(queries):
|
||||||
|
rtn["q{}".format(i)] = query.value
|
||||||
|
return json.dumps(rtn)
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_response_to_json(content):
|
||||||
|
content = _util.strip_to_json(content) # Usually only needed in some error cases
|
||||||
|
try:
|
||||||
|
j = json.loads(content, cls=ConcatJSONDecoder)
|
||||||
|
except Exception:
|
||||||
|
raise FBchatException("Error while parsing JSON: {}".format(repr(content)))
|
||||||
|
|
||||||
|
rtn = [None] * (len(j))
|
||||||
|
for x in j:
|
||||||
|
if "error_results" in x:
|
||||||
|
del rtn[-1]
|
||||||
|
continue
|
||||||
|
_util.check_json(x)
|
||||||
|
[(key, value)] = x.items()
|
||||||
|
_util.check_json(value)
|
||||||
|
if "response" in value:
|
||||||
|
rtn[int(key[1:])] = value["response"]
|
||||||
|
else:
|
||||||
|
rtn[int(key[1:])] = value["data"]
|
||||||
|
|
||||||
|
_util.log.debug(rtn)
|
||||||
|
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
|
class GraphQL(object):
|
||||||
|
def __init__(self, query=None, doc_id=None, params=None):
|
||||||
|
if params is None:
|
||||||
|
params = {}
|
||||||
|
if query is not None:
|
||||||
|
self.value = {"priority": 0, "q": query, "query_params": params}
|
||||||
|
elif doc_id is not None:
|
||||||
|
self.value = {"doc_id": doc_id, "query_params": params}
|
||||||
|
else:
|
||||||
|
raise FBchatUserError("A query or doc_id must be specified")
|
||||||
|
|
||||||
|
FRAGMENT_USER = """
|
||||||
|
QueryFragment User: User {
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
first_name,
|
||||||
|
last_name,
|
||||||
|
profile_picture.width(<pic_size>).height(<pic_size>) {
|
||||||
|
uri
|
||||||
|
},
|
||||||
|
is_viewer_friend,
|
||||||
|
url,
|
||||||
|
gender,
|
||||||
|
viewer_affinity
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
FRAGMENT_GROUP = """
|
||||||
|
QueryFragment Group: MessageThread {
|
||||||
|
name,
|
||||||
|
thread_key {
|
||||||
|
thread_fbid
|
||||||
|
},
|
||||||
|
image {
|
||||||
|
uri
|
||||||
|
},
|
||||||
|
is_group_thread,
|
||||||
|
all_participants {
|
||||||
|
nodes {
|
||||||
|
messaging_actor {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
customization_info {
|
||||||
|
participant_customizations {
|
||||||
|
participant_id,
|
||||||
|
nickname
|
||||||
|
},
|
||||||
|
outgoing_bubble_color,
|
||||||
|
emoji
|
||||||
|
},
|
||||||
|
thread_admins {
|
||||||
|
id
|
||||||
|
},
|
||||||
|
group_approval_queue {
|
||||||
|
nodes {
|
||||||
|
requester {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
approval_mode,
|
||||||
|
joinable_mode {
|
||||||
|
mode,
|
||||||
|
link
|
||||||
|
},
|
||||||
|
event_reminders {
|
||||||
|
nodes {
|
||||||
|
id,
|
||||||
|
lightweight_event_creator {
|
||||||
|
id
|
||||||
|
},
|
||||||
|
time,
|
||||||
|
location_name,
|
||||||
|
event_title,
|
||||||
|
event_reminder_members {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
},
|
||||||
|
guest_list_state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
FRAGMENT_PAGE = """
|
||||||
|
QueryFragment Page: Page {
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
profile_picture.width(32).height(32) {
|
||||||
|
uri
|
||||||
|
},
|
||||||
|
url,
|
||||||
|
category_type,
|
||||||
|
city {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
SEARCH_USER = (
|
||||||
|
"""
|
||||||
|
Query SearchUser(<search> = '', <limit> = 10) {
|
||||||
|
entities_named(<search>) {
|
||||||
|
search_results.of_type(user).first(<limit>) as users {
|
||||||
|
nodes {
|
||||||
|
@User
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
+ FRAGMENT_USER
|
||||||
|
)
|
||||||
|
|
||||||
|
SEARCH_GROUP = (
|
||||||
|
"""
|
||||||
|
Query SearchGroup(<search> = '', <limit> = 10, <pic_size> = 32) {
|
||||||
|
viewer() {
|
||||||
|
message_threads.with_thread_name(<search>).last(<limit>) as groups {
|
||||||
|
nodes {
|
||||||
|
@Group
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
+ FRAGMENT_GROUP
|
||||||
|
)
|
||||||
|
|
||||||
|
SEARCH_PAGE = (
|
||||||
|
"""
|
||||||
|
Query SearchPage(<search> = '', <limit> = 10) {
|
||||||
|
entities_named(<search>) {
|
||||||
|
search_results.of_type(page).first(<limit>) as pages {
|
||||||
|
nodes {
|
||||||
|
@Page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
+ FRAGMENT_PAGE
|
||||||
|
)
|
||||||
|
|
||||||
|
SEARCH_THREAD = (
|
||||||
|
"""
|
||||||
|
Query SearchThread(<search> = '', <limit> = 10) {
|
||||||
|
entities_named(<search>) {
|
||||||
|
search_results.first(<limit>) as threads {
|
||||||
|
nodes {
|
||||||
|
__typename,
|
||||||
|
@User,
|
||||||
|
@Group,
|
||||||
|
@Page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
+ FRAGMENT_USER
|
||||||
|
+ FRAGMENT_GROUP
|
||||||
|
+ FRAGMENT_PAGE
|
||||||
|
)
|
@@ -2,6 +2,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
from . import _plan
|
||||||
from ._thread import ThreadType, Thread
|
from ._thread import ThreadType, Thread
|
||||||
|
|
||||||
|
|
||||||
@@ -60,6 +61,49 @@ class Group(Thread):
|
|||||||
self.approval_requests = approval_requests
|
self.approval_requests = approval_requests
|
||||||
self.join_link = join_link
|
self.join_link = join_link
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if data.get("image") is None:
|
||||||
|
data["image"] = {}
|
||||||
|
c_info = cls._parse_customization_info(data)
|
||||||
|
last_message_timestamp = None
|
||||||
|
if "last_message" in data:
|
||||||
|
last_message_timestamp = data["last_message"]["nodes"][0][
|
||||||
|
"timestamp_precise"
|
||||||
|
]
|
||||||
|
plan = None
|
||||||
|
if data.get("event_reminders") and data["event_reminders"].get("nodes"):
|
||||||
|
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
|
||||||
|
|
||||||
|
return cls(
|
||||||
|
data["thread_key"]["thread_fbid"],
|
||||||
|
participants=set(
|
||||||
|
[
|
||||||
|
node["messaging_actor"]["id"]
|
||||||
|
for node in data["all_participants"]["nodes"]
|
||||||
|
]
|
||||||
|
),
|
||||||
|
nicknames=c_info.get("nicknames"),
|
||||||
|
color=c_info.get("color"),
|
||||||
|
emoji=c_info.get("emoji"),
|
||||||
|
admins=set([node.get("id") for node in data.get("thread_admins")]),
|
||||||
|
approval_mode=bool(data.get("approval_mode"))
|
||||||
|
if data.get("approval_mode") is not None
|
||||||
|
else None,
|
||||||
|
approval_requests=set(
|
||||||
|
node["requester"]["id"]
|
||||||
|
for node in data["group_approval_queue"]["nodes"]
|
||||||
|
)
|
||||||
|
if data.get("group_approval_queue")
|
||||||
|
else None,
|
||||||
|
join_link=data["joinable_mode"].get("link"),
|
||||||
|
photo=data["image"].get("uri"),
|
||||||
|
name=data.get("name"),
|
||||||
|
message_count=data.get("messages_count"),
|
||||||
|
last_message_timestamp=last_message_timestamp,
|
||||||
|
plan=plan,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
class Room(Group):
|
class Room(Group):
|
||||||
|
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import attr
|
import attr
|
||||||
from ._attachment import Attachment
|
from ._attachment import Attachment
|
||||||
|
from . import _util
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
@@ -30,6 +31,30 @@ class LocationAttachment(Attachment):
|
|||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
url = data.get("url")
|
||||||
|
address = _util.get_url_parameter(_util.get_url_parameter(url, "u"), "where1")
|
||||||
|
try:
|
||||||
|
latitude, longitude = [float(x) for x in address.split(", ")]
|
||||||
|
address = None
|
||||||
|
except ValueError:
|
||||||
|
latitude, longitude = None, None
|
||||||
|
rtn = cls(
|
||||||
|
uid=int(data["deduplication_key"]),
|
||||||
|
latitude=latitude,
|
||||||
|
longitude=longitude,
|
||||||
|
address=address,
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
rtn.url = url
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
class LiveLocationAttachment(LocationAttachment):
|
class LiveLocationAttachment(LocationAttachment):
|
||||||
@@ -46,3 +71,42 @@ class LiveLocationAttachment(LocationAttachment):
|
|||||||
super(LiveLocationAttachment, self).__init__(**kwargs)
|
super(LiveLocationAttachment, self).__init__(**kwargs)
|
||||||
self.expiration_time = expiration_time
|
self.expiration_time = expiration_time
|
||||||
self.is_expired = is_expired
|
self.is_expired = is_expired
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_pull(cls, data):
|
||||||
|
return cls(
|
||||||
|
uid=data["id"],
|
||||||
|
latitude=data["coordinate"]["latitude"] / (10 ** 8)
|
||||||
|
if not data.get("stopReason")
|
||||||
|
else None,
|
||||||
|
longitude=data["coordinate"]["longitude"] / (10 ** 8)
|
||||||
|
if not data.get("stopReason")
|
||||||
|
else None,
|
||||||
|
name=data.get("locationTitle"),
|
||||||
|
expiration_time=data["expirationTime"],
|
||||||
|
is_expired=bool(data.get("stopReason")),
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
target = data["target"]
|
||||||
|
rtn = cls(
|
||||||
|
uid=int(target["live_location_id"]),
|
||||||
|
latitude=target["coordinate"]["latitude"]
|
||||||
|
if target.get("coordinate")
|
||||||
|
else None,
|
||||||
|
longitude=target["coordinate"]["longitude"]
|
||||||
|
if target.get("coordinate")
|
||||||
|
else None,
|
||||||
|
name=data["title_with_entities"]["text"],
|
||||||
|
expiration_time=target.get("expiration_time"),
|
||||||
|
is_expired=target.get("is_expired"),
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
rtn.url = data.get("url")
|
||||||
|
return rtn
|
||||||
|
@@ -2,7 +2,9 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
import json
|
||||||
from string import Formatter
|
from string import Formatter
|
||||||
|
from . import _util, _attachment, _location, _file, _quick_reply, _sticker
|
||||||
from ._core import Enum
|
from ._core import Enum
|
||||||
|
|
||||||
|
|
||||||
@@ -13,6 +15,22 @@ class EmojiSize(Enum):
|
|||||||
MEDIUM = "369239343222814"
|
MEDIUM = "369239343222814"
|
||||||
SMALL = "369239263222822"
|
SMALL = "369239263222822"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_tags(cls, tags):
|
||||||
|
string_to_emojisize = {
|
||||||
|
"large": cls.LARGE,
|
||||||
|
"medium": cls.MEDIUM,
|
||||||
|
"small": cls.SMALL,
|
||||||
|
"l": cls.LARGE,
|
||||||
|
"m": cls.MEDIUM,
|
||||||
|
"s": cls.SMALL,
|
||||||
|
}
|
||||||
|
for tag in tags or ():
|
||||||
|
data = tag.split(":", maxsplit=1)
|
||||||
|
if len(data) > 1 and data[0] == "hot_emoji_size":
|
||||||
|
return string_to_emojisize.get(data[1])
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class MessageReaction(Enum):
|
class MessageReaction(Enum):
|
||||||
"""Used to specify a message reaction"""
|
"""Used to specify a message reaction"""
|
||||||
@@ -68,6 +86,12 @@ class Message(object):
|
|||||||
quick_replies = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
|
quick_replies = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
|
||||||
#: Whether the message is unsent (deleted for everyone)
|
#: Whether the message is unsent (deleted for everyone)
|
||||||
unsent = attr.ib(False, init=False)
|
unsent = attr.ib(False, init=False)
|
||||||
|
#: Message ID you want to reply to
|
||||||
|
reply_to_id = attr.ib(None)
|
||||||
|
#: Replied message
|
||||||
|
replied_to = attr.ib(None, init=False)
|
||||||
|
#: Whether the message was forwarded
|
||||||
|
forwarded = attr.ib(False, init=False)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def formatMentions(cls, text, *args, **kwargs):
|
def formatMentions(cls, text, *args, **kwargs):
|
||||||
@@ -121,3 +145,203 @@ class Message(object):
|
|||||||
|
|
||||||
message = cls(text=result, mentions=mentions)
|
message = cls(text=result, mentions=mentions)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_forwarded_from_tags(tags):
|
||||||
|
if tags is None:
|
||||||
|
return False
|
||||||
|
return any(map(lambda tag: "forward" in tag or "copy" in tag, tags))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if data.get("message_sender") is None:
|
||||||
|
data["message_sender"] = {}
|
||||||
|
if data.get("message") is None:
|
||||||
|
data["message"] = {}
|
||||||
|
tags = data.get("tags_list")
|
||||||
|
rtn = cls(
|
||||||
|
text=data["message"].get("text"),
|
||||||
|
mentions=[
|
||||||
|
Mention(
|
||||||
|
m.get("entity", {}).get("id"),
|
||||||
|
offset=m.get("offset"),
|
||||||
|
length=m.get("length"),
|
||||||
|
)
|
||||||
|
for m in data["message"].get("ranges") or ()
|
||||||
|
],
|
||||||
|
emoji_size=EmojiSize._from_tags(tags),
|
||||||
|
sticker=_sticker.Sticker._from_graphql(data.get("sticker")),
|
||||||
|
)
|
||||||
|
rtn.forwarded = cls._get_forwarded_from_tags(tags)
|
||||||
|
rtn.uid = str(data["message_id"])
|
||||||
|
rtn.author = str(data["message_sender"]["id"])
|
||||||
|
rtn.timestamp = data.get("timestamp_precise")
|
||||||
|
rtn.unsent = False
|
||||||
|
if data.get("unread") is not None:
|
||||||
|
rtn.is_read = not data["unread"]
|
||||||
|
rtn.reactions = {
|
||||||
|
str(r["user"]["id"]): MessageReaction._extend_if_invalid(r["reaction"])
|
||||||
|
for r in data["message_reactions"]
|
||||||
|
}
|
||||||
|
if data.get("blob_attachments") is not None:
|
||||||
|
rtn.attachments = [
|
||||||
|
_file.graphql_to_attachment(attachment)
|
||||||
|
for attachment in data["blob_attachments"]
|
||||||
|
]
|
||||||
|
if data.get("platform_xmd_encoded"):
|
||||||
|
quick_replies = json.loads(data["platform_xmd_encoded"]).get(
|
||||||
|
"quick_replies"
|
||||||
|
)
|
||||||
|
if isinstance(quick_replies, list):
|
||||||
|
rtn.quick_replies = [
|
||||||
|
_quick_reply.graphql_to_quick_reply(q) for q in quick_replies
|
||||||
|
]
|
||||||
|
elif isinstance(quick_replies, dict):
|
||||||
|
rtn.quick_replies = [
|
||||||
|
_quick_reply.graphql_to_quick_reply(quick_replies, is_response=True)
|
||||||
|
]
|
||||||
|
if data.get("extensible_attachment") is not None:
|
||||||
|
attachment = graphql_to_extensible_attachment(data["extensible_attachment"])
|
||||||
|
if isinstance(attachment, _attachment.UnsentMessage):
|
||||||
|
rtn.unsent = True
|
||||||
|
elif attachment:
|
||||||
|
rtn.attachments.append(attachment)
|
||||||
|
if data.get("replied_to_message") is not None:
|
||||||
|
rtn.replied_to = cls._from_graphql(data["replied_to_message"]["message"])
|
||||||
|
rtn.reply_to_id = rtn.replied_to.uid
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_reply(cls, data):
|
||||||
|
tags = data["messageMetadata"].get("tags")
|
||||||
|
rtn = cls(
|
||||||
|
text=data.get("body"),
|
||||||
|
mentions=[
|
||||||
|
Mention(m.get("i"), offset=m.get("o"), length=m.get("l"))
|
||||||
|
for m in json.loads(data.get("data", {}).get("prng", "[]"))
|
||||||
|
],
|
||||||
|
emoji_size=EmojiSize._from_tags(tags),
|
||||||
|
)
|
||||||
|
metadata = data.get("messageMetadata", {})
|
||||||
|
rtn.forwarded = cls._get_forwarded_from_tags(tags)
|
||||||
|
rtn.uid = metadata.get("messageId")
|
||||||
|
rtn.author = str(metadata.get("actorFbId"))
|
||||||
|
rtn.timestamp = metadata.get("timestamp")
|
||||||
|
rtn.unsent = False
|
||||||
|
if data.get("data", {}).get("platform_xmd"):
|
||||||
|
quick_replies = json.loads(data["data"]["platform_xmd"]).get(
|
||||||
|
"quick_replies"
|
||||||
|
)
|
||||||
|
if isinstance(quick_replies, list):
|
||||||
|
rtn.quick_replies = [
|
||||||
|
_quick_reply.graphql_to_quick_reply(q) for q in quick_replies
|
||||||
|
]
|
||||||
|
elif isinstance(quick_replies, dict):
|
||||||
|
rtn.quick_replies = [
|
||||||
|
_quick_reply.graphql_to_quick_reply(quick_replies, is_response=True)
|
||||||
|
]
|
||||||
|
if data.get("attachments") is not None:
|
||||||
|
for attachment in data["attachments"]:
|
||||||
|
attachment = json.loads(attachment["mercuryJSON"])
|
||||||
|
if attachment.get("blob_attachment"):
|
||||||
|
rtn.attachments.append(
|
||||||
|
_file.graphql_to_attachment(attachment["blob_attachment"])
|
||||||
|
)
|
||||||
|
if attachment.get("extensible_attachment"):
|
||||||
|
extensible_attachment = graphql_to_extensible_attachment(
|
||||||
|
attachment["extensible_attachment"]
|
||||||
|
)
|
||||||
|
if isinstance(extensible_attachment, _attachment.UnsentMessage):
|
||||||
|
rtn.unsent = True
|
||||||
|
else:
|
||||||
|
rtn.attachments.append(extensible_attachment)
|
||||||
|
if attachment.get("sticker_attachment"):
|
||||||
|
rtn.sticker = _sticker.Sticker._from_graphql(
|
||||||
|
attachment["sticker_attachment"]
|
||||||
|
)
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_pull(cls, data, mid=None, tags=None, author=None, timestamp=None):
|
||||||
|
rtn = cls(text=data.get("body"))
|
||||||
|
rtn.uid = mid
|
||||||
|
rtn.author = author
|
||||||
|
rtn.timestamp = timestamp
|
||||||
|
|
||||||
|
if data.get("data") and data["data"].get("prng"):
|
||||||
|
try:
|
||||||
|
rtn.mentions = [
|
||||||
|
Mention(
|
||||||
|
str(mention.get("i")),
|
||||||
|
offset=mention.get("o"),
|
||||||
|
length=mention.get("l"),
|
||||||
|
)
|
||||||
|
for mention in _util.parse_json(data["data"]["prng"])
|
||||||
|
]
|
||||||
|
except Exception:
|
||||||
|
_util.log.exception("An exception occured while reading attachments")
|
||||||
|
|
||||||
|
if data.get("attachments"):
|
||||||
|
try:
|
||||||
|
for a in data["attachments"]:
|
||||||
|
mercury = a["mercury"]
|
||||||
|
if mercury.get("blob_attachment"):
|
||||||
|
image_metadata = a.get("imageMetadata", {})
|
||||||
|
attach_type = mercury["blob_attachment"]["__typename"]
|
||||||
|
attachment = _file.graphql_to_attachment(
|
||||||
|
mercury["blob_attachment"]
|
||||||
|
)
|
||||||
|
|
||||||
|
if attach_type in [
|
||||||
|
"MessageFile",
|
||||||
|
"MessageVideo",
|
||||||
|
"MessageAudio",
|
||||||
|
]:
|
||||||
|
# TODO: Add more data here for audio files
|
||||||
|
attachment.size = int(a["fileSize"])
|
||||||
|
rtn.attachments.append(attachment)
|
||||||
|
|
||||||
|
elif mercury.get("sticker_attachment"):
|
||||||
|
rtn.sticker = _sticker.Sticker._from_graphql(
|
||||||
|
mercury["sticker_attachment"]
|
||||||
|
)
|
||||||
|
|
||||||
|
elif mercury.get("extensible_attachment"):
|
||||||
|
attachment = graphql_to_extensible_attachment(
|
||||||
|
mercury["extensible_attachment"]
|
||||||
|
)
|
||||||
|
if isinstance(attachment, _attachment.UnsentMessage):
|
||||||
|
rtn.unsent = True
|
||||||
|
elif attachment:
|
||||||
|
rtn.attachments.append(attachment)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
_util.log.exception(
|
||||||
|
"An exception occured while reading attachments: {}".format(
|
||||||
|
data["attachments"]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
rtn.emoji_size = EmojiSize._from_tags(tags)
|
||||||
|
rtn.forwarded = cls._get_forwarded_from_tags(tags)
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_to_extensible_attachment(data):
|
||||||
|
story = data.get("story_attachment")
|
||||||
|
if not story:
|
||||||
|
return None
|
||||||
|
|
||||||
|
target = story.get("target")
|
||||||
|
if not target:
|
||||||
|
return _attachment.UnsentMessage(uid=data.get("legacy_attachment_id"))
|
||||||
|
|
||||||
|
_type = target["__typename"]
|
||||||
|
if _type == "MessageLocation":
|
||||||
|
return _location.LocationAttachment._from_graphql(story)
|
||||||
|
elif _type == "MessageLiveLocation":
|
||||||
|
return _location.LiveLocationAttachment._from_graphql(story)
|
||||||
|
elif _type in ["ExternalUrl", "Story"]:
|
||||||
|
return _attachment.ShareAttachment._from_graphql(story)
|
||||||
|
|
||||||
|
return None
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
from . import _plan
|
||||||
from ._thread import ThreadType, Thread
|
from ._thread import ThreadType, Thread
|
||||||
|
|
||||||
|
|
||||||
@@ -36,3 +37,24 @@ class Page(Thread):
|
|||||||
self.likes = likes
|
self.likes = likes
|
||||||
self.sub_title = sub_title
|
self.sub_title = sub_title
|
||||||
self.category = category
|
self.category = category
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if data.get("profile_picture") is None:
|
||||||
|
data["profile_picture"] = {}
|
||||||
|
if data.get("city") is None:
|
||||||
|
data["city"] = {}
|
||||||
|
plan = None
|
||||||
|
if data.get("event_reminders") and data["event_reminders"].get("nodes"):
|
||||||
|
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
|
||||||
|
|
||||||
|
return cls(
|
||||||
|
data["id"],
|
||||||
|
url=data.get("url"),
|
||||||
|
city=data.get("city").get("name"),
|
||||||
|
category=data.get("category_type"),
|
||||||
|
photo=data["profile_picture"].get("uri"),
|
||||||
|
name=data.get("name"),
|
||||||
|
message_count=data.get("messages_count"),
|
||||||
|
plan=plan,
|
||||||
|
)
|
||||||
|
@@ -2,6 +2,14 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
import json
|
||||||
|
from ._core import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class GuestStatus(Enum):
|
||||||
|
INVITED = 1
|
||||||
|
GOING = 2
|
||||||
|
DECLINED = 3
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
@@ -20,9 +28,76 @@ class Plan(object):
|
|||||||
location_id = attr.ib(None, converter=lambda x: x or "")
|
location_id = attr.ib(None, converter=lambda x: x or "")
|
||||||
#: ID of the plan creator
|
#: ID of the plan creator
|
||||||
author_id = attr.ib(None, init=False)
|
author_id = attr.ib(None, init=False)
|
||||||
#: List of the people IDs who will take part in the plan
|
#: Dict of `User` IDs mapped to their `GuestStatus`
|
||||||
going = attr.ib(factory=list, init=False)
|
guests = attr.ib(None, init=False)
|
||||||
#: List of the people IDs who won't take part in the plan
|
|
||||||
declined = attr.ib(factory=list, init=False)
|
@property
|
||||||
#: List of the people IDs who are invited to the plan
|
def going(self):
|
||||||
invited = attr.ib(factory=list, init=False)
|
"""List of the `User` IDs who will take part in the plan."""
|
||||||
|
return [
|
||||||
|
id_
|
||||||
|
for id_, status in (self.guests or {}).items()
|
||||||
|
if status is GuestStatus.GOING
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def declined(self):
|
||||||
|
"""List of the `User` IDs who won't take part in the plan."""
|
||||||
|
return [
|
||||||
|
id_
|
||||||
|
for id_, status in (self.guests or {}).items()
|
||||||
|
if status is GuestStatus.DECLINED
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def invited(self):
|
||||||
|
"""List of the `User` IDs who are invited to the plan."""
|
||||||
|
return [
|
||||||
|
id_
|
||||||
|
for id_, status in (self.guests or {}).items()
|
||||||
|
if status is GuestStatus.INVITED
|
||||||
|
]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_pull(cls, data):
|
||||||
|
rtn = cls(
|
||||||
|
time=data.get("event_time"),
|
||||||
|
title=data.get("event_title"),
|
||||||
|
location=data.get("event_location_name"),
|
||||||
|
location_id=data.get("event_location_id"),
|
||||||
|
)
|
||||||
|
rtn.uid = data.get("event_id")
|
||||||
|
rtn.author_id = data.get("event_creator_id")
|
||||||
|
rtn.guests = {
|
||||||
|
x["node"]["id"]: GuestStatus[x["guest_list_state"]]
|
||||||
|
for x in json.loads(data["guest_state_list"])
|
||||||
|
}
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_fetch(cls, data):
|
||||||
|
rtn = cls(
|
||||||
|
time=data.get("event_time"),
|
||||||
|
title=data.get("title"),
|
||||||
|
location=data.get("location_name"),
|
||||||
|
location_id=str(data["location_id"]) if data.get("location_id") else None,
|
||||||
|
)
|
||||||
|
rtn.uid = data.get("oid")
|
||||||
|
rtn.author_id = data.get("creator_id")
|
||||||
|
rtn.guests = {id_: GuestStatus[s] for id_, s in data["event_members"].items()}
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
rtn = cls(
|
||||||
|
time=data.get("time"),
|
||||||
|
title=data.get("event_title"),
|
||||||
|
location=data.get("location_name"),
|
||||||
|
)
|
||||||
|
rtn.uid = data.get("id")
|
||||||
|
rtn.author_id = data["lightweight_event_creator"].get("id")
|
||||||
|
rtn.guests = {
|
||||||
|
x["node"]["id"]: GuestStatus[x["guest_list_state"]]
|
||||||
|
for x in data["event_reminder_members"]["edges"]
|
||||||
|
}
|
||||||
|
return rtn
|
||||||
|
@@ -8,27 +8,60 @@ import attr
|
|||||||
class Poll(object):
|
class Poll(object):
|
||||||
"""Represents a poll"""
|
"""Represents a poll"""
|
||||||
|
|
||||||
#: ID of the poll
|
|
||||||
uid = attr.ib(None, init=False)
|
|
||||||
#: Title of the poll
|
#: Title of the poll
|
||||||
title = attr.ib()
|
title = attr.ib()
|
||||||
#: List of :class:`PollOption`, can be fetched with :func:`fbchat.Client.fetchPollOptions`
|
#: List of :class:`PollOption`, can be fetched with :func:`fbchat.Client.fetchPollOptions`
|
||||||
options = attr.ib()
|
options = attr.ib()
|
||||||
#: Options count
|
#: Options count
|
||||||
options_count = attr.ib(None, init=False)
|
options_count = attr.ib(None)
|
||||||
|
#: ID of the poll
|
||||||
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
return cls(
|
||||||
|
uid=int(data["id"]),
|
||||||
|
title=data.get("title") if data.get("title") else data.get("text"),
|
||||||
|
options=[PollOption._from_graphql(m) for m in data.get("options")],
|
||||||
|
options_count=data.get("total_count"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
class PollOption(object):
|
class PollOption(object):
|
||||||
"""Represents a poll option"""
|
"""Represents a poll option"""
|
||||||
|
|
||||||
#: ID of the poll option
|
|
||||||
uid = attr.ib(None, init=False)
|
|
||||||
#: Text of the poll option
|
#: Text of the poll option
|
||||||
text = attr.ib()
|
text = attr.ib()
|
||||||
#: Whether vote when creating or client voted
|
#: Whether vote when creating or client voted
|
||||||
vote = attr.ib(False)
|
vote = attr.ib(False)
|
||||||
#: ID of the users who voted for this poll option
|
#: ID of the users who voted for this poll option
|
||||||
voters = attr.ib(None, init=False)
|
voters = attr.ib(None)
|
||||||
#: Votes count
|
#: Votes count
|
||||||
votes_count = attr.ib(None, init=False)
|
votes_count = attr.ib(None)
|
||||||
|
#: ID of the poll option
|
||||||
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if data.get("viewer_has_voted") is None:
|
||||||
|
vote = None
|
||||||
|
elif isinstance(data["viewer_has_voted"], bool):
|
||||||
|
vote = data["viewer_has_voted"]
|
||||||
|
else:
|
||||||
|
vote = data["viewer_has_voted"] == "true"
|
||||||
|
return cls(
|
||||||
|
uid=int(data["id"]),
|
||||||
|
text=data.get("text"),
|
||||||
|
vote=vote,
|
||||||
|
voters=(
|
||||||
|
[m.get("node").get("id") for m in data.get("voters").get("edges")]
|
||||||
|
if isinstance(data.get("voters"), dict)
|
||||||
|
else data.get("voters")
|
||||||
|
),
|
||||||
|
votes_count=(
|
||||||
|
data.get("voters").get("count")
|
||||||
|
if isinstance(data.get("voters"), dict)
|
||||||
|
else data.get("total_count")
|
||||||
|
),
|
||||||
|
)
|
||||||
|
@@ -74,3 +74,26 @@ class QuickReplyEmail(QuickReply):
|
|||||||
def __init__(self, image_url=None, **kwargs):
|
def __init__(self, image_url=None, **kwargs):
|
||||||
super(QuickReplyEmail, self).__init__(**kwargs)
|
super(QuickReplyEmail, self).__init__(**kwargs)
|
||||||
self.image_url = image_url
|
self.image_url = image_url
|
||||||
|
|
||||||
|
|
||||||
|
def graphql_to_quick_reply(q, is_response=False):
|
||||||
|
data = dict()
|
||||||
|
_type = q.get("content_type").lower()
|
||||||
|
if q.get("payload"):
|
||||||
|
data["payload"] = q["payload"]
|
||||||
|
if q.get("data"):
|
||||||
|
data["data"] = q["data"]
|
||||||
|
if q.get("image_url") and _type is not QuickReplyLocation._type:
|
||||||
|
data["image_url"] = q["image_url"]
|
||||||
|
data["is_response"] = is_response
|
||||||
|
if _type == QuickReplyText._type:
|
||||||
|
if q.get("title") is not None:
|
||||||
|
data["title"] = q["title"]
|
||||||
|
rtn = QuickReplyText(**data)
|
||||||
|
elif _type == QuickReplyLocation._type:
|
||||||
|
rtn = QuickReplyLocation(**data)
|
||||||
|
elif _type == QuickReplyPhoneNumber._type:
|
||||||
|
rtn = QuickReplyPhoneNumber(**data)
|
||||||
|
elif _type == QuickReplyEmail._type:
|
||||||
|
rtn = QuickReplyEmail(**data)
|
||||||
|
return rtn
|
||||||
|
@@ -37,3 +37,24 @@ class Sticker(Attachment):
|
|||||||
|
|
||||||
def __init__(self, uid=None):
|
def __init__(self, uid=None):
|
||||||
super(Sticker, self).__init__(uid=uid)
|
super(Sticker, self).__init__(uid=uid)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if not data:
|
||||||
|
return None
|
||||||
|
self = cls(uid=data["id"])
|
||||||
|
if data.get("pack"):
|
||||||
|
self.pack = data["pack"].get("id")
|
||||||
|
if data.get("sprite_image"):
|
||||||
|
self.is_animated = True
|
||||||
|
self.medium_sprite_image = data["sprite_image"].get("uri")
|
||||||
|
self.large_sprite_image = data["sprite_image_2x"].get("uri")
|
||||||
|
self.frames_per_row = data.get("frames_per_row")
|
||||||
|
self.frames_per_col = data.get("frames_per_column")
|
||||||
|
self.frame_rate = data.get("frame_rate")
|
||||||
|
self.url = data.get("url")
|
||||||
|
self.width = data.get("width")
|
||||||
|
self.height = data.get("height")
|
||||||
|
if data.get("label"):
|
||||||
|
self.label = data["label"]
|
||||||
|
return self
|
||||||
|
@@ -41,6 +41,21 @@ class ThreadColor(Enum):
|
|||||||
CAMEO = "#d4a88c"
|
CAMEO = "#d4a88c"
|
||||||
BRILLIANT_ROSE = "#ff5ca1"
|
BRILLIANT_ROSE = "#ff5ca1"
|
||||||
BILOBA_FLOWER = "#a695c7"
|
BILOBA_FLOWER = "#a695c7"
|
||||||
|
TICKLE_ME_PINK = "#ff7ca8"
|
||||||
|
MALACHITE = "#1adb5b"
|
||||||
|
RUBY = "#f01d6a"
|
||||||
|
DARK_TANGERINE = "#ff9c19"
|
||||||
|
BRIGHT_TURQUOISE = "#0edcde"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, color):
|
||||||
|
if color is None:
|
||||||
|
return None
|
||||||
|
if not color:
|
||||||
|
return cls.MESSENGER_BLUE
|
||||||
|
color = color[2:] # Strip the alpha value
|
||||||
|
value = "#{}".format(color.lower())
|
||||||
|
return cls._extend_if_invalid(value)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
@@ -79,3 +94,36 @@ class Thread(object):
|
|||||||
self.last_message_timestamp = last_message_timestamp
|
self.last_message_timestamp = last_message_timestamp
|
||||||
self.message_count = message_count
|
self.message_count = message_count
|
||||||
self.plan = plan
|
self.plan = plan
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_customization_info(data):
|
||||||
|
if data is None or data.get("customization_info") is None:
|
||||||
|
return {}
|
||||||
|
info = data["customization_info"]
|
||||||
|
|
||||||
|
rtn = {
|
||||||
|
"emoji": info.get("emoji"),
|
||||||
|
"color": ThreadColor._from_graphql(info.get("outgoing_bubble_color")),
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
data.get("thread_type") == "GROUP"
|
||||||
|
or data.get("is_group_thread")
|
||||||
|
or data.get("thread_key", {}).get("thread_fbid")
|
||||||
|
):
|
||||||
|
rtn["nicknames"] = {}
|
||||||
|
for k in info.get("participant_customizations", []):
|
||||||
|
rtn["nicknames"][k["participant_id"]] = k.get("nickname")
|
||||||
|
elif info.get("participant_customizations"):
|
||||||
|
uid = data.get("thread_key", {}).get("other_user_id") or data.get("id")
|
||||||
|
pc = info["participant_customizations"]
|
||||||
|
if len(pc) > 0:
|
||||||
|
if pc[0].get("participant_id") == uid:
|
||||||
|
rtn["nickname"] = pc[0].get("nickname")
|
||||||
|
else:
|
||||||
|
rtn["own_nickname"] = pc[0].get("nickname")
|
||||||
|
if len(pc) > 1:
|
||||||
|
if pc[1].get("participant_id") == uid:
|
||||||
|
rtn["nickname"] = pc[1].get("nickname")
|
||||||
|
else:
|
||||||
|
rtn["own_nickname"] = pc[1].get("nickname")
|
||||||
|
return rtn
|
||||||
|
132
fbchat/_user.py
132
fbchat/_user.py
@@ -3,9 +3,40 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import attr
|
import attr
|
||||||
from ._core import Enum
|
from ._core import Enum
|
||||||
|
from . import _plan
|
||||||
from ._thread import ThreadType, Thread
|
from ._thread import ThreadType, Thread
|
||||||
|
|
||||||
|
|
||||||
|
GENDERS = {
|
||||||
|
# For standard requests
|
||||||
|
0: "unknown",
|
||||||
|
1: "female_singular",
|
||||||
|
2: "male_singular",
|
||||||
|
3: "female_singular_guess",
|
||||||
|
4: "male_singular_guess",
|
||||||
|
5: "mixed",
|
||||||
|
6: "neuter_singular",
|
||||||
|
7: "unknown_singular",
|
||||||
|
8: "female_plural",
|
||||||
|
9: "male_plural",
|
||||||
|
10: "neuter_plural",
|
||||||
|
11: "unknown_plural",
|
||||||
|
# For graphql requests
|
||||||
|
"UNKNOWN": "unknown",
|
||||||
|
"FEMALE": "female_singular",
|
||||||
|
"MALE": "male_singular",
|
||||||
|
# '': 'female_singular_guess',
|
||||||
|
# '': 'male_singular_guess',
|
||||||
|
# '': 'mixed',
|
||||||
|
"NEUTER": "neuter_singular",
|
||||||
|
# '': 'unknown_singular',
|
||||||
|
# '': 'female_plural',
|
||||||
|
# '': 'male_plural',
|
||||||
|
# '': 'neuter_plural',
|
||||||
|
# '': 'unknown_plural',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TypingStatus(Enum):
|
class TypingStatus(Enum):
|
||||||
"""Used to specify whether the user is typing or has stopped typing"""
|
"""Used to specify whether the user is typing or has stopped typing"""
|
||||||
|
|
||||||
@@ -65,6 +96,91 @@ class User(Thread):
|
|||||||
self.color = color
|
self.color = color
|
||||||
self.emoji = emoji
|
self.emoji = emoji
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
if data.get("profile_picture") is None:
|
||||||
|
data["profile_picture"] = {}
|
||||||
|
c_info = cls._parse_customization_info(data)
|
||||||
|
plan = None
|
||||||
|
if data.get("event_reminders") and data["event_reminders"].get("nodes"):
|
||||||
|
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
|
||||||
|
|
||||||
|
return cls(
|
||||||
|
data["id"],
|
||||||
|
url=data.get("url"),
|
||||||
|
first_name=data.get("first_name"),
|
||||||
|
last_name=data.get("last_name"),
|
||||||
|
is_friend=data.get("is_viewer_friend"),
|
||||||
|
gender=GENDERS.get(data.get("gender")),
|
||||||
|
affinity=data.get("affinity"),
|
||||||
|
nickname=c_info.get("nickname"),
|
||||||
|
color=c_info.get("color"),
|
||||||
|
emoji=c_info.get("emoji"),
|
||||||
|
own_nickname=c_info.get("own_nickname"),
|
||||||
|
photo=data["profile_picture"].get("uri"),
|
||||||
|
name=data.get("name"),
|
||||||
|
message_count=data.get("messages_count"),
|
||||||
|
plan=plan,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_thread_fetch(cls, data):
|
||||||
|
if data.get("big_image_src") is None:
|
||||||
|
data["big_image_src"] = {}
|
||||||
|
c_info = cls._parse_customization_info(data)
|
||||||
|
participants = [
|
||||||
|
node["messaging_actor"] for node in data["all_participants"]["nodes"]
|
||||||
|
]
|
||||||
|
user = next(
|
||||||
|
p for p in participants if p["id"] == data["thread_key"]["other_user_id"]
|
||||||
|
)
|
||||||
|
last_message_timestamp = None
|
||||||
|
if "last_message" in data:
|
||||||
|
last_message_timestamp = data["last_message"]["nodes"][0][
|
||||||
|
"timestamp_precise"
|
||||||
|
]
|
||||||
|
|
||||||
|
first_name = user.get("short_name")
|
||||||
|
if first_name is None:
|
||||||
|
last_name = None
|
||||||
|
else:
|
||||||
|
last_name = user.get("name").split(first_name, 1).pop().strip()
|
||||||
|
|
||||||
|
plan = None
|
||||||
|
if data.get("event_reminders") and data["event_reminders"].get("nodes"):
|
||||||
|
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
|
||||||
|
|
||||||
|
return cls(
|
||||||
|
user["id"],
|
||||||
|
url=user.get("url"),
|
||||||
|
name=user.get("name"),
|
||||||
|
first_name=first_name,
|
||||||
|
last_name=last_name,
|
||||||
|
is_friend=user.get("is_viewer_friend"),
|
||||||
|
gender=GENDERS.get(user.get("gender")),
|
||||||
|
affinity=user.get("affinity"),
|
||||||
|
nickname=c_info.get("nickname"),
|
||||||
|
color=c_info.get("color"),
|
||||||
|
emoji=c_info.get("emoji"),
|
||||||
|
own_nickname=c_info.get("own_nickname"),
|
||||||
|
photo=user["big_image_src"].get("uri"),
|
||||||
|
message_count=data.get("messages_count"),
|
||||||
|
last_message_timestamp=last_message_timestamp,
|
||||||
|
plan=plan,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_all_fetch(cls, data):
|
||||||
|
return cls(
|
||||||
|
data["id"],
|
||||||
|
first_name=data.get("firstName"),
|
||||||
|
url=data.get("uri"),
|
||||||
|
photo=data.get("thumbSrc"),
|
||||||
|
name=data.get("name"),
|
||||||
|
is_friend=data.get("is_friend"),
|
||||||
|
gender=GENDERS.get(data.get("gender")),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
class ActiveStatus(object):
|
class ActiveStatus(object):
|
||||||
@@ -74,3 +190,19 @@ class ActiveStatus(object):
|
|||||||
last_active = attr.ib(None)
|
last_active = attr.ib(None)
|
||||||
#: Whether the user is playing Messenger game now
|
#: Whether the user is playing Messenger game now
|
||||||
in_game = attr.ib(None)
|
in_game = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_chatproxy_presence(cls, id_, data):
|
||||||
|
return cls(
|
||||||
|
active=data["p"] in [2, 3] if "p" in data else None,
|
||||||
|
last_active=data.get("lat"),
|
||||||
|
in_game=int(id_) in data.get("gamers", {}),
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_buddylist_overlay(cls, data, in_game=None):
|
||||||
|
return cls(
|
||||||
|
active=data["a"] in [2, 3] if "a" in data else None,
|
||||||
|
last_active=data.get("la"),
|
||||||
|
in_game=None,
|
||||||
|
)
|
||||||
|
@@ -11,8 +11,7 @@ from os.path import basename
|
|||||||
import warnings
|
import warnings
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import aenum
|
from ._exception import FBchatException, FBchatFacebookError
|
||||||
from .models import *
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode, parse_qs, urlparse
|
from urllib.parse import urlencode, parse_qs, urlparse
|
||||||
@@ -47,45 +46,6 @@ USER_AGENTS = [
|
|||||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
|
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
LIKES = {
|
|
||||||
"large": EmojiSize.LARGE,
|
|
||||||
"medium": EmojiSize.MEDIUM,
|
|
||||||
"small": EmojiSize.SMALL,
|
|
||||||
"l": EmojiSize.LARGE,
|
|
||||||
"m": EmojiSize.MEDIUM,
|
|
||||||
"s": EmojiSize.SMALL,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
GENDERS = {
|
|
||||||
# For standard requests
|
|
||||||
0: "unknown",
|
|
||||||
1: "female_singular",
|
|
||||||
2: "male_singular",
|
|
||||||
3: "female_singular_guess",
|
|
||||||
4: "male_singular_guess",
|
|
||||||
5: "mixed",
|
|
||||||
6: "neuter_singular",
|
|
||||||
7: "unknown_singular",
|
|
||||||
8: "female_plural",
|
|
||||||
9: "male_plural",
|
|
||||||
10: "neuter_plural",
|
|
||||||
11: "unknown_plural",
|
|
||||||
# For graphql requests
|
|
||||||
"UNKNOWN": "unknown",
|
|
||||||
"FEMALE": "female_singular",
|
|
||||||
"MALE": "male_singular",
|
|
||||||
# '': 'female_singular_guess',
|
|
||||||
# '': 'male_singular_guess',
|
|
||||||
# '': 'mixed',
|
|
||||||
"NEUTER": "neuter_singular",
|
|
||||||
# '': 'unknown_singular',
|
|
||||||
# '': 'female_plural',
|
|
||||||
# '': 'male_plural',
|
|
||||||
# '': 'neuter_plural',
|
|
||||||
# '': 'unknown_plural',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class ReqUrl(object):
|
class ReqUrl(object):
|
||||||
"""A class containing all urls used by `fbchat`"""
|
"""A class containing all urls used by `fbchat`"""
|
||||||
@@ -154,6 +114,7 @@ class ReqUrl(object):
|
|||||||
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"
|
UNSEND = "https://www.facebook.com/messaging/unsend_message/?dpr=1"
|
||||||
|
FORWARD_ATTACHMENT = "https://www.facebook.com/mercury/attachments/forward/"
|
||||||
|
|
||||||
pull_channel = 0
|
pull_channel = 0
|
||||||
|
|
||||||
@@ -232,8 +193,13 @@ def generateOfflineThreadingID():
|
|||||||
|
|
||||||
|
|
||||||
def check_json(j):
|
def check_json(j):
|
||||||
if j.get("error") is None:
|
if hasattr(j.get("payload"), "get") and j["payload"].get("error"):
|
||||||
return
|
raise FBchatFacebookError(
|
||||||
|
"Error when sending request: {}".format(j["payload"]["error"]),
|
||||||
|
fb_error_code=None,
|
||||||
|
fb_error_message=j["payload"]["error"],
|
||||||
|
)
|
||||||
|
elif j.get("error"):
|
||||||
if "errorDescription" in j:
|
if "errorDescription" in j:
|
||||||
# 'errorDescription' is in the users own language!
|
# 'errorDescription' is in the users own language!
|
||||||
raise FBchatFacebookError(
|
raise FBchatFacebookError(
|
||||||
@@ -253,7 +219,8 @@ def check_json(j):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise FBchatFacebookError(
|
raise FBchatFacebookError(
|
||||||
"Error {} when sending request".format(j["error"]), fb_error_code=j["error"]
|
"Error {} when sending request".format(j["error"]),
|
||||||
|
fb_error_code=j["error"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -295,20 +262,6 @@ def get_jsmods_require(j, index):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_emojisize_from_tags(tags):
|
|
||||||
if tags is None:
|
|
||||||
return None
|
|
||||||
tmp = [tag for tag in tags if tag.startswith("hot_emoji_size:")]
|
|
||||||
if len(tmp) > 0:
|
|
||||||
try:
|
|
||||||
return LIKES[tmp[0].split(":")[1]]
|
|
||||||
except (KeyError, IndexError):
|
|
||||||
log.exception(
|
|
||||||
"Could not determine emoji size from {} - {}".format(tags, tmp)
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def require_list(list_):
|
def require_list(list_):
|
||||||
if isinstance(list_, list):
|
if isinstance(list_, list):
|
||||||
return set(list_)
|
return set(list_)
|
||||||
@@ -335,7 +288,7 @@ def get_files_from_urls(file_urls):
|
|||||||
# https://stackoverflow.com/a/37060758
|
# https://stackoverflow.com/a/37060758
|
||||||
files.append(
|
files.append(
|
||||||
(
|
(
|
||||||
basename(file_url),
|
basename(file_url).split("?")[0].split("#")[0],
|
||||||
r.content,
|
r.content,
|
||||||
r.headers.get("Content-Type") or guess_type(file_url)[0],
|
r.headers.get("Content-Type") or guess_type(file_url)[0],
|
||||||
)
|
)
|
||||||
@@ -355,19 +308,6 @@ def get_files_from_paths(filenames):
|
|||||||
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):
|
def get_url_parameters(url, *args):
|
||||||
params = parse_qs(urlparse(url).query)
|
params = parse_qs(urlparse(url).query)
|
||||||
return [params[arg][0] for arg in args if params.get(arg)]
|
return [params[arg][0] for arg in args if params.get(arg)]
|
@@ -1,765 +0,0 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
from .models import *
|
|
||||||
from .utils import *
|
|
||||||
|
|
||||||
# Shameless copy from https://stackoverflow.com/a/8730674
|
|
||||||
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
|
|
||||||
WHITESPACE = re.compile(r"[ \t\n\r]*", FLAGS)
|
|
||||||
|
|
||||||
|
|
||||||
class ConcatJSONDecoder(json.JSONDecoder):
|
|
||||||
def decode(self, s, _w=WHITESPACE.match):
|
|
||||||
s_len = len(s)
|
|
||||||
|
|
||||||
objs = []
|
|
||||||
end = 0
|
|
||||||
while end != s_len:
|
|
||||||
obj, end = self.raw_decode(s, idx=_w(s, end).end())
|
|
||||||
end = _w(s, end).end()
|
|
||||||
objs.append(obj)
|
|
||||||
return objs
|
|
||||||
|
|
||||||
|
|
||||||
# End shameless copy
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_color_to_enum(color):
|
|
||||||
if color is None:
|
|
||||||
return None
|
|
||||||
if not color:
|
|
||||||
return ThreadColor.MESSENGER_BLUE
|
|
||||||
color = color[2:] # Strip the alpha value
|
|
||||||
color_value = "#{}".format(color.lower())
|
|
||||||
return enum_extend_if_invalid(ThreadColor, color_value)
|
|
||||||
|
|
||||||
|
|
||||||
def get_customization_info(thread):
|
|
||||||
if thread is None or thread.get("customization_info") is None:
|
|
||||||
return {}
|
|
||||||
info = thread["customization_info"]
|
|
||||||
|
|
||||||
rtn = {
|
|
||||||
"emoji": info.get("emoji"),
|
|
||||||
"color": graphql_color_to_enum(info.get("outgoing_bubble_color")),
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
thread.get("thread_type") == "GROUP"
|
|
||||||
or thread.get("is_group_thread")
|
|
||||||
or thread.get("thread_key", {}).get("thread_fbid")
|
|
||||||
):
|
|
||||||
rtn["nicknames"] = {}
|
|
||||||
for k in info.get("participant_customizations", []):
|
|
||||||
rtn["nicknames"][k["participant_id"]] = k.get("nickname")
|
|
||||||
elif info.get("participant_customizations"):
|
|
||||||
uid = thread.get("thread_key", {}).get("other_user_id") or thread.get("id")
|
|
||||||
pc = info["participant_customizations"]
|
|
||||||
if len(pc) > 0:
|
|
||||||
if pc[0].get("participant_id") == uid:
|
|
||||||
rtn["nickname"] = pc[0].get("nickname")
|
|
||||||
else:
|
|
||||||
rtn["own_nickname"] = pc[0].get("nickname")
|
|
||||||
if len(pc) > 1:
|
|
||||||
if pc[1].get("participant_id") == uid:
|
|
||||||
rtn["nickname"] = pc[1].get("nickname")
|
|
||||||
else:
|
|
||||||
rtn["own_nickname"] = pc[1].get("nickname")
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_sticker(s):
|
|
||||||
if not s:
|
|
||||||
return None
|
|
||||||
sticker = Sticker(uid=s["id"])
|
|
||||||
if s.get("pack"):
|
|
||||||
sticker.pack = s["pack"].get("id")
|
|
||||||
if s.get("sprite_image"):
|
|
||||||
sticker.is_animated = True
|
|
||||||
sticker.medium_sprite_image = s["sprite_image"].get("uri")
|
|
||||||
sticker.large_sprite_image = s["sprite_image_2x"].get("uri")
|
|
||||||
sticker.frames_per_row = s.get("frames_per_row")
|
|
||||||
sticker.frames_per_col = s.get("frames_per_column")
|
|
||||||
sticker.frame_rate = s.get("frame_rate")
|
|
||||||
sticker.url = s.get("url")
|
|
||||||
sticker.width = s.get("width")
|
|
||||||
sticker.height = s.get("height")
|
|
||||||
if s.get("label"):
|
|
||||||
sticker.label = s["label"]
|
|
||||||
return sticker
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_attachment(a):
|
|
||||||
_type = a["__typename"]
|
|
||||||
if _type in ["MessageImage", "MessageAnimatedImage"]:
|
|
||||||
return ImageAttachment(
|
|
||||||
original_extension=a.get("original_extension")
|
|
||||||
or (a["filename"].split("-")[0] if a.get("filename") else None),
|
|
||||||
width=a.get("original_dimensions", {}).get("width"),
|
|
||||||
height=a.get("original_dimensions", {}).get("height"),
|
|
||||||
is_animated=_type == "MessageAnimatedImage",
|
|
||||||
thumbnail_url=a.get("thumbnail", {}).get("uri"),
|
|
||||||
preview=a.get("preview") or a.get("preview_image"),
|
|
||||||
large_preview=a.get("large_preview"),
|
|
||||||
animated_preview=a.get("animated_image"),
|
|
||||||
uid=a.get("legacy_attachment_id"),
|
|
||||||
)
|
|
||||||
elif _type == "MessageVideo":
|
|
||||||
return VideoAttachment(
|
|
||||||
width=a.get("original_dimensions", {}).get("width"),
|
|
||||||
height=a.get("original_dimensions", {}).get("height"),
|
|
||||||
duration=a.get("playable_duration_in_ms"),
|
|
||||||
preview_url=a.get("playable_url"),
|
|
||||||
small_image=a.get("chat_image"),
|
|
||||||
medium_image=a.get("inbox_image"),
|
|
||||||
large_image=a.get("large_image"),
|
|
||||||
uid=a.get("legacy_attachment_id"),
|
|
||||||
)
|
|
||||||
elif _type == "MessageAudio":
|
|
||||||
return AudioAttachment(
|
|
||||||
filename=a.get("filename"),
|
|
||||||
url=a.get("playable_url"),
|
|
||||||
duration=a.get("playable_duration_in_ms"),
|
|
||||||
audio_type=a.get("audio_type"),
|
|
||||||
)
|
|
||||||
elif _type == "MessageFile":
|
|
||||||
return FileAttachment(
|
|
||||||
url=a.get("url"),
|
|
||||||
name=a.get("filename"),
|
|
||||||
is_malicious=a.get("is_malicious"),
|
|
||||||
uid=a.get("message_file_fbid"),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return Attachment(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":
|
|
||||||
url = story.get("url")
|
|
||||||
address = get_url_parameter(get_url_parameter(url, "u"), "where1")
|
|
||||||
try:
|
|
||||||
latitude, longitude = [float(x) for x in address.split(", ")]
|
|
||||||
address = None
|
|
||||||
except ValueError:
|
|
||||||
latitude, longitude = None, None
|
|
||||||
rtn = LocationAttachment(
|
|
||||||
uid=int(story["deduplication_key"]),
|
|
||||||
latitude=latitude,
|
|
||||||
longitude=longitude,
|
|
||||||
address=address,
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
rtn.url = 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"].get("expiration_time"),
|
|
||||||
is_expired=story["target"].get("is_expired"),
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
rtn.url = story.get("url")
|
|
||||||
return rtn
|
|
||||||
elif _type in ["ExternalUrl", "Story"]:
|
|
||||||
url = story.get("url")
|
|
||||||
rtn = ShareAttachment(
|
|
||||||
uid=a.get("legacy_attachment_id"),
|
|
||||||
author=story["target"]["actors"][0]["id"]
|
|
||||||
if story["target"].get("actors")
|
|
||||||
else None,
|
|
||||||
url=url,
|
|
||||||
original_url=get_url_parameter(url, "u")
|
|
||||||
if "/l.php?u=" in url
|
|
||||||
else url,
|
|
||||||
title=story["title_with_entities"].get("text"),
|
|
||||||
description=story["description"].get("text")
|
|
||||||
if story.get("description")
|
|
||||||
else None,
|
|
||||||
source=story["source"].get("text"),
|
|
||||||
attachments=[
|
|
||||||
graphql_to_subattachment(attachment)
|
|
||||||
for attachment in story.get("subattachments")
|
|
||||||
],
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.original_image_url = (
|
|
||||||
get_url_parameter(rtn.image_url, "url")
|
|
||||||
if "/safe_image.php" in rtn.image_url
|
|
||||||
else rtn.image_url
|
|
||||||
)
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
return rtn
|
|
||||||
else:
|
|
||||||
return UnsentMessage(uid=a.get("legacy_attachment_id"))
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_subattachment(a):
|
|
||||||
_type = a["target"]["__typename"]
|
|
||||||
if _type == "Video":
|
|
||||||
media = a["media"]
|
|
||||||
return VideoAttachment(
|
|
||||||
duration=media.get("playable_duration_in_ms"),
|
|
||||||
preview_url=media.get("playable_url"),
|
|
||||||
medium_image=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):
|
|
||||||
rtn = Poll(
|
|
||||||
title=a.get("title") if a.get("title") else a.get("text"),
|
|
||||||
options=[graphql_to_poll_option(m) for m in a.get("options")],
|
|
||||||
)
|
|
||||||
rtn.uid = int(a["id"])
|
|
||||||
rtn.options_count = a.get("total_count")
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_poll_option(a):
|
|
||||||
if a.get("viewer_has_voted") is None:
|
|
||||||
vote = None
|
|
||||||
elif isinstance(a["viewer_has_voted"], bool):
|
|
||||||
vote = a["viewer_has_voted"]
|
|
||||||
else:
|
|
||||||
vote = a["viewer_has_voted"] == "true"
|
|
||||||
rtn = PollOption(text=a.get("text"), vote=vote)
|
|
||||||
rtn.uid = int(a["id"])
|
|
||||||
rtn.voters = (
|
|
||||||
[m.get("node").get("id") for m in a.get("voters").get("edges")]
|
|
||||||
if isinstance(a.get("voters"), dict)
|
|
||||||
else a.get("voters")
|
|
||||||
)
|
|
||||||
rtn.votes_count = (
|
|
||||||
a.get("voters").get("count")
|
|
||||||
if isinstance(a.get("voters"), dict)
|
|
||||||
else a.get("total_count")
|
|
||||||
)
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_plan(a):
|
|
||||||
if a.get("event_members"):
|
|
||||||
rtn = Plan(
|
|
||||||
time=a.get("event_time"),
|
|
||||||
title=a.get("title"),
|
|
||||||
location=a.get("location_name"),
|
|
||||||
)
|
|
||||||
if a.get("location_id") != 0:
|
|
||||||
rtn.location_id = str(a.get("location_id"))
|
|
||||||
rtn.uid = a.get("oid")
|
|
||||||
rtn.author_id = a.get("creator_id")
|
|
||||||
guests = a.get("event_members")
|
|
||||||
rtn.going = [uid for uid in guests if guests[uid] == "GOING"]
|
|
||||||
rtn.declined = [uid for uid in guests if guests[uid] == "DECLINED"]
|
|
||||||
rtn.invited = [uid for uid in guests if guests[uid] == "INVITED"]
|
|
||||||
return rtn
|
|
||||||
elif a.get("id") is None:
|
|
||||||
rtn = Plan(
|
|
||||||
time=a.get("event_time"),
|
|
||||||
title=a.get("event_title"),
|
|
||||||
location=a.get("event_location_name"),
|
|
||||||
location_id=a.get("event_location_id"),
|
|
||||||
)
|
|
||||||
rtn.uid = a.get("event_id")
|
|
||||||
rtn.author_id = a.get("event_creator_id")
|
|
||||||
guests = json.loads(a.get("guest_state_list"))
|
|
||||||
else:
|
|
||||||
rtn = Plan(
|
|
||||||
time=a.get("time"),
|
|
||||||
title=a.get("event_title"),
|
|
||||||
location=a.get("location_name"),
|
|
||||||
)
|
|
||||||
rtn.uid = a.get("id")
|
|
||||||
rtn.author_id = a.get("lightweight_event_creator").get("id")
|
|
||||||
guests = a.get("event_reminder_members").get("edges")
|
|
||||||
rtn.going = [
|
|
||||||
m.get("node").get("id") for m in guests if m.get("guest_list_state") == "GOING"
|
|
||||||
]
|
|
||||||
rtn.declined = [
|
|
||||||
m.get("node").get("id")
|
|
||||||
for m in guests
|
|
||||||
if m.get("guest_list_state") == "DECLINED"
|
|
||||||
]
|
|
||||||
rtn.invited = [
|
|
||||||
m.get("node").get("id")
|
|
||||||
for m in guests
|
|
||||||
if m.get("guest_list_state") == "INVITED"
|
|
||||||
]
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_quick_reply(q, is_response=False):
|
|
||||||
data = dict()
|
|
||||||
_type = q.get("content_type").lower()
|
|
||||||
if q.get("payload"):
|
|
||||||
data["payload"] = q["payload"]
|
|
||||||
if q.get("data"):
|
|
||||||
data["data"] = q["data"]
|
|
||||||
if q.get("image_url") and _type is not QuickReplyLocation._type:
|
|
||||||
data["image_url"] = q["image_url"]
|
|
||||||
data["is_response"] = is_response
|
|
||||||
if _type == QuickReplyText._type:
|
|
||||||
if q.get("title") is not None:
|
|
||||||
data["title"] = q["title"]
|
|
||||||
rtn = QuickReplyText(**data)
|
|
||||||
elif _type == QuickReplyLocation._type:
|
|
||||||
rtn = QuickReplyLocation(**data)
|
|
||||||
elif _type == QuickReplyPhoneNumber._type:
|
|
||||||
rtn = QuickReplyPhoneNumber(**data)
|
|
||||||
elif _type == QuickReplyEmail._type:
|
|
||||||
rtn = QuickReplyEmail(**data)
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_message(message):
|
|
||||||
if message.get("message_sender") is None:
|
|
||||||
message["message_sender"] = {}
|
|
||||||
if message.get("message") is None:
|
|
||||||
message["message"] = {}
|
|
||||||
rtn = Message(
|
|
||||||
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", [])
|
|
||||||
],
|
|
||||||
emoji_size=get_emojisize_from_tags(message.get("tags_list")),
|
|
||||||
sticker=graphql_to_sticker(message.get("sticker")),
|
|
||||||
)
|
|
||||||
rtn.uid = str(message.get("message_id"))
|
|
||||||
rtn.author = str(message.get("message_sender").get("id"))
|
|
||||||
rtn.timestamp = message.get("timestamp_precise")
|
|
||||||
rtn.unsent = False
|
|
||||||
if message.get("unread") is not None:
|
|
||||||
rtn.is_read = not message["unread"]
|
|
||||||
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:
|
|
||||||
rtn.attachments = [
|
|
||||||
graphql_to_attachment(attachment)
|
|
||||||
for attachment in message["blob_attachments"]
|
|
||||||
]
|
|
||||||
if message.get("platform_xmd_encoded"):
|
|
||||||
quick_replies = json.loads(message["platform_xmd_encoded"]).get("quick_replies")
|
|
||||||
if isinstance(quick_replies, list):
|
|
||||||
rtn.quick_replies = [graphql_to_quick_reply(q) for q in quick_replies]
|
|
||||||
elif isinstance(quick_replies, dict):
|
|
||||||
rtn.quick_replies = [
|
|
||||||
graphql_to_quick_reply(quick_replies, is_response=True)
|
|
||||||
]
|
|
||||||
if message.get("extensible_attachment") is not None:
|
|
||||||
attachment = graphql_to_extensible_attachment(message["extensible_attachment"])
|
|
||||||
if isinstance(attachment, UnsentMessage):
|
|
||||||
rtn.unsent = True
|
|
||||||
elif attachment:
|
|
||||||
rtn.attachments.append(attachment)
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_user(user):
|
|
||||||
if user.get("profile_picture") is None:
|
|
||||||
user["profile_picture"] = {}
|
|
||||||
c_info = get_customization_info(user)
|
|
||||||
plan = None
|
|
||||||
if user.get("event_reminders"):
|
|
||||||
plan = (
|
|
||||||
graphql_to_plan(user["event_reminders"]["nodes"][0])
|
|
||||||
if user["event_reminders"].get("nodes")
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
return User(
|
|
||||||
user["id"],
|
|
||||||
url=user.get("url"),
|
|
||||||
first_name=user.get("first_name"),
|
|
||||||
last_name=user.get("last_name"),
|
|
||||||
is_friend=user.get("is_viewer_friend"),
|
|
||||||
gender=GENDERS.get(user.get("gender")),
|
|
||||||
affinity=user.get("affinity"),
|
|
||||||
nickname=c_info.get("nickname"),
|
|
||||||
color=c_info.get("color"),
|
|
||||||
emoji=c_info.get("emoji"),
|
|
||||||
own_nickname=c_info.get("own_nickname"),
|
|
||||||
photo=user["profile_picture"].get("uri"),
|
|
||||||
name=user.get("name"),
|
|
||||||
message_count=user.get("messages_count"),
|
|
||||||
plan=plan,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_thread(thread):
|
|
||||||
if thread["thread_type"] == "GROUP":
|
|
||||||
return graphql_to_group(thread)
|
|
||||||
elif thread["thread_type"] == "ONE_TO_ONE":
|
|
||||||
if thread.get("big_image_src") is None:
|
|
||||||
thread["big_image_src"] = {}
|
|
||||||
c_info = get_customization_info(thread)
|
|
||||||
participants = [
|
|
||||||
node["messaging_actor"] for node in thread["all_participants"]["nodes"]
|
|
||||||
]
|
|
||||||
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"
|
|
||||||
]
|
|
||||||
|
|
||||||
first_name = user.get("short_name")
|
|
||||||
if first_name is None:
|
|
||||||
last_name = None
|
|
||||||
else:
|
|
||||||
last_name = user.get("name").split(first_name, 1).pop().strip()
|
|
||||||
|
|
||||||
plan = None
|
|
||||||
if thread.get("event_reminders"):
|
|
||||||
plan = (
|
|
||||||
graphql_to_plan(thread["event_reminders"]["nodes"][0])
|
|
||||||
if thread["event_reminders"].get("nodes")
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
return User(
|
|
||||||
user["id"],
|
|
||||||
url=user.get("url"),
|
|
||||||
name=user.get("name"),
|
|
||||||
first_name=first_name,
|
|
||||||
last_name=last_name,
|
|
||||||
is_friend=user.get("is_viewer_friend"),
|
|
||||||
gender=GENDERS.get(user.get("gender")),
|
|
||||||
affinity=user.get("affinity"),
|
|
||||||
nickname=c_info.get("nickname"),
|
|
||||||
color=c_info.get("color"),
|
|
||||||
emoji=c_info.get("emoji"),
|
|
||||||
own_nickname=c_info.get("own_nickname"),
|
|
||||||
photo=user["big_image_src"].get("uri"),
|
|
||||||
message_count=thread.get("messages_count"),
|
|
||||||
last_message_timestamp=last_message_timestamp,
|
|
||||||
plan=plan,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise FBchatException(
|
|
||||||
"Unknown thread type: {}, with data: {}".format(
|
|
||||||
thread.get("thread_type"), thread
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_group(group):
|
|
||||||
if group.get("image") is None:
|
|
||||||
group["image"] = {}
|
|
||||||
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"]
|
|
||||||
plan = None
|
|
||||||
if group.get("event_reminders"):
|
|
||||||
plan = (
|
|
||||||
graphql_to_plan(group["event_reminders"]["nodes"][0])
|
|
||||||
if group["event_reminders"].get("nodes")
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
return Group(
|
|
||||||
group["thread_key"]["thread_fbid"],
|
|
||||||
participants=set(
|
|
||||||
[
|
|
||||||
node["messaging_actor"]["id"]
|
|
||||||
for node in group["all_participants"]["nodes"]
|
|
||||||
]
|
|
||||||
),
|
|
||||||
nicknames=c_info.get("nicknames"),
|
|
||||||
color=c_info.get("color"),
|
|
||||||
emoji=c_info.get("emoji"),
|
|
||||||
admins=set([node.get("id") for node in group.get("thread_admins")]),
|
|
||||||
approval_mode=bool(group.get("approval_mode"))
|
|
||||||
if group.get("approval_mode") is not None
|
|
||||||
else None,
|
|
||||||
approval_requests=set(
|
|
||||||
node["requester"]["id"] for node in group["group_approval_queue"]["nodes"]
|
|
||||||
)
|
|
||||||
if group.get("group_approval_queue")
|
|
||||||
else None,
|
|
||||||
join_link=group["joinable_mode"].get("link"),
|
|
||||||
photo=group["image"].get("uri"),
|
|
||||||
name=group.get("name"),
|
|
||||||
message_count=group.get("messages_count"),
|
|
||||||
last_message_timestamp=last_message_timestamp,
|
|
||||||
plan=plan,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_page(page):
|
|
||||||
if page.get("profile_picture") is None:
|
|
||||||
page["profile_picture"] = {}
|
|
||||||
if page.get("city") is None:
|
|
||||||
page["city"] = {}
|
|
||||||
plan = None
|
|
||||||
if page.get("event_reminders"):
|
|
||||||
plan = (
|
|
||||||
graphql_to_plan(page["event_reminders"]["nodes"][0])
|
|
||||||
if page["event_reminders"].get("nodes")
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
return Page(
|
|
||||||
page["id"],
|
|
||||||
url=page.get("url"),
|
|
||||||
city=page.get("city").get("name"),
|
|
||||||
category=page.get("category_type"),
|
|
||||||
photo=page["profile_picture"].get("uri"),
|
|
||||||
name=page.get("name"),
|
|
||||||
message_count=page.get("messages_count"),
|
|
||||||
plan=plan,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_queries_to_json(*queries):
|
|
||||||
"""
|
|
||||||
Queries should be a list of GraphQL objects
|
|
||||||
"""
|
|
||||||
rtn = {}
|
|
||||||
for i, query in enumerate(queries):
|
|
||||||
rtn["q{}".format(i)] = query.value
|
|
||||||
return json.dumps(rtn)
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_response_to_json(content):
|
|
||||||
content = strip_to_json(content) # Usually only needed in some error cases
|
|
||||||
try:
|
|
||||||
j = json.loads(content, cls=ConcatJSONDecoder)
|
|
||||||
except Exception:
|
|
||||||
raise FBchatException("Error while parsing JSON: {}".format(repr(content)))
|
|
||||||
|
|
||||||
rtn = [None] * (len(j))
|
|
||||||
for x in j:
|
|
||||||
if "error_results" in x:
|
|
||||||
del rtn[-1]
|
|
||||||
continue
|
|
||||||
check_json(x)
|
|
||||||
[(key, value)] = x.items()
|
|
||||||
check_json(value)
|
|
||||||
if "response" in value:
|
|
||||||
rtn[int(key[1:])] = value["response"]
|
|
||||||
else:
|
|
||||||
rtn[int(key[1:])] = value["data"]
|
|
||||||
|
|
||||||
log.debug(rtn)
|
|
||||||
|
|
||||||
return rtn
|
|
||||||
|
|
||||||
|
|
||||||
class GraphQL(object):
|
|
||||||
def __init__(self, query=None, doc_id=None, params=None):
|
|
||||||
if params is None:
|
|
||||||
params = {}
|
|
||||||
if query is not None:
|
|
||||||
self.value = {"priority": 0, "q": query, "query_params": params}
|
|
||||||
elif doc_id is not None:
|
|
||||||
self.value = {"doc_id": doc_id, "query_params": params}
|
|
||||||
else:
|
|
||||||
raise FBchatUserError("A query or doc_id must be specified")
|
|
||||||
|
|
||||||
FRAGMENT_USER = """
|
|
||||||
QueryFragment User: User {
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
profile_picture.width(<pic_size>).height(<pic_size>) {
|
|
||||||
uri
|
|
||||||
},
|
|
||||||
is_viewer_friend,
|
|
||||||
url,
|
|
||||||
gender,
|
|
||||||
viewer_affinity
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
FRAGMENT_GROUP = """
|
|
||||||
QueryFragment Group: MessageThread {
|
|
||||||
name,
|
|
||||||
thread_key {
|
|
||||||
thread_fbid
|
|
||||||
},
|
|
||||||
image {
|
|
||||||
uri
|
|
||||||
},
|
|
||||||
is_group_thread,
|
|
||||||
all_participants {
|
|
||||||
nodes {
|
|
||||||
messaging_actor {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
customization_info {
|
|
||||||
participant_customizations {
|
|
||||||
participant_id,
|
|
||||||
nickname
|
|
||||||
},
|
|
||||||
outgoing_bubble_color,
|
|
||||||
emoji
|
|
||||||
},
|
|
||||||
thread_admins {
|
|
||||||
id
|
|
||||||
},
|
|
||||||
group_approval_queue {
|
|
||||||
nodes {
|
|
||||||
requester {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
approval_mode,
|
|
||||||
joinable_mode {
|
|
||||||
mode,
|
|
||||||
link
|
|
||||||
},
|
|
||||||
event_reminders {
|
|
||||||
nodes {
|
|
||||||
id,
|
|
||||||
lightweight_event_creator {
|
|
||||||
id
|
|
||||||
},
|
|
||||||
time,
|
|
||||||
location_name,
|
|
||||||
event_title,
|
|
||||||
event_reminder_members {
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
id
|
|
||||||
},
|
|
||||||
guest_list_state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
FRAGMENT_PAGE = """
|
|
||||||
QueryFragment Page: Page {
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
profile_picture.width(32).height(32) {
|
|
||||||
uri
|
|
||||||
},
|
|
||||||
url,
|
|
||||||
category_type,
|
|
||||||
city {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
SEARCH_USER = (
|
|
||||||
"""
|
|
||||||
Query SearchUser(<search> = '', <limit> = 10) {
|
|
||||||
entities_named(<search>) {
|
|
||||||
search_results.of_type(user).first(<limit>) as users {
|
|
||||||
nodes {
|
|
||||||
@User
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
+ FRAGMENT_USER
|
|
||||||
)
|
|
||||||
|
|
||||||
SEARCH_GROUP = (
|
|
||||||
"""
|
|
||||||
Query SearchGroup(<search> = '', <limit> = 10, <pic_size> = 32) {
|
|
||||||
viewer() {
|
|
||||||
message_threads.with_thread_name(<search>).last(<limit>) as groups {
|
|
||||||
nodes {
|
|
||||||
@Group
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
+ FRAGMENT_GROUP
|
|
||||||
)
|
|
||||||
|
|
||||||
SEARCH_PAGE = (
|
|
||||||
"""
|
|
||||||
Query SearchPage(<search> = '', <limit> = 10) {
|
|
||||||
entities_named(<search>) {
|
|
||||||
search_results.of_type(page).first(<limit>) as pages {
|
|
||||||
nodes {
|
|
||||||
@Page
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
+ FRAGMENT_PAGE
|
|
||||||
)
|
|
||||||
|
|
||||||
SEARCH_THREAD = (
|
|
||||||
"""
|
|
||||||
Query SearchThread(<search> = '', <limit> = 10) {
|
|
||||||
entities_named(<search>) {
|
|
||||||
search_results.first(<limit>) as threads {
|
|
||||||
nodes {
|
|
||||||
__typename,
|
|
||||||
@User,
|
|
||||||
@Group,
|
|
||||||
@Page
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
+ FRAGMENT_USER
|
|
||||||
+ FRAGMENT_GROUP
|
|
||||||
+ FRAGMENT_PAGE
|
|
||||||
)
|
|
@@ -26,4 +26,4 @@ from ._quick_reply import (
|
|||||||
QuickReplyEmail,
|
QuickReplyEmail,
|
||||||
)
|
)
|
||||||
from ._poll import Poll, PollOption
|
from ._poll import Poll, PollOption
|
||||||
from ._plan import Plan
|
from ._plan import GuestStatus, Plan
|
||||||
|
Reference in New Issue
Block a user