From 73cfcbd1709ce651f4adf0206e3b1299652ae615 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Fri, 7 Jun 2019 13:01:33 +0200 Subject: [PATCH] Fixed broken json parsing --- retribute_api/search/sources.py | 7 +++++-- retribute_api/search/webfinger.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/retribute_api/search/sources.py b/retribute_api/search/sources.py index 80159ad..7e01bbe 100644 --- a/retribute_api/search/sources.py +++ b/retribute_api/search/sources.py @@ -1,5 +1,6 @@ from django.conf import settings from django.utils import timezone +import json from .. import providers from . import activitypub @@ -38,6 +39,7 @@ class Activitypub(Source): ] async def get(self, lookup, session, cache): + try: actor_data = await cache.get("activitypub:profile:{}".format(lookup)) except cache.NotFound: @@ -45,7 +47,8 @@ class Activitypub(Source): lookup, headers={"Accept": "application/activity+json"} ) as response: response.raise_for_status() - actor_data = await response.json() + text = await response.read() + actor_data = json.loads(text) await cache.set("activitypub:profile:{}".format(lookup), actor_data) serializer = activitypub.ActorSerializer(data=actor_data) serializer.is_valid(raise_exception=True) @@ -55,7 +58,7 @@ class Activitypub(Source): data = { "links": activitypub.extract_urls_from_attachments( - serializer.validated_data["attachment"] + serializer.validated_data.get("attachment", []) ) + activitypub.extract_urls_from_summary( serializer.validated_data["summary"] diff --git a/retribute_api/search/webfinger.py b/retribute_api/search/webfinger.py index c89a7be..a3f1534 100644 --- a/retribute_api/search/webfinger.py +++ b/retribute_api/search/webfinger.py @@ -1,3 +1,5 @@ +import json + from rest_framework import serializers from . import exceptions @@ -19,7 +21,8 @@ async def lookup(name, session, cache): params={"resource": "acct:{}".format(name)}, ) as response: response.raise_for_status() - payload = await response.json() + text = await response.read() + payload = json.loads(text) await cache.set("webfinger:links:{}".format(name), payload) return payload -- GitLab