1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

Fixed an uncaught exception caused by broken URLs.

Examples of fixed pages: photo/11689, stackoverflow/315911
This commit is contained in:
Alexei Baboulevitch 2014-05-12 14:35:28 +02:00
parent a06d2a4c55
commit 7f6ed7b438

View File

@ -827,7 +827,12 @@ def _rewrite_html(html, app_url_root, sites_by_urls):
internal_link = False
url = t.get('href', None)
if url:
try:
host = urllib2.Request(url).get_host()
except ValueError:
# invalid URL or local anchor, leaving as-is
internal_link = True
else:
site = sites_by_urls.get(host, None)
if site:
# rewrite this URL for stackdump
@ -836,18 +841,17 @@ def _rewrite_html(html, app_url_root, sites_by_urls):
question_id = question_id.groupdict()['id']
url = '%s%s/%s' % (app_url_root, site.key, question_id)
t.set('href', url)
t.set('class', t.get('class', '') + ' internal-link')
internal_link = True
answer_id = SE_ANSWER_ID_RE.search(url)
if answer_id:
answer_id = answer_id.groupdict()['id']
url = '%s%s/a/%s' % (app_url_root, site.key, answer_id)
t.set('href', url)
t.set('class', t.get('class', '') + ' internal-link')
internal_link = True
if not internal_link:
if internal_link:
t.set('class', t.get('class', '') + ' internal-link')
else:
t.set('class', t.get('class', '') + ' external-link')
# get a string back