-
Notifications
You must be signed in to change notification settings - Fork 7
/
scrape.py
65 lines (54 loc) · 1.88 KB
/
scrape.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 sys
import time
from config import scraper_config, influx_config
from scraper.outputters.influxdb import InfluxDBOutputter
from scraper.outputters.printer import PrinterOutputter
from scraper.downloaders.requests import RequestsDownloader
MAX_RETRIES = scraper_config['max_retries']
MODEM_URL = scraper_config['modem_url']
MODEM_MODEL = scraper_config['modem_model']
OUTPUTTER = scraper_config['outputter']
def run_scraper():
"""
Starts the scraper.
"""
print("Modem scraper running.")
target = get_target(MODEM_MODEL)
outputter = get_outputter(OUTPUTTER)
downloader = RequestsDownloader()
retries = 0
while retries < MAX_RETRIES:
try:
body = downloader.download(MODEM_URL)
items = target.extract_items_from_html(body)
outputter.output(items)
retries = 0
except KeyboardInterrupt:
sys.exit()
except:
outputter.reset()
retries += 1
print("Error:", sys.exc_info())
time.sleep(scraper_config['poll_interval_seconds'])
print("Abort! Max retries reached:", MAX_RETRIES)
sys.exit(1)
def get_target(model):
if model == "SB6183":
from scraper.targets.arris_modem_SB6183 import ArrisModemSB6183
return ArrisModemSB6183()
if model == "TM1602AP2":
from scraper.targets.arris_modem_TM1602AP2 import ArrisModemTM1602AP2
return ArrisModemTM1602AP2()
if model == "CM802A":
from scraper.targets.arris_modem_CM820A import ArrisModemCM820A
return ArrisModemCM820A()
if model == "SB6190":
from scraper.targets.arris_modem_SB6190 import ArrisModemSB6190
return ArrisModemSB6190()
def get_outputter(output):
if output == 'influxdb':
return InfluxDBOutputter(influx_config)
else:
return PrinterOutputter()
if __name__ == "__main__":
run_scraper()