-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_scraper.py
348 lines (304 loc) · 12.5 KB
/
base_scraper.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import base64
import json
from requests import Session
class GithubContents:
# https://github.com/simonw/github-contents/blob/main/github_contents.py
class NotFound(Exception):
pass
class UnknownError(Exception):
pass
def __init__(self, owner, repo, token, branch="main"):
self.owner = owner
self.repo = repo
self.token = token
self.session = Session()
self.branch = branch
def base_url(self):
return "https://api.github.com/repos/{}/{}".format(self.owner, self.repo)
def headers(self):
return {"Authorization": "token {}".format(self.token)}
def read(self, filepath):
"Returns (file_contents_in_bytes, sha1)"
# Try reading using content API
content_url = "{}/contents/{}".format(self.base_url(), filepath)
response = self.session.get(content_url, headers=self.headers())
if response.status_code == 200:
data = response.json()
if not data.get("content") and data.get("download_url"):
return self.session.get(data["download_url"], headers=self.headers()).text, data["sha"]
return base64.b64decode(data["content"]), data["sha"]
elif response.status_code == 404:
raise self.NotFound(filepath)
elif response.status_code == 403:
# It's probably too large
if response.json()["errors"][0]["code"] != "too_large":
raise self.UnknownError(response.content)
else:
return self.read_large(filepath)
else:
raise self.UnknownError(response.content)
def read_large(self, filepath):
"Returns (file_contents_in_bytes, sha1)"
default_tree = self.session.get(
self.base_url() + "/git/trees/{}?recursive=1".format(self.branch),
headers=self.headers(),
).json()
try:
tree_entry = [t for t in default_tree["tree"] if t["path"] == filepath][0]
except IndexError:
raise self.NotFound(filepath)
data = self.session.get(tree_entry["url"], headers=self.headers()).json()
return base64.b64decode(data["content"]), data["sha"]
def write(
self, filepath, content_bytes, sha=None, commit_message="", committer=None
):
if not isinstance(content_bytes, bytes):
raise TypeError("content_bytes must be a bytestring")
github_url = "{}/contents/{}".format(self.base_url(), filepath)
payload = {
"path": filepath,
"content": base64.b64encode(content_bytes).decode("latin1"),
"message": commit_message,
}
if sha:
payload["sha"] = sha
if committer:
payload["committer"] = committer
response = self.session.put(github_url, json=payload, headers=self.headers())
if (
response.status_code == 403
and response.json()["errors"][0]["code"] == "too_large"
):
return self.write_large(filepath, content_bytes, commit_message, committer)
elif (
sha is None
and response.status_code == 422
and "sha" in response.json().get("message", "")
):
# Missing sha - we need to figure out the sha and try again
_, old_sha = self.read(filepath)
return self.write(
filepath,
content_bytes,
sha=old_sha,
commit_message=commit_message,
committer=committer,
)
elif response.status_code in (201, 200):
updated = response.json()
return updated["content"]["sha"], updated["commit"]["sha"]
else:
raise self.UnknownError(
str(response.status_code) + ":" + repr(response.content)
)
def write_large(self, filepath, content_bytes, commit_message="", committer=None):
if not isinstance(content_bytes, bytes):
raise TypeError("content_bytes must be a bytestring")
# Create a new blob with the file contents
created_blob = self.session.post(
self.base_url() + "/git/blobs",
json={
"encoding": "base64",
"content": base64.b64encode(content_bytes).decode("latin1"),
},
headers=self.headers(),
).json()
# Retrieve default tree sha
default_branch_sha = self.session.get(
self.base_url() + "/git/trees/{}?recursive=1".format(self.branch),
headers=self.headers(),
).json()["sha"]
# Construct a new tree
created_tree = self.session.post(
self.base_url() + "/git/trees",
json={
"base_tree": default_branch_sha,
"tree": [
{
"mode": "100644", # file (blob),
"path": filepath,
"type": "blob",
"sha": created_blob["sha"],
}
],
},
headers=self.headers(),
).json()
# Create a commit which references the new tree
payload = {
"message": commit_message,
"parents": [default_branch_sha],
"tree": created_tree["sha"],
}
if committer:
payload["committer"] = committer
created_commit = self.session.post(
self.base_url() + "/git/commits", json=payload, headers=self.headers()
).json()
# Move HEAD reference on master to the new commit
self.session.patch(
self.base_url() + "/git/refs/heads/{}".format(self.branch),
json={"sha": created_commit["sha"]},
headers=self.headers(),
).json()
return created_blob["sha"], created_commit["sha"]
def branch_exists(self):
assert self.branch
return (
self.session.get(
self.base_url() + "/git/refs/heads/{}".format(self.branch),
headers=self.headers(),
).status_code
== 200
)
class Scraper:
owner = None
repo = None
filepath = None
committer = {"name": "outage-scrapers", "email": "none@example.com"}
test_mode = False
def __init__(self, github_token):
self.last_data = None
self.last_sha = None
self.github_token = github_token
def create_message(self, new_data):
return "Created {}".format(self.filepath)
def update_message(self, old_data, new_data):
return "Updated {}".format(self.filepath)
def fetch_data(self):
return []
def scrape_and_store(self):
# We need to store the data
github = GithubContents(self.owner, self.repo, self.github_token)
if not self.last_data or not self.last_sha:
# Check and see if it exists yet
try:
content, sha = github.read(self.filepath)
self.last_data = json.loads(content)
self.last_sha = sha
except GithubContents.NotFound:
self.last_data = None
self.last_sha = None
data = self.fetch_data()
if data is None:
print("{}; Data was None".format(self.filepath))
return
if self.test_mode and not self.github_token:
print(json.dumps(data, indent=2))
return
if self.last_data == data:
print("%s: Nothing changed" % self.filepath)
return
if self.last_sha:
print("Updating {}".format(self.filepath))
message = self.update_message(self.last_data, data)
else:
print("Creating {}".format(self.filepath))
message = self.create_message(data)
if self.test_mode:
print(message)
print()
print(json.dumps(data, indent=2))
return
content_sha, commit_sha = github.write(
filepath=self.filepath,
content_bytes=json.dumps(data, indent=2).encode("utf8"),
sha=self.last_sha,
commit_message=message,
committer=self.committer,
)
self.last_sha = content_sha
self.last_data = data
print("https://github.com/{}/{}/commit/{}".format(self.owner, self.repo, commit_sha))
class DeltaScraper(Scraper):
"""
The fetch_data() method should return a list of dicts. Each dict
should have a key that can be used to identify the row in that dict.
Then you define a display_record(record) method that returns a string.
"""
record_key = None
show_changes = False
noun = "record"
plural = None
source_url = None
@property
def display_name(self):
return self.filepath.replace(".json", "")
@property
def noun_plural(self):
return self.plural or (self.noun + "s")
def display_record(self, record):
pairs = []
for key, value in record.items():
pairs.append("{} = {}".format(key, value))
return "\n".join(pairs)
def display_changes(self, old_record, new_record):
changes = []
for key in old_record:
prev = old_record[key]
next = new_record.get(key)
if prev != next:
changes.append("{}: {} => {}".format(key, prev, next))
return "\n".join(changes)
def create_message(self, new_records):
return self.update_message([], new_records, "Created")
def update_message(self, old_records, new_records, verb="Updated"):
previous_ids = [record[self.record_key] for record in old_records]
current_ids = [record[self.record_key] for record in new_records]
added_ids = [id for id in current_ids if id not in previous_ids]
removed_ids = [id for id in previous_ids if id not in current_ids]
message_blocks = []
if added_ids:
messages = []
messages.append("{} new {}:".format(len(added_ids), self.noun if len(added_ids) == 1 else self.noun_plural,))
for id in added_ids:
record = [r for r in new_records if r[self.record_key] == id][0]
messages.append(self.display_record(record))
message_blocks.append(messages)
if removed_ids:
messages = []
messages.append(
"{} {} removed:".format(len(removed_ids), self.noun if len(removed_ids) == 1 else self.noun_plural,)
)
for id in removed_ids:
record = [r for r in old_records if r[self.record_key] == id][0]
messages.append(self.display_record(record))
message_blocks.append(messages)
# Add useful rendering of CHANGED records as well
changed_records = []
for new_record in new_records:
try:
old_record = [r for r in old_records if r[self.record_key] == new_record[self.record_key]][0]
except IndexError:
continue
if json.dumps(old_record, sort_keys=True) != json.dumps(new_record, sort_keys=True):
changed_records.append((old_record, new_record))
if self.show_changes and changed_records:
messages = []
messages.append(
"{} {} changed:".format(len(removed_ids), self.noun if len(removed_ids) == 1 else self.noun_plural,)
)
for old_record, new_record in changed_records:
messages.append(self.display_changes(old_record, new_record))
message_blocks.append(messages)
blocks = []
for message_block in message_blocks:
block = "\n".join(message_block)
blocks.append(block.strip())
if self.source_url:
blocks.append("Detected on {}".format(self.source_url))
body = "\n\n".join(blocks)
summary = []
if added_ids:
summary.append("{} {} added".format(len(added_ids), self.noun if len(added_ids) == 1 else self.noun_plural,))
if removed_ids:
summary.append("{} {} removed".format(len(removed_ids), self.noun if len(removed_ids) == 1 else self.noun_plural,))
if changed_records:
summary.append(
"{} {} changed".format(len(changed_records), self.noun if len(changed_records) == 1 else self.noun_plural,)
)
if summary:
summary_text = self.display_name + ": " + (", ".join(summary))
else:
summary_text = "{} {}".format(verb, self.display_name)
return "{}\n\n{}".format(summary_text, body)