-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_scraper.py
46 lines (40 loc) · 1.27 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
import abc
import json
import re
class BaseScraper(abc.ABC):
def __init__(self, scraper : str):
known_scrapers = ["mains_2021", "mains_2022"]
if scraper not in known_scrapers:
raise ValueError("Given scraper doesn't exist.")
self.scraper = scraper
self.data = {}
with open(f'scrapers/{self.scraper}/metadata.json') as f:
self.metadata = json.load(f)
def sane_int(self, x : str):
x = x.strip()
if x in self.metadata["null_list"]:
return None
try:
return int(x)
except ValueError:
return None
def sane_float(self, x : str):
x = x.strip()
if x in self.metadata["null_list"]:
return None
try:
return float(x)
except ValueError:
return None
def sane_str(self, x : str):
x = x.strip()
if x in self.metadata["null_list"]:
return None
return x
def get_regexes(self, kind : str):
scraper_file = open(f'scrapers/{self.scraper}/{kind}.json')
data = json.load(scraper_file).items()
scraper_file.close()
return dict((k, re.compile(i, re.M)) for (k, i) in data)
def dump_to(self, file):
json.dump(self.data, file)