Skip to content

Commit

Permalink
ImportError fixed and refactored by pep8/pyflakes
Browse files Browse the repository at this point in the history
  • Loading branch information
adw0rd committed Jun 16, 2015
1 parent 7cab7e7 commit adaffb6
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pingdjack/client.py
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import urlsplit
except ImportError:
from urllib import urlopen
from urlparse import urlsplit
try:
from xmlrpc.client import ServerProxy, Error
except ImportError:
Expand All @@ -13,6 +14,7 @@

import html5lib


def external_urls(html, root_url):
'''
Finds external links in an HTML fragment and returns an iterator
Expand All @@ -25,14 +27,15 @@ def external_urls(html, root_url):
def is_external(url):
schema, host, path, query, fragment = urlsplit(url)
return schema in ('', 'http', 'https') and host != '' and \
(host != root_host or not path.startswith(root_path))
(host != root_host or not path.startswith(root_path))

doc = html5lib.parse(html)
walker = html5lib.treewalkers.getTreeWalker('etree')(doc)
links = (n for n in walker if n['type'] == 'StartTag' and n['name'] == 'a')
urls = (n['data'].get((None, 'href'), '') for n in links)
return (u.encode('utf-8') for u in urls if is_external(u))


def ping(source_url, target_url):
'''
Makes a pingback request to target_url on behalf of source_url, i.e.
Expand All @@ -49,13 +52,14 @@ def search_link(content):
try:
info = f.info()
server_url = info.get('X-Pingback', '') or \
search_link(f.read(512 * 1024))
search_link(f.read(512 * 1024))
if server_url:
server = ServerProxy(server_url)
server.pingback.ping(source_url, target_url)
finally:
f.close()


def ping_external_urls(source_url, html, root_url):
'''
Makes pingback requests to all external links in an HTML fragment.
Expand All @@ -66,6 +70,6 @@ def ping_external_urls(source_url, html, root_url):
for url in external_urls(html, root_url):
try:
ping(source_url, url)
except (IOError, xmlrpc.client.Error, ExpatError):
except (IOError, Error, ExpatError):
# One failed URL shouldn't block others
pass

0 comments on commit adaffb6

Please sign in to comment.