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