diff --git a/fedbadges/manual/libravatar.py b/fedbadges/manual/libravatar.py index 2a6c12c..2f0697f 100644 --- a/fedbadges/manual/libravatar.py +++ b/fedbadges/manual/libravatar.py @@ -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 @@ -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): @@ -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)