-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
64 lines (49 loc) · 1.84 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import os
import requests
from bs4 import BeautifulSoup
try:
# progressbar is provided by progressbar2 on PYPI.
import progressbar
from requests_download import download, TrackerBase
except:
progressbar = None
download, TrackerBase = None, None
# class UIProgressTracker(TrackerBase):
class UIProgressTracker():
def __init__(self, progressbar):
self.progressbar = progressbar
self.recvd = 0
def on_start(self, response):
max_value = None
if 'content-length' in response.headers:
max_value = int(response.headers['content-length'])
self.progressbar.maxval = max_value
self.progressbar.start()
self.recvd = 0
def on_chunk(self, chunk):
self.recvd += len(chunk)
try:
self.progressbar.update(self.recvd)
except ValueError:
# Probably the HTTP headers lied.
pass
def on_finish(self):
self.progressbar.finish()
# progress = UIProgressTracker(progressbar.ProgressBar())
# download('https://github.com/takluyver/requests_download/archive/master.zip',
# 'requests_download.zip', trackers=[progress])
def download_with_progress_indicator(url, path, create_dirs=True):
if create_dirs:
os.makedirs(os.path.dirname(path), exist_ok=True)
if progressbar is not None and download is not None:
progress_tracker = UIProgressTracker(progressbar.ProgressBar())
download(url, path, trackers=[progress_tracker])
else:
print("""Downloading "%s" to "%s" ... please wait.""" % (url, path))
with open(path, 'wb') as fh:
response = requests.get(url)
fh.write(response.content)
print("""\tFinished downloading the file "%s" """ % (path))
def soupify_html(content):
return BeautifulSoup(content, 'lxml')