From 18a3ffb90dc7a5be99bb13f05644d8a4855b83cb Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 7 May 2020 11:46:42 +0200 Subject: [PATCH] Fix Client.fetch_image_url in some cases Sometimes (or always?), jsmods require includes a JS version specifier. This means we couldn't find the url --- fbchat/_session.py | 2 ++ fbchat/_util.py | 11 +++++++---- tests/online/test_client.py | 13 ++++++++----- tests/test_util.py | 11 +++++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/fbchat/_session.py b/fbchat/_session.py index c7c7ce7..d7ca1bc 100644 --- a/fbchat/_session.py +++ b/fbchat/_session.py @@ -24,6 +24,8 @@ def parse_server_js_define(html: str) -> Mapping[str, Any]: # Find points where we should start parsing define_splits = SERVER_JS_DEFINE_REGEX.split(html) + # TODO: Extract jsmods "require" and "define" from `bigPipe.onPageletArrive`? + # Skip leading entry _, *define_splits = define_splits diff --git a/fbchat/_util.py b/fbchat/_util.py index d6e9376..ce23f87 100644 --- a/fbchat/_util.py +++ b/fbchat/_util.py @@ -63,16 +63,19 @@ def generate_offline_threading_id(): return str(int(msgs, 2)) +def remove_version_from_module(module): + return module.split("@", 1)[0] + + def get_jsmods_require(require) -> Mapping[str, Sequence[Any]]: rtn = {} for item in require: if len(item) == 1: (module,) = item - rtn[module] = [] + rtn[remove_version_from_module(module)] = [] continue - method = "{}.{}".format(item[0], item[1]) - requirements = item[2] - arguments = item[3] + module, method, requirements, arguments = item + method = "{}.{}".format(remove_version_from_module(module), method) rtn[method] = arguments return rtn diff --git a/tests/online/test_client.py b/tests/online/test_client.py index 8021980..3a17d8b 100644 --- a/tests/online/test_client.py +++ b/tests/online/test_client.py @@ -46,11 +46,6 @@ def test_undocumented(client): client.fetch_unseen() -@pytest.mark.skip(reason="need a way to get an image id") -def test_fetch_image_url(client): - client.fetch_image_url("TODO") - - @pytest.fixture def open_resource(pytestconfig): def get_resource_inner(filename): @@ -60,6 +55,14 @@ def open_resource(pytestconfig): return get_resource_inner +def test_upload_and_fetch_image_url(client, open_resource): + with open_resource("image.png") as f: + ((id, mimetype),) = client.upload([("image.png", f, "image/png")]) + assert mimetype == "image/png" + + assert client.fetch_image_url(id).startswith("http") + + def test_upload_image(client, open_resource): with open_resource("image.png") as f: _ = client.upload([("image.png", f, "image/png")]) diff --git a/tests/test_util.py b/tests/test_util.py index 8214d3a..02fe1b4 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -68,6 +68,17 @@ def test_get_jsmods_require(): } +def test_get_jsmods_require_version_specifier(): + data = [ + ["DimensionTracking@1234"], + ["CavalryLoggerImpl@2345", "startInstrumentation", [], []], + ] + assert get_jsmods_require(data) == { + "DimensionTracking": [], + "CavalryLoggerImpl.startInstrumentation": [], + } + + def test_get_jsmods_require_get_image_url(): data = [ [