This repository has been archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
301 lines (228 loc) · 8.36 KB
/
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
import difflib
import json
import re
from collections import defaultdict
from typing import Dict, List
import pendulum
import requests
from bs4 import BeautifulSoup
from bs4.element import Tag, ResultSet
from config import LOGGER, MODEL_CHANGES
URL = (
'https://www.chromium.org'
'/chromium-os/developer-information-for-chrome-os-devices'
)
FILES = [
'boardnamedevices',
'boardnamedevices-1',
'boardnamedevices-2',
]
COL_MODEL = 'Model'
COL_BNAME = 'Board name(s)'
WL = 'WHITE_LABEL'
TO_REMOVE = re.compile('(x86-|_he)')
# Type-hints
JSON = Dict[str, List[str]] # {board name: [device 1, device 2]}
JSONS = Dict[str, JSON] # {json file: {board name: [...]}}
def sanitize(word: str) -> str:
"""Sanitizes a `word` to remove spaces including `\xa0`.
Args:
word (str): the word to sanitize
Returns:
str: the word, without the unnecessary stuff
"""
return word.get_text(strip=True).replace('\xa0', ' ')
def simplify_board_name(board_name: str) -> str:
"""Removes the following from board names:
- `x86-`, e.g. `x86-mario`
- `_he`, e.g. `x86-alex_he`
- `&` - e.g. `falco & falco_II`
- ',' - e.g. `hoho, but substitute a dp to vga chip` (why)
Args:
board_name: the board name to simplify
Returns:
str: a simplified board name
"""
if '&' in board_name:
# Always try to extract the first of two. For the time being,
# only legacy devices have this format and the second element
# is always the 'II' one.
board_name = board_name.split('&')[0].strip()
if ',' in board_name:
# hohoho
board_name = board_name.split(',')[0].strip()
return TO_REMOVE.sub('', board_name.lower())
def simplify_underscores(board_name: str) -> str:
"""Simplifies to use a common name `x` given a name in the format
`x_y`, discarding `_y` in the process.
Used for `boardnamedevices-2.json`.
Args:
board_name (str): the board name to simplify again
Returns:
str: a simplified board name
"""
if '_' in board_name:
# Always try to extract the first of two, because the `x`
# in the format `x_y` is the significant part of the name.
board_name = board_name.split('_')[0].strip()
return board_name
def parse_header(header: ResultSet) -> Dict[str, int]:
"""Parses header to extract `Model` and `Board name(s)` positions.
Args:
header (ResultSet): the table header with at least 'Model' and
'Board name(s)' columns
Returns:
Dict[str, int]: 0-based indices of the columns we want
"""
idx = {}
in_text = [td.get_text(strip=True) for td in header]
idx[COL_MODEL] = in_text.index(COL_MODEL)
idx[COL_BNAME] = in_text.index(COL_BNAME)
return idx
def iterate_table(table: Tag, header: ResultSet = None) -> JSONS:
"""Iterates through a `table` to retrieve `Model` and
`Board name(s)`, creating three distinct JSONs.
Args:
table (Tag): the table to parse
header (ResultSet, optional): the bs4 header to use;
defaults to None
Returns:
JSONS: each JSON with `Board name(s)` as the keys and
`Model` as the values
"""
jsons = {file: defaultdict(list) for file in FILES}
white_label = {file: defaultdict(int) for file in FILES}
if not header:
header = table.tr.find_all('td')
head = parse_header(header)
for row in table.find_all('tr')[1:]:
tds = row.find_all('td')
model = sanitize(tds[head[COL_MODEL]])
board_name = simplify_board_name(sanitize(tds[head[COL_BNAME]]))
# Fix errata in model names by comparing with the change list.
if model in MODEL_CHANGES:
model = MODEL_CHANGES[model]
for file, contents in jsons.items():
if file == FILES[2]:
board_name = simplify_underscores(board_name)
if not model and file == FILES[0]:
# For `boardnamedevices.json`, do not record blank names
continue
elif model:
contents[board_name].append(model)
else:
# This is a dummy value to remove later. This is to
# preserve device order as seen on the source page.
if WL not in contents[board_name]:
contents[board_name].append(WL)
white_label[file][board_name] += 1
for file, contents in jsons.items():
for board_name, content in contents.items():
content.sort()
if board_name in white_label[file]:
content.remove(WL)
count = white_label[file][board_name]
if count == 1:
content.append(f'White Label')
else:
content.append(f'White Label ({count})')
return jsons
def combine_dicts(dict_a: JSONS, dict_b: JSONS) -> JSONS:
"""Combines two dicts of dicts into one. Specifically,
combines two dicts with the keys = `FILES`. This is preferred
over `dict.update`, because `dict.update` also overwrites
existing information if keys collide.
Args:
dict_a (JSONS): dictionary a
dict_b (JSONS): dictionary b
Returns:
JSONS: a combined dict
"""
return {key: {**dict_a[key], **dict_b[key]} for key in dict_a}
def flatten_models(dicts: JSON) -> Dict[str, Dict[str, str]]:
"""Flattens models from `list` to `str`.
If multiple models are found on a given board, delimit
the results with ' | ', a pipe with surrounding spaces.
Args:
dicts (JSON): a dictionary of 3 defaultdicts, with
the 'default' being `list`
Returns:
Dict[str, Dict[str, str]]: flattened
"""
return {
file: {
board_name: ' | '.join(models)
for board_name, models
in contents.items()
}
for file, contents
in dicts.items()
}
def create_jsons() -> None:
"""Create all JSON files and diffs for easier consumption."""
jsons = {file: {} for file in FILES}
# with open('example.html', 'r') as example:
# soup = BeautifulSoup(example, 'html.parser')
page = requests.get(URL)
soup = BeautifulSoup(page.text, 'html.parser')
two_tables = soup.find('td', class_='sites-layout-tile')
try:
routers, usb_typec = two_tables.find_all('tbody')
except ValueError as e:
LOGGER.error(e)
return
main_header = (
soup.find('table', class_='goog-ws-list-header')
.find('tr').find_all('th')
)
main_table = soup.find('table', class_='sites-table').tbody
for table in [routers, usb_typec]:
jsons = combine_dicts(jsons, iterate_table(table))
jsons = flatten_models(
combine_dicts(
jsons, iterate_table(main_table, main_header)
)
)
for file, contents in jsons.items():
diff_file = f'{file}.diff'
file = f'{file}.json'
# If no changes were detected, don't bother with checking for
# diffs or create the file.
try:
with open(file, 'r') as f:
old = json.load(f)
if contents == old:
continue
except json.decoder.JSONDecodeError:
# Since the "old" file doesn't exist, just make it an empty
# dict. Because the file doesn't exist, there will be
# certainly a diff generated.
old = {}
with open(file, 'w') as f:
dump = json.dumps(contents, indent=4)
diff = [pendulum.today().strftime('%Y-%m-%d')]
diff.append('===')
diff.extend(
[
d
for d
in difflib.ndiff(
json.dumps(old, indent=4).splitlines(),
dump.splitlines()
)
if d.startswith('+') or d.startswith('-')
]
)
if old:
with open(diff_file, 'r') as g:
old_diff = '\n\n' + g.read()
else:
old_diff = ''
with open(diff_file, 'w') as g:
g.write('\n'.join(diff))
with open(diff_file, 'a') as g:
g.write(old_diff)
f.write(dump)
return
if __name__ == '__main__':
create_jsons()