Skip to content

Instantly share code, notes, and snippets.

@miketaylr
Last active August 29, 2015 13:56
Show Gist options
  • Save miketaylr/9075703 to your computer and use it in GitHub Desktop.
Save miketaylr/9075703 to your computer and use it in GitHub Desktop.
Bugzilla Tech Evangelism URL status checker
#!/usr/bin/env python
import json
import requests
# This search corresponds to: Tech Evangelism bugs that are UNCONFIRMED, NEW,
# or REOPENED. When we loop through the response, we skip bugs with an empty
# url field.
SEARCH_URL = ("https://api-dev.bugzilla.mozilla.org/latest/bug?"
"bug_status=UNCONFIRMED"
"&bug_status=NEW"
"&bug_status=REOPENED"
"&product=Tech%20Evangelism"
"&include_fields=url,id")
FX_UA = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) "
"Gecko/20100101 Firefox/27.0")
api = requests.get(SEARCH_URL, headers={'Accept': 'application/json',
'Content-Type': 'application/json'})
r = api.json()
s = requests.Session()
# In theory a 3XX redirect could mean the bug is INVALID
s.max_redirects = 0
for bug in r['bugs']:
try:
if bug['url']:
# Arbitary timeout of 5 seconds so this doesn't take ages.
new_r = s.get(bug['url'], timeout=5, headers={'User-Agent': FX_UA})
if new_r.status_code != 200:
print("{0}: {1} ({2})".format(new_r.status_code, bug['url'],
bug['id']))
# Types of Exceptions we care about:
# The site probably no longer exists
except requests.exceptions.ConnectionError as e:
print("ConnectionError for {0} ({1})".format(bug['url'], bug['id']))
# The site took too long to connect. Probably worth checking manually.
except requests.exceptions.Timeout as e:
print("Timeout for {0} ({1})".format(bug['url'], bug['id']))
# The site redirected to somewhere else. This could mean the original site
# no longer exists and should be INVALID. Or it could be something as
# simple as redirecting to a login page (and is still VALID)
except requests.exceptions.TooManyRedirects as e:
print("Redirect for {1} ({2})".format(bug['url'], bug['id']))
@miketaylr
Copy link
Author

Oh, should probably filter out the Mobile product ones from this. Otherwise the desktop UA will cause too many redirects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment