forked from openkentuckiana/kubra-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_scraper.py
187 lines (153 loc) · 6.47 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
from github_contents import GithubContents
import json
class Scraper:
owner = None
repo = None
filepath = None
committer = {"name": "outage-scrapers", "email": "[email protected]"}
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):
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
# 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
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)