Skip to content

Commit

Permalink
Guard against libravatar failures
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Apr 22, 2024
1 parent 286507e commit 8202917
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions fedbadges/manual/libravatar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import hashlib
import logging
import sys
import traceback

import backoff
import click
import requests
from fedora_messaging.config import conf as fm_config
Expand All @@ -16,6 +19,31 @@
HTTP_TIMEOUT = 5


def _backoff_hdlr(details):
log.warning("Request to Libravatar failed, retrying.")


def _giveup_hdlr(details):
log.warning(
f"Request to Libravatar failed, giving up. {traceback.format_tb(sys.exc_info()[2])}"
)


@backoff.on_exception(
backoff.expo,
(requests.exceptions.SSLError, requests.exceptions.ConnectionError),
max_tries=10,
on_backoff=_backoff_hdlr,
on_giveup=_giveup_hdlr,
raise_on_giveup=False,
)
def query_libravatar(nickname):
openid = f"http://{nickname}.id.fedoraproject.org/"
hash = hashlib.sha256(openid.encode("utf-8")).hexdigest()
url = f"https://seccdn.libravatar.org/avatar/{hash}?d=404"
return requests.get(url, timeout=HTTP_TIMEOUT)


@click.command()
@option_debug
def main(debug):
Expand All @@ -39,23 +67,12 @@ def main(debug):
log.debug("Skipping %s", person)
continue

openid = f"http://{person.nickname}.id.fedoraproject.org/"
hash = hashlib.sha256(openid.encode("utf-8")).hexdigest()
url = f"https://seccdn.libravatar.org/avatar/{hash}?d=404"

response = None
for i in range(10):
log.debug("Try %s on %s", i, url)
try:
response = requests.get(url, timeout=HTTP_TIMEOUT)
break
except requests.exceptions.SSLError as e:
print(" * failed, trying again", str(e))

response = query_libravatar(person.nickname)
if response is None:
raise
# Query failed, ignore
continue

if response.status_code == 200:
if response.ok:
log.info("%s totally gets the mugshot badge.", person.nickname)
good.append(person)
award_badge(tahrir, badge, person.email, check_existing=False)
Expand Down

0 comments on commit 8202917

Please sign in to comment.