From 1e3ca02e933934dd5c43204c8b659918882e8aac Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sun, 13 Oct 2019 21:45:43 +1100 Subject: [PATCH 1/2] abstracted doc ids using enums --- fbchat/_client.py | 14 +++++++------- fbchat/_graphql.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index fa1aab63..031be4dd 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -198,7 +198,7 @@ def logout(self): def _forced_fetch(self, thread_id, mid): params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}} - j, = self.graphql_requests(_graphql.from_doc_id("1768656253222505", params)) + j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_INFO, params)) return j def fetch_threads(self, thread_location, before=None, after=None, limit=None): @@ -615,7 +615,7 @@ def fetch_thread_info(self, *thread_ids): "load_read_receipts": False, "before": None, } - queries.append(_graphql.from_doc_id("2147762685294928", params)) + queries.append(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_INFO, params)) j = self.graphql_requests(*queries) @@ -679,7 +679,7 @@ def fetch_thread_messages(self, thread_id=None, limit=20, before=None): "load_read_receipts": True, "before": _util.datetime_to_millis(before) if before else None, } - j, = self.graphql_requests(_graphql.from_doc_id("1860982147341344", params)) + j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_MESSAGES, params)) if j.get("message_thread") is None: raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j)) @@ -733,7 +733,7 @@ def fetch_thread_list( "includeDeliveryReceipts": True, "includeSeqID": False, } - j, = self.graphql_requests(_graphql.from_doc_id("1349387578499440", params)) + j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_LIST, params)) rtn = [] for node in j["viewer"]["message_threads"]["nodes"]: @@ -853,7 +853,7 @@ def fetch_plan_info(self, plan_id): return Plan._from_fetch(j) def _get_private_data(self): - j, = self.graphql_requests(_graphql.from_doc_id("1868889766468115", {})) + j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.GET_PRIVATE_DATA, {})) return j["viewer"] def get_phone_numbers(self): @@ -1370,7 +1370,7 @@ def _users_approval(self, user_ids, approve, thread_id=None): "surface": "ADMIN_MODEL_APPROVAL_CENTER", } j, = self.graphql_requests( - _graphql.from_doc_id("1574519202665847", {"data": data}) + _graphql.from_doc_id(_graphql.DocID.USER_APPROVAL, {"data": data}) ) def accept_users_to_group(self, user_ids, thread_id=None): @@ -1540,7 +1540,7 @@ def react_to_message(self, message_id, reaction): "message_id": str(message_id), "reaction": reaction.value if reaction else None, } - data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})} + data = {"doc_id": _graphql.DocID.MESSAGE_REACT, "variables": json.dumps({"data": data})} j = self._payload_post("/webgraphql/mutation", data) _util.handle_graphql_errors(j) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 1d4f2203..452d84a3 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -1,3 +1,4 @@ +import enum import json import re from ._core import log @@ -23,6 +24,15 @@ def decode(self, s, _w=WHITESPACE.match): # End shameless copy +class DocID(enum.Enum): + FETCH_INFO = "1768656253222505" + FETCH_THREAD_INFO = "2147762685294928" + FETCH_THREAD_MESSAGES = "1860982147341344" + FETCH_THREAD_LIST = "1349387578499440" + GET_PRIVATE_DATA = "1868889766468115" + USER_APPROVAL = "1574519202665847" + MESSAGE_REACT = "1491398900900362" + def queries_to_json(*queries): """ From 2bbf84d7ac7e06a63dd27ac2cf956f7503159a77 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sun, 13 Oct 2019 21:51:34 +1100 Subject: [PATCH 2/2] black linter changes --- fbchat/_client.py | 25 +++++++++++++++++++------ fbchat/_graphql.py | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 031be4dd..338a9bb1 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -198,7 +198,9 @@ def logout(self): def _forced_fetch(self, thread_id, mid): params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}} - j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_INFO, params)) + j, = self.graphql_requests( + _graphql.from_doc_id(_graphql.DocID.FETCH_INFO, params) + ) return j def fetch_threads(self, thread_location, before=None, after=None, limit=None): @@ -615,7 +617,9 @@ def fetch_thread_info(self, *thread_ids): "load_read_receipts": False, "before": None, } - queries.append(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_INFO, params)) + queries.append( + _graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_INFO, params) + ) j = self.graphql_requests(*queries) @@ -679,7 +683,9 @@ def fetch_thread_messages(self, thread_id=None, limit=20, before=None): "load_read_receipts": True, "before": _util.datetime_to_millis(before) if before else None, } - j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_MESSAGES, params)) + j, = self.graphql_requests( + _graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_MESSAGES, params) + ) if j.get("message_thread") is None: raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j)) @@ -733,7 +739,9 @@ def fetch_thread_list( "includeDeliveryReceipts": True, "includeSeqID": False, } - j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_LIST, params)) + j, = self.graphql_requests( + _graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_LIST, params) + ) rtn = [] for node in j["viewer"]["message_threads"]["nodes"]: @@ -853,7 +861,9 @@ def fetch_plan_info(self, plan_id): return Plan._from_fetch(j) def _get_private_data(self): - j, = self.graphql_requests(_graphql.from_doc_id(_graphql.DocID.GET_PRIVATE_DATA, {})) + j, = self.graphql_requests( + _graphql.from_doc_id(_graphql.DocID.GET_PRIVATE_DATA, {}) + ) return j["viewer"] def get_phone_numbers(self): @@ -1540,7 +1550,10 @@ def react_to_message(self, message_id, reaction): "message_id": str(message_id), "reaction": reaction.value if reaction else None, } - data = {"doc_id": _graphql.DocID.MESSAGE_REACT, "variables": json.dumps({"data": data})} + data = { + "doc_id": _graphql.DocID.MESSAGE_REACT, + "variables": json.dumps({"data": data}), + } j = self._payload_post("/webgraphql/mutation", data) _util.handle_graphql_errors(j) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 452d84a3..3076577e 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -24,6 +24,7 @@ def decode(self, s, _w=WHITESPACE.match): # End shameless copy + class DocID(enum.Enum): FETCH_INFO = "1768656253222505" FETCH_THREAD_INFO = "2147762685294928"