Skip to content

Commit

Permalink
lamadava: Fallback to GQL if v2 fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocj89 committed Jun 10, 2024
1 parent 03d9873 commit 0e9038a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions eas/api/instagram/lamadava.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def _session(): # pragma: no cover
)
)
def fetch_comments(media_pk): # pragma: no cover
try:
return _fetch_comments_v2(media_pk)
except Exception: # pylint: disable=W0703
LOG.info(
"Failed to fetch comments via v2 API, falling back to gql", exc_info=True
)
return _fetch_comments_gql(media_pk)


def _fetch_comments_v2(media_pk): # pragma: no cover
LOG.info("Sending request to lamadava for %s", media_pk)
response = _session().get(
"https://api.lamadava.com/v2/media/comments",
Expand All @@ -45,3 +55,23 @@ def fetch_comments(media_pk): # pragma: no cover
except KeyError:
LOG.warning("Failed lamadava request! %s", response.text)
raise


def _fetch_comments_gql(media_pk): # pragma: no cover
LOG.info("Sending request to lamadava for %s", media_pk)
response = _session().get(
"https://api.lamadava.com/gql/comments",
params={
"media_id": media_pk,
"amount": 50,
"access_key": LAMADAVA_APIK,
},
timeout=ONE_MINUTE * 2,
)
if not response.ok:
LOG.warning("Failed lamadava request! %s", response.text)
with contextlib.suppress(Exception):
if response.json()["exc_type"] == "NotFoundError":
return []
response.raise_for_status()
return response.json()

0 comments on commit 0e9038a

Please sign in to comment.