1
0
Fork 0
NewsGrabber/grabber.py

48 lines
1.0 KiB
Python
Raw Normal View History

2020-01-18 15:34:40 +00:00
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.")