-
Notifications
You must be signed in to change notification settings - Fork 18
/
processor.py
181 lines (159 loc) · 6.43 KB
/
processor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import urllib.error
from urllib import request, parse
from urllib.error import HTTPError, URLError
import csv
import json
import os
import time
from functools import wraps
import ssl
class Processor:
# Type should be one of the following: 'dcat', 'arcgis', 'usmart'
def __init__(self, type):
self.type = type
self.header = [
"Title",
"Owner",
"PageURL",
"AssetURL",
"FileName",
"DateCreated",
"DateUpdated",
"FileSize",
"FileSizeUnit",
"FileType",
"NumRecords",
"OriginalTags",
"ManualTags",
"License",
"Description",
]
self.urls = {}
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
"""Retry calling the decorated function using an exponential backoff.
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
:param ExceptionToCheck: the exception to check. may be a tuple of
exceptions to check
:type ExceptionToCheck: Exception or tuple
:param tries: number of times to try (not retry) before giving up
:type tries: int
:param delay: initial delay between retries in seconds
:type delay: int
:param backoff: backoff multiplier e.g. value of 2 will double the delay
each retry
:type backoff: int
:param logger: logger to use. If None, print
:type logger: logging.Logger instance
"""
def deco_retry(f):
@wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except ExceptionToCheck as e:
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
if logger:
logger.warning(msg)
else:
print(msg)
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return f_retry # true decorator
return deco_retry
@retry(HTTPError, tries=3, delay=60, backoff=2)
def urlopen_with_retry(self, req):
# TEMP FIX: Handle Angus SSL errors
ctx = ssl.create_default_context()
if "opendata.angus.gov.uk" in req.host:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return request.urlopen(req, context=ctx)
def get_urls(self):
with open("sources.csv", "r", encoding="utf-8") as file:
csv_file = csv.DictReader(file)
for row in csv_file:
if row["Processor"] == self.type:
self.urls[row["Name"]] = row["Source URL"]
for r in csv_file:
print("r", r)
def get_json(self, url):
req = request.Request(url)
try:
resp = self.urlopen_with_retry(req)
decoded_resp = resp.read().decode()
return json.loads(decoded_resp)
except HTTPError as err1:
print(url, "cannot be accessed. The URL returned:", err1.code, err1.reason)
error_dict = {
"url": url,
"error_code": err1.code,
"error_reason": err1.reason,
}
except URLError as err2:
print(type(err2))
print(url, "cannot be accessed. The URL returned:", err2.reason)
error_dict = {
"url": url,
"error_code": "",
"error_reason": str(err2.reason),
}
with open("log.json", "a") as f:
json.dump(error_dict, f)
with open("log.md", "a") as file:
file.write(
f'| {error_dict["url"]} | {error_dict["error_code"]} | {error_dict["error_reason"]} | \n'
)
return "NULL"
def get_license(self, dataset):
try:
# Known Licenses info
allLicenses = [
"http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/",
"http://www.nationalarchives.gov.uk/doc/open-government-licence/version/2/",
"http://opendatacommons.org/licenses/odbl/1-0/",
"Open Data Commons Open Database License 1.0",
"uk-ogl",
"UK Open Government Licence (OGL)",
"Open Government Licence 3.0 (United Kingdom)",
"OGL3",
"https://creativecommons.org/licenses/by/4.0/legalcode",
"Creative Commons Attribution 4.0",
"https://creativecommons.org/licenses/by-sa/3.0/",
]
# Return License info, If License 'url' key available
if "url" in dataset["attributes"]["structuredLicense"]:
return dataset["attributes"]["structuredLicense"]["url"]
# Check for License in 'text' key and return the license info, if license 'url' key not available
elif "text" in dataset["attributes"]["structuredLicense"]:
for license in allLicenses:
if license in dataset["attributes"]["structuredLicense"]["text"]:
return license
return ""
# Return '', if 'url' & 'text' key not available
else:
return ""
except:
return ""
def write_csv(self, fname, prepped):
with open(fname, "w", newline="", encoding="utf-8") as csvf:
w = csv.writer(csvf, quoting=csv.QUOTE_MINIMAL, lineterminator="\n")
w.writerow(self.header)
for r in prepped:
if r[-1]:
r[-1] = r[-1].replace("\n", " ")
w.writerow(r)
def write_json(self, fname, prepped):
with open(fname, "w", encoding="utf8") as json_file:
json.dump(prepped, json_file, indent=4)
def get_datasets(self, owner, url, fname):
print("Override this method")
def process(self, file_type = "csv"):
self.get_urls()
for name, url in self.urls.items():
print(name)
self.get_datasets(name, url, os.path.join("data", self.type, f"{name}.{file_type}"))