Merge pull request #436 from carpedm20/clean-up-requests
Normalize a lot of request parsing
This commit is contained in:
@@ -118,81 +118,42 @@ class Client(object):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _get(self, url, query=None, fix_request=False, as_json=False, error_retries=3):
|
||||
def _get(self, url, query=None, error_retries=3):
|
||||
payload = self._generatePayload(query)
|
||||
r = self._state._session.get(prefix_url(url), params=payload)
|
||||
if not fix_request:
|
||||
return r
|
||||
try:
|
||||
return check_request(r, as_json=as_json)
|
||||
return check_request(r)
|
||||
except FBchatFacebookError as e:
|
||||
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code):
|
||||
return self._get(
|
||||
url,
|
||||
query=query,
|
||||
fix_request=fix_request,
|
||||
as_json=as_json,
|
||||
error_retries=error_retries - 1,
|
||||
)
|
||||
return self._get(url, query=query, error_retries=error_retries - 1)
|
||||
raise e
|
||||
|
||||
def _post(
|
||||
self,
|
||||
url,
|
||||
query=None,
|
||||
fix_request=False,
|
||||
as_json=False,
|
||||
as_graphql=False,
|
||||
error_retries=3,
|
||||
):
|
||||
def _post(self, url, query=None, files=None, as_graphql=False, error_retries=3):
|
||||
payload = self._generatePayload(query)
|
||||
r = self._state._session.post(prefix_url(url), data=payload)
|
||||
if not fix_request:
|
||||
return r
|
||||
r = self._state._session.post(prefix_url(url), data=payload, files=files)
|
||||
try:
|
||||
if as_graphql:
|
||||
content = check_request(r, as_json=False)
|
||||
return graphql_response_to_json(content)
|
||||
else:
|
||||
return check_request(r, as_json=as_json)
|
||||
return check_request(r)
|
||||
except FBchatFacebookError as e:
|
||||
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code):
|
||||
return self._post(
|
||||
url,
|
||||
query=query,
|
||||
fix_request=fix_request,
|
||||
as_json=as_json,
|
||||
files=files,
|
||||
as_graphql=as_graphql,
|
||||
error_retries=error_retries - 1,
|
||||
)
|
||||
raise e
|
||||
|
||||
def _postFile(
|
||||
self,
|
||||
url,
|
||||
files=None,
|
||||
query=None,
|
||||
fix_request=False,
|
||||
as_json=False,
|
||||
error_retries=3,
|
||||
):
|
||||
payload = self._generatePayload(query)
|
||||
r = self._state._session.post(prefix_url(url), data=payload, files=files)
|
||||
if not fix_request:
|
||||
return r
|
||||
def _payload_post(self, url, data, files=None):
|
||||
j = self._post(url, data, files=files)
|
||||
try:
|
||||
return check_request(r, as_json=as_json)
|
||||
except FBchatFacebookError as e:
|
||||
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code):
|
||||
return self._postFile(
|
||||
url,
|
||||
files=files,
|
||||
query=query,
|
||||
fix_request=fix_request,
|
||||
as_json=as_json,
|
||||
error_retries=error_retries - 1,
|
||||
)
|
||||
raise e
|
||||
return j["payload"]
|
||||
except (KeyError, TypeError):
|
||||
raise FBchatException("Missing payload: {}".format(j))
|
||||
|
||||
def graphql_requests(self, *queries):
|
||||
"""
|
||||
@@ -208,9 +169,7 @@ class Client(object):
|
||||
"response_format": "json",
|
||||
"queries": graphql_queries_to_json(*queries),
|
||||
}
|
||||
return tuple(
|
||||
self._post("/api/graphqlbatch/", data, fix_request=True, as_graphql=True)
|
||||
)
|
||||
return tuple(self._post("/api/graphqlbatch/", data, as_graphql=True))
|
||||
|
||||
def graphql_request(self, query):
|
||||
"""
|
||||
@@ -454,14 +413,10 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"viewer": self._uid}
|
||||
j = self._post(
|
||||
"/chat/user_info_all", query=data, fix_request=True, as_json=True
|
||||
)
|
||||
if j.get("payload") is None:
|
||||
raise FBchatException("Missing payload while fetching users: {}".format(j))
|
||||
j = self._payload_post("/chat/user_info_all", data)
|
||||
|
||||
users = []
|
||||
for data in j["payload"].values():
|
||||
for data in j.values():
|
||||
if data["type"] in ["user", "friend"]:
|
||||
if data["id"] in ["0", 0]:
|
||||
# Skip invalid users
|
||||
@@ -568,14 +523,9 @@ class Client(object):
|
||||
"identifier": "thread_fbid",
|
||||
"thread_fbid": thread_id,
|
||||
}
|
||||
j = self._post(
|
||||
"/ajax/mercury/search_snippets.php?dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
)
|
||||
j = self._payload_post("/ajax/mercury/search_snippets.php?dpr=1", data)
|
||||
|
||||
result = j["payload"]["search_snippets"][query]
|
||||
result = j["search_snippets"][query]
|
||||
snippets = result[thread_id]["snippets"] if result.get(thread_id) else []
|
||||
for snippet in snippets:
|
||||
yield snippet["message_id"]
|
||||
@@ -618,13 +568,8 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"query": query, "snippetLimit": thread_limit}
|
||||
j = self._post(
|
||||
"/ajax/mercury/search_snippets.php?dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
)
|
||||
result = j["payload"]["search_snippets"][query]
|
||||
j = self._payload_post("/ajax/mercury/search_snippets.php?dpr=1", data)
|
||||
result = j["search_snippets"][query]
|
||||
|
||||
if not result:
|
||||
return {}
|
||||
@@ -641,14 +586,14 @@ class Client(object):
|
||||
|
||||
def _fetchInfo(self, *ids):
|
||||
data = {"ids[{}]".format(i): _id for i, _id in enumerate(ids)}
|
||||
j = self._post("/chat/user_info/", data, fix_request=True, as_json=True)
|
||||
j = self._payload_post("/chat/user_info/", data)
|
||||
|
||||
if j.get("payload") is None or j["payload"].get("profiles") is None:
|
||||
if j.get("profiles") is None:
|
||||
raise FBchatException("No users/pages returned: {}".format(j))
|
||||
|
||||
entries = {}
|
||||
for _id in j["payload"]["profiles"]:
|
||||
k = j["payload"]["profiles"][_id]
|
||||
for _id in j["profiles"]:
|
||||
k = j["profiles"][_id]
|
||||
if k["type"] in ["user", "friend"]:
|
||||
entries[_id] = {
|
||||
"id": _id,
|
||||
@@ -911,12 +856,10 @@ class Client(object):
|
||||
"last_action_timestamp": now() - 60 * 1000
|
||||
# 'last_action_timestamp': 0
|
||||
}
|
||||
j = self._post(
|
||||
"/ajax/mercury/unread_threads.php", form, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/ajax/mercury/unread_threads.php", form)
|
||||
|
||||
payload = j["payload"]["unread_thread_fbids"][0]
|
||||
return payload["thread_fbids"] + payload["other_user_fbids"]
|
||||
result = j["unread_thread_fbids"][0]
|
||||
return result["thread_fbids"] + result["other_user_fbids"]
|
||||
|
||||
def fetchUnseen(self):
|
||||
"""
|
||||
@@ -926,12 +869,10 @@ class Client(object):
|
||||
:rtype: list
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
j = self._post(
|
||||
"/mercury/unseen_thread_ids/", None, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/mercury/unseen_thread_ids/", None)
|
||||
|
||||
payload = j["payload"]["unseen_thread_fbids"][0]
|
||||
return payload["thread_fbids"] + payload["other_user_fbids"]
|
||||
result = j["unseen_thread_fbids"][0]
|
||||
return result["thread_fbids"] + result["other_user_fbids"]
|
||||
|
||||
def fetchImageUrl(self, image_id):
|
||||
"""Fetches the url to the original image from an image attachment ID
|
||||
@@ -944,9 +885,7 @@ class Client(object):
|
||||
"""
|
||||
image_id = str(image_id)
|
||||
data = {"photo_id": str(image_id)}
|
||||
j = self._get(
|
||||
"/mercury/attachments/photo/", query=data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._post("/mercury/attachments/photo/", data)
|
||||
|
||||
url = get_jsmods_require(j, 3)
|
||||
if url is None:
|
||||
@@ -976,10 +915,8 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"question_id": poll_id}
|
||||
j = self._post(
|
||||
"/ajax/mercury/get_poll_options", data, fix_request=True, as_json=True
|
||||
)
|
||||
return [PollOption._from_graphql(m) for m in j["payload"]]
|
||||
j = self._payload_post("/ajax/mercury/get_poll_options", data)
|
||||
return [PollOption._from_graphql(m) for m in j]
|
||||
|
||||
def fetchPlanInfo(self, plan_id):
|
||||
"""
|
||||
@@ -991,8 +928,8 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"event_reminder_id": plan_id}
|
||||
j = self._post("/ajax/eventreminder", data, fix_request=True, as_json=True)
|
||||
return Plan._from_fetch(j["payload"])
|
||||
j = self._payload_post("/ajax/eventreminder", data)
|
||||
return Plan._from_fetch(j)
|
||||
|
||||
def _getPrivateData(self):
|
||||
j = self.graphql_request(GraphQL(doc_id="1868889766468115"))
|
||||
@@ -1116,7 +1053,7 @@ class Client(object):
|
||||
|
||||
def _doSendRequest(self, data, get_thread_id=False):
|
||||
"""Sends the data to `SendURL`, and returns the message ID or None on failure"""
|
||||
j = self._post("/messaging/send/", data, fix_request=True, as_json=True)
|
||||
j = self._post("/messaging/send/", data)
|
||||
|
||||
# update JS token if received in response
|
||||
fb_dtsg = get_jsmods_require(j, 2)
|
||||
@@ -1251,8 +1188,7 @@ class Client(object):
|
||||
:param mid: :ref:`Message ID <intro_message_ids>` of the message to unsend
|
||||
"""
|
||||
data = {"message_id": mid}
|
||||
r = self._post("/messaging/unsend_message/?dpr=1", data)
|
||||
r.raise_for_status()
|
||||
j = self._payload_post("/messaging/unsend_message/?dpr=1", data)
|
||||
|
||||
def _sendLocation(
|
||||
self, location, current=True, message=None, thread_id=None, thread_type=None
|
||||
@@ -1326,22 +1262,18 @@ class Client(object):
|
||||
|
||||
data = {"voice_clip": voice_clip}
|
||||
|
||||
j = self._postFile(
|
||||
"https://upload.facebook.com/ajax/mercury/upload.php",
|
||||
files=file_dict,
|
||||
query=data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
j = self._payload_post(
|
||||
"https://upload.facebook.com/ajax/mercury/upload.php", data, files=file_dict
|
||||
)
|
||||
|
||||
if len(j["payload"]["metadata"]) != len(files):
|
||||
if len(j["metadata"]) != len(files):
|
||||
raise FBchatException(
|
||||
"Some files could not be uploaded: {}, {}".format(j, files)
|
||||
)
|
||||
|
||||
return [
|
||||
(data[mimetype_to_key(data["filetype"])], data["filetype"])
|
||||
for data in j["payload"]["metadata"]
|
||||
for data in j["metadata"]
|
||||
]
|
||||
|
||||
def _sendFiles(
|
||||
@@ -1510,9 +1442,7 @@ class Client(object):
|
||||
"attachment_id": attachment_id,
|
||||
"recipient_map[{}]".format(generateOfflineThreadingID()): thread_id,
|
||||
}
|
||||
j = self._post(
|
||||
"/mercury/attachments/forward/", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/mercury/attachments/forward/", data)
|
||||
|
||||
def createGroup(self, message, user_ids):
|
||||
"""
|
||||
@@ -1578,9 +1508,7 @@ class Client(object):
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
|
||||
data = {"uid": user_id, "tid": thread_id}
|
||||
j = self._post(
|
||||
"/chat/remove_participants/", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/chat/remove_participants/", data)
|
||||
|
||||
def _adminStatus(self, admin_ids, admin, thread_id=None):
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
@@ -1592,9 +1520,7 @@ class Client(object):
|
||||
for i, admin_id in enumerate(admin_ids):
|
||||
data["admin_ids[{}]".format(i)] = str(admin_id)
|
||||
|
||||
j = self._post(
|
||||
"/messaging/save_admins/?dpr=1", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/messaging/save_admins/?dpr=1", data)
|
||||
|
||||
def addGroupAdmins(self, admin_ids, thread_id=None):
|
||||
"""
|
||||
@@ -1627,9 +1553,7 @@ class Client(object):
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
|
||||
data = {"set_mode": int(require_admin_approval), "thread_fbid": thread_id}
|
||||
j = self._post(
|
||||
"/messaging/set_approval_mode/?dpr=1", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/messaging/set_approval_mode/?dpr=1", data)
|
||||
|
||||
def _usersApproval(self, user_ids, approve, thread_id=None):
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
@@ -1680,9 +1604,7 @@ class Client(object):
|
||||
|
||||
data = {"thread_image_id": image_id, "thread_id": thread_id}
|
||||
|
||||
j = self._post(
|
||||
"/messaging/set_thread_image/?dpr=1", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/messaging/set_thread_image/?dpr=1", data)
|
||||
return image_id
|
||||
|
||||
def changeGroupImageRemote(self, image_url, thread_id=None):
|
||||
@@ -1729,9 +1651,7 @@ class Client(object):
|
||||
)
|
||||
|
||||
data = {"thread_name": title, "thread_id": thread_id}
|
||||
j = self._post(
|
||||
"/messaging/set_thread_name/?dpr=1", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/messaging/set_thread_name/?dpr=1", data)
|
||||
|
||||
def changeNickname(
|
||||
self, nickname, user_id, thread_id=None, thread_type=ThreadType.USER
|
||||
@@ -1753,11 +1673,8 @@ class Client(object):
|
||||
"participant_id": user_id,
|
||||
"thread_or_other_fbid": thread_id,
|
||||
}
|
||||
j = self._post(
|
||||
"/messaging/save_thread_nickname/?source=thread_settings&dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
j = self._payload_post(
|
||||
"/messaging/save_thread_nickname/?source=thread_settings&dpr=1", data
|
||||
)
|
||||
|
||||
def changeThreadColor(self, color, thread_id=None):
|
||||
@@ -1775,11 +1692,8 @@ class Client(object):
|
||||
"color_choice": color.value if color != ThreadColor.MESSENGER_BLUE else "",
|
||||
"thread_or_other_fbid": thread_id,
|
||||
}
|
||||
j = self._post(
|
||||
"/messaging/save_thread_color/?source=thread_settings&dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
j = self._payload_post(
|
||||
"/messaging/save_thread_color/?source=thread_settings&dpr=1", data
|
||||
)
|
||||
|
||||
def changeThreadEmoji(self, emoji, thread_id=None):
|
||||
@@ -1795,11 +1709,8 @@ class Client(object):
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
|
||||
data = {"emoji_choice": emoji, "thread_or_other_fbid": thread_id}
|
||||
j = self._post(
|
||||
"/messaging/save_thread_emoji/?source=thread_settings&dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
j = self._payload_post(
|
||||
"/messaging/save_thread_emoji/?source=thread_settings&dpr=1", data
|
||||
)
|
||||
|
||||
def reactToMessage(self, message_id, reaction):
|
||||
@@ -1819,7 +1730,7 @@ class Client(object):
|
||||
"reaction": reaction.value if reaction else None,
|
||||
}
|
||||
data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})}
|
||||
self._post("/webgraphql/mutation", data, fix_request=True, as_json=True)
|
||||
j = self._payload_post("/webgraphql/mutation", data)
|
||||
|
||||
def createPlan(self, plan, thread_id=None):
|
||||
"""
|
||||
@@ -1841,9 +1752,7 @@ class Client(object):
|
||||
"location_name": plan.location or "",
|
||||
"acontext": ACONTEXT,
|
||||
}
|
||||
j = self._post(
|
||||
"/ajax/eventreminder/create", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/ajax/eventreminder/create", data)
|
||||
|
||||
def editPlan(self, plan, new_plan):
|
||||
"""
|
||||
@@ -1863,9 +1772,7 @@ class Client(object):
|
||||
"title": new_plan.title,
|
||||
"acontext": ACONTEXT,
|
||||
}
|
||||
j = self._post(
|
||||
"/ajax/eventreminder/submit", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/ajax/eventreminder/submit", data)
|
||||
|
||||
def deletePlan(self, plan):
|
||||
"""
|
||||
@@ -1875,9 +1782,7 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"event_reminder_id": plan.uid, "delete": "true", "acontext": ACONTEXT}
|
||||
j = self._post(
|
||||
"/ajax/eventreminder/submit", data, fix_request=True, as_json=True
|
||||
)
|
||||
j = self._payload_post("/ajax/eventreminder/submit", data)
|
||||
|
||||
def changePlanParticipation(self, plan, take_part=True):
|
||||
"""
|
||||
@@ -1892,7 +1797,7 @@ class Client(object):
|
||||
"guest_state": "GOING" if take_part else "DECLINED",
|
||||
"acontext": ACONTEXT,
|
||||
}
|
||||
j = self._post("/ajax/eventreminder/rsvp", data, fix_request=True, as_json=True)
|
||||
j = self._payload_post("/ajax/eventreminder/rsvp", data)
|
||||
|
||||
def eventReminder(self, thread_id, time, title, location="", location_id=""):
|
||||
"""
|
||||
@@ -1922,12 +1827,7 @@ class Client(object):
|
||||
data["option_text_array[{}]".format(i)] = option.text
|
||||
data["option_is_selected_array[{}]".format(i)] = str(int(option.vote))
|
||||
|
||||
j = self._post(
|
||||
"/messaging/group_polling/create_poll/?dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
)
|
||||
j = self._payload_post("/messaging/group_polling/create_poll/?dpr=1", data)
|
||||
|
||||
def updatePollVote(self, poll_id, option_ids=[], new_options=[]):
|
||||
"""
|
||||
@@ -1949,12 +1849,7 @@ class Client(object):
|
||||
for i, option_text in enumerate(new_options):
|
||||
data["new_options[{}]".format(i)] = option_text
|
||||
|
||||
j = self._post(
|
||||
"/messaging/group_polling/update_vote/?dpr=1",
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
)
|
||||
j = self._payload_post("/messaging/group_polling/update_vote/?dpr=1", data)
|
||||
|
||||
def setTypingStatus(self, status, thread_id=None, thread_type=None):
|
||||
"""
|
||||
@@ -1975,7 +1870,7 @@ class Client(object):
|
||||
"to": thread_id if thread_type == ThreadType.USER else "",
|
||||
"source": "mercury-chat",
|
||||
}
|
||||
j = self._post("/ajax/messaging/typ.php", data, fix_request=True, as_json=True)
|
||||
j = self._payload_post("/ajax/messaging/typ.php", data)
|
||||
|
||||
"""
|
||||
END SEND METHODS
|
||||
@@ -1987,7 +1882,7 @@ class Client(object):
|
||||
|
||||
:param thread_id: User/Group ID to which the message belongs. See :ref:`intro_threads`
|
||||
:param message_id: Message ID to set as delivered. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {
|
||||
@@ -1995,8 +1890,8 @@ class Client(object):
|
||||
"thread_ids[%s][0]" % thread_id: message_id,
|
||||
}
|
||||
|
||||
r = self._post("/ajax/mercury/delivery_receipts.php", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/delivery_receipts.php", data)
|
||||
return True
|
||||
|
||||
def _readStatus(self, read, thread_ids):
|
||||
thread_ids = require_list(thread_ids)
|
||||
@@ -2006,8 +1901,7 @@ class Client(object):
|
||||
for thread_id in thread_ids:
|
||||
data["ids[{}]".format(thread_id)] = "true" if read else "false"
|
||||
|
||||
r = self._post("/ajax/mercury/change_read_status.php", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/change_read_status.php", data)
|
||||
|
||||
def markAsRead(self, thread_ids=None):
|
||||
"""
|
||||
@@ -2015,7 +1909,6 @@ class Client(object):
|
||||
All messages inside the threads will be marked as read
|
||||
|
||||
:param thread_ids: User/Group IDs to set as read. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
self._readStatus(True, thread_ids)
|
||||
@@ -2026,7 +1919,6 @@ class Client(object):
|
||||
All messages inside the threads will be marked as unread
|
||||
|
||||
:param thread_ids: User/Group IDs to set as unread. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
self._readStatus(False, thread_ids)
|
||||
@@ -2036,8 +1928,7 @@ class Client(object):
|
||||
.. todo::
|
||||
Documenting this
|
||||
"""
|
||||
r = self._post("/ajax/mercury/mark_seen.php", {"seen_timestamp": now()})
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/mark_seen.php", {"seen_timestamp": now()})
|
||||
|
||||
def friendConnect(self, friend_id):
|
||||
"""
|
||||
@@ -2046,37 +1937,31 @@ class Client(object):
|
||||
"""
|
||||
data = {"to_friend": friend_id, "action": "confirm"}
|
||||
|
||||
r = self._post("/ajax/add_friend/action.php?dpr=1", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/add_friend/action.php?dpr=1", data)
|
||||
|
||||
def removeFriend(self, friend_id=None):
|
||||
"""
|
||||
Removes a specifed friend from your friend list
|
||||
|
||||
:param friend_id: The ID of the friend that you want to remove
|
||||
:return: Returns error if the removing was unsuccessful, returns True when successful.
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
payload = {"friend_id": friend_id, "unref": "none", "confirm": "Confirm"}
|
||||
r = self._post("https://m.facebook.com/a/removefriend.php", payload)
|
||||
query = parse_qs(urlparse(r.url).query)
|
||||
if "err" not in query:
|
||||
log.debug("Remove was successful!")
|
||||
return True
|
||||
else:
|
||||
log.warning("Error while removing friend")
|
||||
return False
|
||||
data = {"uid": friend_id}
|
||||
j = self._payload_post("/ajax/profile/removefriendconfirm.php", data)
|
||||
return True
|
||||
|
||||
def blockUser(self, user_id):
|
||||
"""
|
||||
Blocks messages from a specifed user
|
||||
|
||||
:param user_id: The ID of the user that you want to block
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"fbid": user_id}
|
||||
r = self._post("/messaging/block_messages/?dpr=1", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/messaging/block_messages/?dpr=1", data)
|
||||
return True
|
||||
|
||||
def unblockUser(self, user_id):
|
||||
"""
|
||||
@@ -2087,8 +1972,8 @@ class Client(object):
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
data = {"fbid": user_id}
|
||||
r = self._post("/messaging/unblock_messages/?dpr=1", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/messaging/unblock_messages/?dpr=1", data)
|
||||
return True
|
||||
|
||||
def moveThreads(self, location, thread_ids):
|
||||
"""
|
||||
@@ -2096,7 +1981,7 @@ class Client(object):
|
||||
|
||||
:param location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
|
||||
:param thread_ids: Thread IDs to move. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
thread_ids = require_list(thread_ids)
|
||||
@@ -2110,26 +1995,25 @@ class Client(object):
|
||||
for thread_id in thread_ids:
|
||||
data_archive["ids[{}]".format(thread_id)] = "true"
|
||||
data_unpin["ids[{}]".format(thread_id)] = "false"
|
||||
r_archive = self._post(
|
||||
j_archive = self._payload_post(
|
||||
"/ajax/mercury/change_archived_status.php?dpr=1", data_archive
|
||||
)
|
||||
r_unpin = self._post(
|
||||
j_unpin = self._payload_post(
|
||||
"/ajax/mercury/change_pinned_status.php?dpr=1", data_unpin
|
||||
)
|
||||
return r_archive.ok and r_unpin.ok
|
||||
else:
|
||||
data = dict()
|
||||
for i, thread_id in enumerate(thread_ids):
|
||||
data["{}[{}]".format(location.name.lower(), i)] = thread_id
|
||||
r = self._post("/ajax/mercury/move_thread.php", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/move_thread.php", data)
|
||||
return True
|
||||
|
||||
def deleteThreads(self, thread_ids):
|
||||
"""
|
||||
Deletes threads
|
||||
|
||||
:param thread_ids: Thread IDs to delete. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
thread_ids = require_list(thread_ids)
|
||||
@@ -2139,36 +2023,40 @@ class Client(object):
|
||||
for i, thread_id in enumerate(thread_ids):
|
||||
data_unpin["ids[{}]".format(thread_id)] = "false"
|
||||
data_delete["ids[{}]".format(i)] = thread_id
|
||||
r_unpin = self._post("/ajax/mercury/change_pinned_status.php?dpr=1", data_unpin)
|
||||
r_delete = self._post("/ajax/mercury/delete_thread.php?dpr=1", data_delete)
|
||||
return r_unpin.ok and r_delete.ok
|
||||
j_unpin = self._payload_post(
|
||||
"/ajax/mercury/change_pinned_status.php?dpr=1", data_unpin
|
||||
)
|
||||
j_delete = self._payload_post(
|
||||
"/ajax/mercury/delete_thread.php?dpr=1", data_delete
|
||||
)
|
||||
return True
|
||||
|
||||
def markAsSpam(self, thread_id=None):
|
||||
"""
|
||||
Mark a thread as spam and delete it
|
||||
|
||||
:param thread_id: User/Group ID to mark as spam. See :ref:`intro_threads`
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
r = self._post("/ajax/mercury/mark_spam.php?dpr=1", {"id": thread_id})
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/mark_spam.php?dpr=1", {"id": thread_id})
|
||||
return True
|
||||
|
||||
def deleteMessages(self, message_ids):
|
||||
"""
|
||||
Deletes specifed messages
|
||||
|
||||
:param message_ids: Message IDs to delete
|
||||
:return: Whether the request was successful
|
||||
:return: True
|
||||
:raises: FBchatException if request failed
|
||||
"""
|
||||
message_ids = require_list(message_ids)
|
||||
data = dict()
|
||||
for i, message_id in enumerate(message_ids):
|
||||
data["message_ids[{}]".format(i)] = message_id
|
||||
r = self._post("/ajax/mercury/delete_messages.php?dpr=1", data)
|
||||
return r.ok
|
||||
j = self._payload_post("/ajax/mercury/delete_messages.php?dpr=1", data)
|
||||
return True
|
||||
|
||||
def muteThread(self, mute_time=-1, thread_id=None):
|
||||
"""
|
||||
@@ -2179,9 +2067,7 @@ class Client(object):
|
||||
"""
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
data = {"mute_settings": str(mute_time), "thread_fbid": thread_id}
|
||||
content = self._post(
|
||||
"/ajax/mercury/change_mute_thread.php?dpr=1", data, fix_request=True
|
||||
)
|
||||
j = self._payload_post("/ajax/mercury/change_mute_thread.php?dpr=1", data)
|
||||
|
||||
def unmuteThread(self, thread_id=None):
|
||||
"""
|
||||
@@ -2200,8 +2086,8 @@ class Client(object):
|
||||
"""
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
data = {"reactions_mute_mode": int(mute), "thread_fbid": thread_id}
|
||||
r = self._post(
|
||||
"/ajax/mercury/change_reactions_mute_thread/?dpr=1", data, fix_request=True
|
||||
j = self._payload_post(
|
||||
"/ajax/mercury/change_reactions_mute_thread/?dpr=1", data
|
||||
)
|
||||
|
||||
def unmuteThreadReactions(self, thread_id=None):
|
||||
@@ -2221,9 +2107,7 @@ class Client(object):
|
||||
"""
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
data = {"mentions_mute_mode": int(mute), "thread_fbid": thread_id}
|
||||
r = self._post(
|
||||
"/ajax/mercury/change_mentions_mute_thread/?dpr=1", data, fix_request=True
|
||||
)
|
||||
j = self._payload_post("/ajax/mercury/change_mentions_mute_thread/?dpr=1", data)
|
||||
|
||||
def unmuteThreadMentions(self, thread_id=None):
|
||||
"""
|
||||
@@ -2250,11 +2134,9 @@ class Client(object):
|
||||
"viewer_uid": self._uid,
|
||||
"state": "active",
|
||||
}
|
||||
self._get(
|
||||
j = self._get(
|
||||
"https://{}-edge-chat.facebook.com/active_ping".format(self._pull_channel),
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=False,
|
||||
)
|
||||
|
||||
def _pullMessage(self):
|
||||
@@ -2268,10 +2150,7 @@ class Client(object):
|
||||
"state": "active" if self._markAlive else "offline",
|
||||
}
|
||||
return self._get(
|
||||
"https://{}-edge-chat.facebook.com/pull".format(self._pull_channel),
|
||||
data,
|
||||
fix_request=True,
|
||||
as_json=True,
|
||||
"https://{}-edge-chat.facebook.com/pull".format(self._pull_channel), data
|
||||
)
|
||||
|
||||
def _parseDelta(self, m):
|
||||
|
@@ -140,25 +140,31 @@ def check_json(j):
|
||||
|
||||
|
||||
def check_request(r, as_json=True):
|
||||
if not r.ok:
|
||||
check_http_code(r.status_code)
|
||||
content = get_decoded_r(r)
|
||||
check_content(content)
|
||||
return to_json(content) if as_json else content
|
||||
|
||||
|
||||
def check_http_code(code):
|
||||
if 400 <= code < 600:
|
||||
raise FBchatFacebookError(
|
||||
"Error when sending request: Got {} response".format(r.status_code),
|
||||
request_status_code=r.status_code,
|
||||
"Error when sending request: Got {} response".format(code),
|
||||
request_status_code=code,
|
||||
)
|
||||
|
||||
content = get_decoded_r(r)
|
||||
|
||||
def check_content(content, as_json=True):
|
||||
if content is None or len(content) == 0:
|
||||
raise FBchatFacebookError("Error when sending request: Got empty response")
|
||||
|
||||
if as_json:
|
||||
content = strip_json_cruft(content)
|
||||
j = parse_json(content)
|
||||
check_json(j)
|
||||
log.debug(j)
|
||||
return j
|
||||
else:
|
||||
return content
|
||||
|
||||
def to_json(content):
|
||||
content = strip_json_cruft(content)
|
||||
j = parse_json(content)
|
||||
check_json(j)
|
||||
log.debug(j)
|
||||
return j
|
||||
|
||||
|
||||
def get_jsmods_require(j, index):
|
||||
|
Reference in New Issue
Block a user