Split overly nested calls
This commit is contained in:
@@ -20,6 +20,13 @@ except ImportError:
|
|||||||
from urlparse import urlparse, parse_qs
|
from urlparse import urlparse, parse_qs
|
||||||
|
|
||||||
|
|
||||||
|
_ACONTEXT = {
|
||||||
|
"action_history": [
|
||||||
|
{"surface": "messenger_chat_tab", "mechanism": "messenger_composer"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
"""A client for the Facebook Chat (Messenger).
|
"""A client for the Facebook Chat (Messenger).
|
||||||
|
|
||||||
@@ -266,16 +273,12 @@ class Client(object):
|
|||||||
:return: A tuple containing json graphql queries
|
:return: A tuple containing json graphql queries
|
||||||
:rtype: tuple
|
:rtype: tuple
|
||||||
"""
|
"""
|
||||||
return tuple(
|
data = {
|
||||||
self._post(
|
|
||||||
{
|
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"response_format": "json",
|
"response_format": "json",
|
||||||
"queries": graphql_queries_to_json(*queries),
|
"queries": graphql_queries_to_json(*queries),
|
||||||
},
|
}
|
||||||
as_graphql=True,
|
return tuple(self._post(data, as_graphql=True))
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def graphql_request(self, query):
|
def graphql_request(self, query):
|
||||||
"""
|
"""
|
||||||
@@ -578,15 +581,8 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def _forcedFetch(self, thread_id, mid):
|
def _forcedFetch(self, thread_id, mid):
|
||||||
j = self.graphql_request(
|
params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}}
|
||||||
GraphQL(
|
return self.graphql_request(GraphQL(doc_id="1768656253222505", params=params))
|
||||||
doc_id="1768656253222505",
|
|
||||||
params={
|
|
||||||
"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return j
|
|
||||||
|
|
||||||
def fetchThreads(self, thread_location, before=None, after=None, limit=None):
|
def fetchThreads(self, thread_location, before=None, after=None, limit=None):
|
||||||
"""
|
"""
|
||||||
@@ -715,9 +711,8 @@ class Client(object):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
j = self.graphql_request(
|
params = {"search": name, "limit": limit}
|
||||||
GraphQL(query=GraphQL.SEARCH_USER, params={"search": name, "limit": limit})
|
j = self.graphql_request(GraphQL(query=GraphQL.SEARCH_USER, params=params))
|
||||||
)
|
|
||||||
|
|
||||||
return [graphql_to_user(node) for node in j[name]["users"]["nodes"]]
|
return [graphql_to_user(node) for node in j[name]["users"]["nodes"]]
|
||||||
|
|
||||||
@@ -730,9 +725,8 @@ class Client(object):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
j = self.graphql_request(
|
params = {"search": name, "limit": limit}
|
||||||
GraphQL(query=GraphQL.SEARCH_PAGE, params={"search": name, "limit": limit})
|
j = self.graphql_request(GraphQL(query=GraphQL.SEARCH_PAGE, params=params))
|
||||||
)
|
|
||||||
|
|
||||||
return [graphql_to_page(node) for node in j[name]["pages"]["nodes"]]
|
return [graphql_to_page(node) for node in j[name]["pages"]["nodes"]]
|
||||||
|
|
||||||
@@ -746,9 +740,8 @@ class Client(object):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
j = self.graphql_request(
|
params = {"search": name, "limit": limit}
|
||||||
GraphQL(query=GraphQL.SEARCH_GROUP, params={"search": name, "limit": limit})
|
j = self.graphql_request(GraphQL(query=GraphQL.SEARCH_GROUP, params=params))
|
||||||
)
|
|
||||||
|
|
||||||
return [graphql_to_group(node) for node in j["viewer"]["groups"]["nodes"]]
|
return [graphql_to_group(node) for node in j["viewer"]["groups"]["nodes"]]
|
||||||
|
|
||||||
@@ -762,11 +755,8 @@ class Client(object):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
j = self.graphql_request(
|
params = {"search": name, "limit": limit}
|
||||||
GraphQL(
|
j = self.graphql_request(GraphQL(query=GraphQL.SEARCH_THREAD, params=params))
|
||||||
query=GraphQL.SEARCH_THREAD, params={"search": name, "limit": limit}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
rtn = []
|
rtn = []
|
||||||
for node in j[name]["threads"]["nodes"]:
|
for node in j[name]["threads"]["nodes"]:
|
||||||
@@ -993,18 +983,14 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
queries = []
|
queries = []
|
||||||
for thread_id in thread_ids:
|
for thread_id in thread_ids:
|
||||||
queries.append(
|
params = {
|
||||||
GraphQL(
|
|
||||||
doc_id="2147762685294928",
|
|
||||||
params={
|
|
||||||
"id": thread_id,
|
"id": thread_id,
|
||||||
"message_limit": 0,
|
"message_limit": 0,
|
||||||
"load_messages": False,
|
"load_messages": False,
|
||||||
"load_read_receipts": False,
|
"load_read_receipts": False,
|
||||||
"before": None,
|
"before": None,
|
||||||
},
|
}
|
||||||
)
|
queries.append(GraphQL(doc_id="2147762685294928", params=params))
|
||||||
)
|
|
||||||
|
|
||||||
j = self.graphql_requests(*queries)
|
j = self.graphql_requests(*queries)
|
||||||
|
|
||||||
@@ -1062,30 +1048,24 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
thread_id, thread_type = self._getThread(thread_id, None)
|
thread_id, thread_type = self._getThread(thread_id, None)
|
||||||
|
|
||||||
j = self.graphql_request(
|
params = {
|
||||||
GraphQL(
|
|
||||||
doc_id="1386147188135407",
|
|
||||||
params={
|
|
||||||
"id": thread_id,
|
"id": thread_id,
|
||||||
"message_limit": limit,
|
"message_limit": limit,
|
||||||
"load_messages": True,
|
"load_messages": True,
|
||||||
"load_read_receipts": True,
|
"load_read_receipts": True,
|
||||||
"before": before,
|
"before": before,
|
||||||
},
|
}
|
||||||
)
|
j = self.graphql_request(GraphQL(doc_id="1386147188135407", params=params))
|
||||||
)
|
|
||||||
|
|
||||||
if j.get("message_thread") is None:
|
if j.get("message_thread") is None:
|
||||||
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
|
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
|
||||||
|
|
||||||
messages = list(
|
messages = [
|
||||||
reversed(
|
|
||||||
[
|
|
||||||
graphql_to_message(message)
|
graphql_to_message(message)
|
||||||
for message in j["message_thread"]["messages"]["nodes"]
|
for message in j["message_thread"]["messages"]["nodes"]
|
||||||
]
|
]
|
||||||
)
|
messages.reverse()
|
||||||
)
|
|
||||||
read_receipts = j["message_thread"]["read_receipts"]["nodes"]
|
read_receipts = j["message_thread"]["read_receipts"]["nodes"]
|
||||||
|
|
||||||
for message in messages:
|
for message in messages:
|
||||||
@@ -1123,18 +1103,14 @@ class Client(object):
|
|||||||
else:
|
else:
|
||||||
raise FBchatUserError('"thread_location" must be a value of ThreadLocation')
|
raise FBchatUserError('"thread_location" must be a value of ThreadLocation')
|
||||||
|
|
||||||
j = self.graphql_request(
|
params = {
|
||||||
GraphQL(
|
|
||||||
doc_id="1349387578499440",
|
|
||||||
params={
|
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
"tags": [loc_str],
|
"tags": [loc_str],
|
||||||
"before": before,
|
"before": before,
|
||||||
"includeDeliveryReceipts": True,
|
"includeDeliveryReceipts": True,
|
||||||
"includeSeqID": False,
|
"includeSeqID": False,
|
||||||
},
|
}
|
||||||
)
|
j = self.graphql_request(GraphQL(doc_id="1349387578499440", params=params))
|
||||||
)
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
graphql_to_thread(node) for node in j["viewer"]["message_threads"]["nodes"]
|
graphql_to_thread(node) for node in j["viewer"]["message_threads"]["nodes"]
|
||||||
@@ -1186,8 +1162,9 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
image_id = str(image_id)
|
image_id = str(image_id)
|
||||||
j = check_request(
|
data = {"photo_id": str(image_id)}
|
||||||
self._get(ReqUrl.ATTACHMENT_PHOTO, query={"photo_id": str(image_id)})
|
j = self._get(
|
||||||
|
ReqUrl.ATTACHMENT_PHOTO, query=data, fix_request=True, as_json=True
|
||||||
)
|
)
|
||||||
|
|
||||||
url = get_jsmods_require(j, 3)
|
url = get_jsmods_require(j, 3)
|
||||||
@@ -1847,11 +1824,7 @@ class Client(object):
|
|||||||
|
|
||||||
user_ids = list(require_list(user_ids))
|
user_ids = list(require_list(user_ids))
|
||||||
|
|
||||||
j = self.graphql_request(
|
data = {
|
||||||
GraphQL(
|
|
||||||
doc_id="1574519202665847",
|
|
||||||
params={
|
|
||||||
"data": {
|
|
||||||
"client_mutation_id": "0",
|
"client_mutation_id": "0",
|
||||||
"actor_id": self.uid,
|
"actor_id": self.uid,
|
||||||
"thread_fbid": thread_id,
|
"thread_fbid": thread_id,
|
||||||
@@ -1859,8 +1832,8 @@ class Client(object):
|
|||||||
"response": "ACCEPT" if approve else "DENY",
|
"response": "ACCEPT" if approve else "DENY",
|
||||||
"surface": "ADMIN_MODEL_APPROVAL_CENTER",
|
"surface": "ADMIN_MODEL_APPROVAL_CENTER",
|
||||||
}
|
}
|
||||||
},
|
j = self.graphql_request(
|
||||||
)
|
GraphQL(doc_id="1574519202665847", params={"data": data})
|
||||||
)
|
)
|
||||||
|
|
||||||
def acceptUsersToGroup(self, user_ids, thread_id=None):
|
def acceptUsersToGroup(self, user_ids, thread_id=None):
|
||||||
@@ -2010,19 +1983,13 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {
|
||||||
"doc_id": 1491398900900362,
|
|
||||||
"variables": json.dumps(
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"action": "ADD_REACTION" if reaction else "REMOVE_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 if reaction else None,
|
"reaction": reaction.value if reaction else None,
|
||||||
}
|
}
|
||||||
}
|
data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})}
|
||||||
),
|
|
||||||
}
|
|
||||||
self._post(self.req_url.MESSAGE_REACTION, data, fix_request=True, as_json=True)
|
self._post(self.req_url.MESSAGE_REACTION, data, fix_request=True, as_json=True)
|
||||||
|
|
||||||
def createPlan(self, plan, thread_id=None):
|
def createPlan(self, plan, thread_id=None):
|
||||||
@@ -2036,23 +2003,16 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
thread_id, thread_type = self._getThread(thread_id, None)
|
thread_id, thread_type = self._getThread(thread_id, None)
|
||||||
|
|
||||||
full_data = {
|
data = {
|
||||||
"event_type": "EVENT",
|
"event_type": "EVENT",
|
||||||
"event_time": plan.time,
|
"event_time": plan.time,
|
||||||
"title": plan.title,
|
"title": plan.title,
|
||||||
"thread_id": thread_id,
|
"thread_id": thread_id,
|
||||||
"location_id": plan.location_id or "",
|
"location_id": plan.location_id or "",
|
||||||
"location_name": plan.location or "",
|
"location_name": plan.location or "",
|
||||||
"acontext": {
|
"acontext": _ACONTEXT,
|
||||||
"action_history": [
|
|
||||||
{"surface": "messenger_chat_tab", "mechanism": "messenger_composer"}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
j = self._post(self.req_url.PLAN_CREATE, data, fix_request=True, as_json=True)
|
||||||
j = self._post(
|
|
||||||
self.req_url.PLAN_CREATE, full_data, fix_request=True, as_json=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def editPlan(self, plan, new_plan):
|
def editPlan(self, plan, new_plan):
|
||||||
"""
|
"""
|
||||||
@@ -2063,22 +2023,16 @@ class Client(object):
|
|||||||
:type plan: models.Plan
|
:type plan: models.Plan
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
full_data = {
|
data = {
|
||||||
"event_reminder_id": plan.uid,
|
"event_reminder_id": plan.uid,
|
||||||
"delete": "false",
|
"delete": "false",
|
||||||
"date": new_plan.time,
|
"date": new_plan.time,
|
||||||
"location_name": new_plan.location or "",
|
"location_name": new_plan.location or "",
|
||||||
"location_id": new_plan.location_id or "",
|
"location_id": new_plan.location_id or "",
|
||||||
"title": new_plan.title,
|
"title": new_plan.title,
|
||||||
"acontext": {
|
"acontext": _ACONTEXT,
|
||||||
"action_history": [
|
|
||||||
{"surface": "messenger_chat_tab", "mechanism": "reminder_banner"}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
j = self._post(
|
j = self._post(self.req_url.PLAN_CHANGE, data, fix_request=True, as_json=True)
|
||||||
self.req_url.PLAN_CHANGE, full_data, fix_request=True, as_json=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def deletePlan(self, plan):
|
def deletePlan(self, plan):
|
||||||
"""
|
"""
|
||||||
@@ -2087,18 +2041,8 @@ class Client(object):
|
|||||||
:param plan: Plan to delete
|
:param plan: Plan to delete
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
full_data = {
|
data = {"event_reminder_id": plan.uid, "delete": "true", "acontext": _ACONTEXT}
|
||||||
"event_reminder_id": plan.uid,
|
j = self._post(self.req_url.PLAN_CHANGE, data, fix_request=True, as_json=True)
|
||||||
"delete": "true",
|
|
||||||
"acontext": {
|
|
||||||
"action_history": [
|
|
||||||
{"surface": "messenger_chat_tab", "mechanism": "reminder_banner"}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
j = self._post(
|
|
||||||
self.req_url.PLAN_CHANGE, full_data, fix_request=True, as_json=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def changePlanParticipation(self, plan, take_part=True):
|
def changePlanParticipation(self, plan, take_part=True):
|
||||||
"""
|
"""
|
||||||
@@ -2108,29 +2052,21 @@ class Client(object):
|
|||||||
:param take_part: Whether to take part in the plan
|
:param take_part: Whether to take part in the plan
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
full_data = {
|
data = {
|
||||||
"event_reminder_id": plan.uid,
|
"event_reminder_id": plan.uid,
|
||||||
"guest_state": "GOING" if take_part else "DECLINED",
|
"guest_state": "GOING" if take_part else "DECLINED",
|
||||||
"acontext": {
|
"acontext": _ACONTEXT,
|
||||||
"action_history": [
|
|
||||||
{"surface": "messenger_chat_tab", "mechanism": "reminder_banner"}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
j = self._post(
|
j = self._post(
|
||||||
self.req_url.PLAN_PARTICIPATION, full_data, fix_request=True, as_json=True
|
self.req_url.PLAN_PARTICIPATION, data, fix_request=True, as_json=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def eventReminder(self, thread_id, time, title, location="", location_id=""):
|
def eventReminder(self, thread_id, time, title, location="", location_id=""):
|
||||||
"""
|
"""
|
||||||
Deprecated. Use :func:`fbchat.Client.createPlan` instead
|
Deprecated. Use :func:`fbchat.Client.createPlan` instead
|
||||||
"""
|
"""
|
||||||
self.createPlan(
|
plan = Plan(time=time, title=title, location=location, location_id=location_id)
|
||||||
plan=Plan(
|
self.createPlan(plan=plan, thread_id=thread_id)
|
||||||
time=time, title=title, location=location, location_id=location_id
|
|
||||||
),
|
|
||||||
thread_id=thread_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
def createPoll(self, poll, thread_id=None):
|
def createPoll(self, poll, thread_id=None):
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user