diff options
Diffstat (limited to 'lib/util.py')
-rw-r--r-- | lib/util.py | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/lib/util.py b/lib/util.py deleted file mode 100644 index 0f6bd81..0000000 --- a/lib/util.py +++ /dev/null @@ -1,47 +0,0 @@ -import re -import urllib.request -from html.parser import HTMLParser - -class Parser(HTMLParser): - def __init__(self, links=None): - HTMLParser.__init__(self) - if links is None: - self.links = [] - else: - self.links = links - self.title = [] - self.current_tag = None - def handle_starttag(self, tag, attrs): - self.current_tag = tag - if tag == 'a': - self.links.append(dict(attrs).get('href')) - def handle_data(self, data): - if self.current_tag == 'title': - self.title.append(data) - -def request(url, headers): - conn = urllib.request.Request( - url, - headers=headers - ) - r = urllib.request.urlopen(conn) - return r - -def download_file(url, headers, dest): - BLOCK = 16 * 1024 - conn = urllib.request.Request( - url, - headers=headers - ) - resp = urllib.request.urlopen(conn) - with open(dest, 'wb') as f: - while True: - chunk = resp.read(BLOCK) - if not chunk: - break - f.write(chunk) - -def sanitize_name(name): - safe = (' ', '.', '_', '-') - name = ''.join(c for c in name if c.isalnum() or c in safe).rstrip() - return name |