48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
|
import logging
|
||
|
import requests
|
||
|
from abc import *
|
||
|
from threading import Timer
|
||
|
|
||
|
|
||
|
class Grabber(ABC):
|
||
|
db = None
|
||
|
_interval = 60
|
||
|
_running = False
|
||
|
|
||
|
def __init__(self, db, interval):
|
||
|
self.db = db
|
||
|
self._interval = interval
|
||
|
|
||
|
@property
|
||
|
@abstractmethod
|
||
|
def feed_url(self):
|
||
|
raise NotImplementedError
|
||
|
|
||
|
@abstractmethod
|
||
|
def grab(self):
|
||
|
raise NotImplementedError
|
||
|
|
||
|
def request(self):
|
||
|
response = requests.get(self.feed_url)
|
||
|
response.raise_for_status()
|
||
|
return response.content
|
||
|
|
||
|
def timer(self):
|
||
|
if self._running:
|
||
|
self.grab()
|
||
|
Timer(self._interval, self.timer).start()
|
||
|
|
||
|
def start(self):
|
||
|
if not self._running:
|
||
|
self._running = True
|
||
|
self.timer()
|
||
|
else:
|
||
|
logging.error(f"Grabber for {self.__class__.__name__} already started.")
|
||
|
|
||
|
def stop(self):
|
||
|
if self._running:
|
||
|
self._running = False
|
||
|
else:
|
||
|
logging.error(f"Grabber for {self.__class__.__name__} already stopped.")
|
||
|
|