Standardize json parsing
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import datetime
|
||||
import time
|
||||
import json
|
||||
import requests
|
||||
|
||||
from ._core import log
|
||||
@@ -1126,7 +1125,7 @@ class Client:
|
||||
score = int(score)
|
||||
leaderboard = delta["untypedData"].get("leaderboard")
|
||||
if leaderboard is not None:
|
||||
leaderboard = json.loads(leaderboard)["scores"]
|
||||
leaderboard = _util.parse_json(leaderboard)["scores"]
|
||||
self.on_game_played(
|
||||
mid=mid,
|
||||
author_id=author_id,
|
||||
@@ -1185,7 +1184,7 @@ class Client:
|
||||
# Group poll event
|
||||
elif delta_type == "group_poll":
|
||||
event_type = delta["untypedData"]["event_type"]
|
||||
poll_json = json.loads(delta["untypedData"]["question_json"])
|
||||
poll_json = _util.parse_json(delta["untypedData"]["question_json"])
|
||||
poll = Poll._from_graphql(poll_json)
|
||||
if event_type == "question_creation":
|
||||
# User created group poll
|
||||
@@ -1200,13 +1199,13 @@ class Client:
|
||||
)
|
||||
elif event_type == "update_vote":
|
||||
# User voted on group poll
|
||||
added_options = json.loads(delta["untypedData"]["added_option_ids"])
|
||||
removed_options = json.loads(delta["untypedData"]["removed_option_ids"])
|
||||
added = _util.parse_json(delta["untypedData"]["added_option_ids"])
|
||||
removed = _util.parse_json(delta["untypedData"]["removed_option_ids"])
|
||||
self.on_poll_voted(
|
||||
mid=mid,
|
||||
poll=poll,
|
||||
added_options=added_options,
|
||||
removed_options=removed_options,
|
||||
added_options=added,
|
||||
removed_options=removed,
|
||||
author_id=author_id,
|
||||
thread=get_thread(metadata),
|
||||
at=at,
|
||||
@@ -1277,7 +1276,7 @@ class Client:
|
||||
|
||||
# Client payload (that weird numbers)
|
||||
elif delta_class == "ClientPayload":
|
||||
payload = json.loads("".join(chr(z) for z in delta["payload"]))
|
||||
payload = _util.parse_json("".join(chr(z) for z in delta["payload"]))
|
||||
at = _util.millis_to_datetime(m.get("ofd_ts"))
|
||||
for d in payload.get("deltas", []):
|
||||
|
||||
|
@@ -31,7 +31,7 @@ def queries_to_json(*queries):
|
||||
rtn = {}
|
||||
for i, query in enumerate(queries):
|
||||
rtn["q{}".format(i)] = query
|
||||
return json.dumps(rtn)
|
||||
return _util.json_minimal(rtn)
|
||||
|
||||
|
||||
def response_to_json(content):
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import attr
|
||||
import json
|
||||
from string import Formatter
|
||||
from ._core import log, attrs_default, Enum
|
||||
from . import _util, _session, _attachment, _location, _file, _quick_reply, _sticker
|
||||
@@ -107,7 +106,10 @@ class Message:
|
||||
"message_id": self.id,
|
||||
"reaction": reaction.value if reaction else None,
|
||||
}
|
||||
data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})}
|
||||
data = {
|
||||
"doc_id": 1491398900900362,
|
||||
"variables": _util.json_minimal({"data": data}),
|
||||
}
|
||||
j = self.session._payload_post("/webgraphql/mutation", data)
|
||||
_util.handle_graphql_errors(j)
|
||||
|
||||
@@ -214,7 +216,7 @@ class MessageData(Message):
|
||||
@staticmethod
|
||||
def _parse_quick_replies(data):
|
||||
if data:
|
||||
data = json.loads(data).get("quick_replies")
|
||||
data = _util.parse_json(data).get("quick_replies")
|
||||
if isinstance(data, list):
|
||||
return [_quick_reply.graphql_to_quick_reply(q) for q in data]
|
||||
elif isinstance(data, dict):
|
||||
@@ -285,7 +287,7 @@ class MessageData(Message):
|
||||
unsent = False
|
||||
sticker = None
|
||||
for attachment in data.get("attachments") or ():
|
||||
attachment = json.loads(attachment["mercuryJSON"])
|
||||
attachment = _util.parse_json(attachment["mercuryJSON"])
|
||||
if attachment.get("blob_attachment"):
|
||||
attachments.append(
|
||||
_file.graphql_to_attachment(attachment["blob_attachment"])
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import attr
|
||||
import datetime
|
||||
import json
|
||||
from ._core import attrs_default, Enum
|
||||
from . import _exception, _util, _session
|
||||
|
||||
@@ -158,7 +157,7 @@ class PlanData(Plan):
|
||||
author_id=data.get("event_creator_id"),
|
||||
guests={
|
||||
x["node"]["id"]: GuestStatus[x["guest_list_state"]]
|
||||
for x in json.loads(data["guest_state_list"])
|
||||
for x in _util.parse_json(data["guest_state_list"])
|
||||
},
|
||||
)
|
||||
|
||||
|
@@ -216,7 +216,7 @@ class ThreadABC(metaclass=abc.ABCMeta):
|
||||
# xmd["quick_replies"].append(q)
|
||||
# if len(quick_replies) == 1 and quick_replies[0].is_response:
|
||||
# xmd["quick_replies"] = xmd["quick_replies"][0]
|
||||
# data["platform_xmd"] = json.dumps(xmd)
|
||||
# data["platform_xmd"] = _util.json_minimal(xmd)
|
||||
|
||||
# TODO: This!
|
||||
# def quick_reply(self, quick_reply, payload=None):
|
||||
|
@@ -28,6 +28,11 @@ def now():
|
||||
return int(time.time() * 1000)
|
||||
|
||||
|
||||
def json_minimal(data):
|
||||
"""Get JSON data in minimal form."""
|
||||
return json.dumps(data, separators=(",", ":"))
|
||||
|
||||
|
||||
def strip_json_cruft(text):
|
||||
"""Removes `for(;;);` (and other cruft) that preceeds JSON responses."""
|
||||
try:
|
||||
|
Reference in New Issue
Block a user