-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScraperManager.py
65 lines (52 loc) · 1.75 KB
/
ScraperManager.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
import logging
try:
from scraper import Scraper
except Exception:
from scrapers.scraper import Scraper
class Scraper_Manager:
"""
A class used to manage scrappers and logging
Atributes
---------
scrapers : dict
a dictionary containing key value pairs of scraper names (str)
and their respective scraper objects (Scraper)
Methods
-------
run_all()
runs all the scrapers and generates a log file
"""
def __init__(self, scrapers={}):
"""
Parameters
----------
scrapers : dict
a dictionary containing key value pairs of scraper names (str)
and their respective scraper objects (Scraper)
"""
self.scrapers = scrapers
# setup logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
datefmt="%m-%d %H:%M",
handlers=[logging.FileHandler("scrapers.log"), logging.StreamHandler()],
)
def run_all(self):
"""Runs all the scrapers stored in the scrapers dict and generates a log file"""
logging.info("Starting Program")
for scraper_name, scraper in self.scrapers.items():
try:
logging.debug(f"Starting: {scraper_name}")
scraper.run()
logging.debug(f"Finishing: {scraper_name}")
except Exception:
logging.exception(f"{scraper_name} failed because of: ")
logging.info("Finished Program")
logging.shutdown()
if __name__ == "__main__":
class TestScraper(Scraper):
def run(self):
print("Test")
manager = Scraper_Manager({"Test": TestScraper()})
manager.run_all()