Clean up a few utility functions
This commit is contained in:
@@ -38,7 +38,7 @@ def graphql_queries_to_json(*queries):
|
|||||||
|
|
||||||
|
|
||||||
def graphql_response_to_json(content):
|
def graphql_response_to_json(content):
|
||||||
content = _util.strip_to_json(content) # Usually only needed in some error cases
|
content = _util.strip_json_cruft(content) # Usually only needed in some error cases
|
||||||
try:
|
try:
|
||||||
j = json.loads(content, cls=ConcatJSONDecoder)
|
j = json.loads(content, cls=ConcatJSONDecoder)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@@ -47,14 +47,12 @@ USER_AGENTS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
facebookEncoding = "UTF-8"
|
|
||||||
|
|
||||||
|
|
||||||
def now():
|
def now():
|
||||||
return int(time() * 1000)
|
return int(time() * 1000)
|
||||||
|
|
||||||
|
|
||||||
def strip_to_json(text):
|
def strip_json_cruft(text):
|
||||||
|
"""Removes `for(;;);` (and other cruft) that preceeds JSON responses."""
|
||||||
try:
|
try:
|
||||||
return text[text.index("{") :]
|
return text[text.index("{") :]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -66,15 +64,14 @@ def get_decoded_r(r):
|
|||||||
|
|
||||||
|
|
||||||
def get_decoded(content):
|
def get_decoded(content):
|
||||||
return content.decode(facebookEncoding)
|
return content.decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def parse_json(content):
|
def parse_json(content):
|
||||||
return json.loads(content)
|
try:
|
||||||
|
return json.loads(content)
|
||||||
|
except ValueError:
|
||||||
def get_json(r):
|
raise FBchatFacebookError("Error while parsing JSON: {!r}".format(content))
|
||||||
return json.loads(strip_to_json(get_decoded_r(r)))
|
|
||||||
|
|
||||||
|
|
||||||
def digitToChar(digit):
|
def digitToChar(digit):
|
||||||
@@ -155,11 +152,8 @@ def check_request(r, as_json=True):
|
|||||||
raise FBchatFacebookError("Error when sending request: Got empty response")
|
raise FBchatFacebookError("Error when sending request: Got empty response")
|
||||||
|
|
||||||
if as_json:
|
if as_json:
|
||||||
content = strip_to_json(content)
|
content = strip_json_cruft(content)
|
||||||
try:
|
j = parse_json(content)
|
||||||
j = json.loads(content)
|
|
||||||
except ValueError:
|
|
||||||
raise FBchatFacebookError("Error while parsing JSON: {!r}".format(content))
|
|
||||||
check_json(j)
|
check_json(j)
|
||||||
log.debug(j)
|
log.debug(j)
|
||||||
return j
|
return j
|
||||||
@@ -173,9 +167,8 @@ def get_jsmods_require(j, index):
|
|||||||
return j["jsmods"]["require"][0][index][0]
|
return j["jsmods"]["require"][0][index][0]
|
||||||
except (KeyError, IndexError) as e:
|
except (KeyError, IndexError) as e:
|
||||||
log.warning(
|
log.warning(
|
||||||
"Error when getting jsmods_require: {}. Facebook might have changed protocol".format(
|
"Error when getting jsmods_require: "
|
||||||
j
|
"{}. Facebook might have changed protocol".format(j)
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user