-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_model.py
32 lines (22 loc) · 828 Bytes
/
url_model.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
__author__ = 'vasyanya'
import urllib
import os.path
#<editor-fold description="Helper functions">
def load_url(url):
content = urllib.urlopen(url).read()
return content
def save_to_file(data, file):
with open(file, "w") as f:
f.write(data)
def load_from_file(file):
with open(file, "r") as f:
return f.read()
#</editor-fold>
def get_content(url, use_test_data=False, update_test_data=False):
test_data_file = url.replace('http://', '').replace('/', '_').replace('?', '').replace('=', '') + ".html"
if use_test_data and not update_test_data and os.path.isfile(test_data_file):
return load_from_file(test_data_file)
content = load_url(url)
if update_test_data or not os.path.isfile(test_data_file):
save_to_file(content, test_data_file)
return content