diff --git a/collector.py b/collector.py index 0fa74c5..deb3625 100644 --- a/collector.py +++ b/collector.py @@ -8,6 +8,7 @@ from plugins.system.disk_plugin import DiskPlugin from plugins.system.network_plugin import NetworkPlugin from plugins.system.temperatures_plugin import TemperaturesPlugin from plugins.system.ping_plugin import PingPlugin +from plugins.finance.robor_plugin import RoborPlugin class Collector(object): @@ -18,7 +19,10 @@ class Collector(object): DiskPlugin(), NetworkPlugin(), TemperaturesPlugin(), - PingPlugin() + PingPlugin(), + + # finance + RoborPlugin() ] self.event = Event() diff --git a/config.py b/config.py index 6234e99..942c4d2 100644 --- a/config.py +++ b/config.py @@ -48,4 +48,10 @@ PING_HOSTS = [ ] # How often to send pings, as a number of INTERVALs -PING_FREQUENCY = 10 \ No newline at end of file +PING_FREQUENCY = 10 + +### ROBOR +# Romanian Interbank Offer Rate +ROBOR_FIELDS = [ + 'ROBOR 6M' +] diff --git a/plugins/finance/robor_plugin.py b/plugins/finance/robor_plugin.py new file mode 100644 index 0000000..75238a5 --- /dev/null +++ b/plugins/finance/robor_plugin.py @@ -0,0 +1,80 @@ +import datetime + +import lxml.html +import requests +from peewee import * +from playhouse.shortcuts import model_to_dict + +import config +from database import BaseModel +from plugins.plugin import Plugin + + +class Robor(BaseModel): + date = DateField(index=True, default=datetime.date.today(), null=False) + field = TextField(null=False) + value = FloatField(null=False) + + +class RoborPlugin(Plugin): + models = [Robor] + + def __init__(self): + self.__table = None + + def get_column_index(self, table, column_name): + header_row = table.find('tr') + for elem in header_row.iter('th'): + if column_name in elem.text_content(): + return header_row.index(elem) + + return None + + def values_newer_than(self, table, oldest_date: datetime.date, col_date, col_value): + # Items are ordered descending, so stop when we reach date + for row in table.iter('tr'): + dt_str = row[col_date].text_content() + val_str = row[col_value].text_content() + + try: + dt = datetime.datetime.strptime(dt_str, '%d/%b/%Y') + val = float(val_str) + except ValueError: + continue + + dt = dt.date() + if (oldest_date is not None) and (dt <= oldest_date): + break + + yield (dt, val) + + def execute(self): + # Get last existing date + latest_date = Robor.select(Robor.date) \ + .order_by(Robor.date.desc()) \ + .limit(1) \ + .scalar() + + # Fetch & parse data + response = requests.get('https://www.bnro.ro/StatisticsReportHTML.aspx?icid=801&table=642', verify=False) + response.raise_for_status() + + html = lxml.html.fromstring(response.text) + + # Read data from table + table = html.find('.//table[@class="stat_table"]') + + for field in config.ROBOR_FIELDS: + col_date = self.get_column_index(table, 'Date') + col_value = self.get_column_index(table, field) + + for date, value in self.values_newer_than(table, latest_date, col_date, col_value): + entry = Robor() + entry.date = date + entry.field = field + entry.value = value + entry.save() + print(model_to_dict(entry)) + + + diff --git a/plugins/finance/stocks_plugin.py b/plugins/finance/stocks_plugin.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index 9612e6d..2e573ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,8 @@ -peewee \ No newline at end of file +peewee + +# Used in most system plugins +psutil + +# Finance plugins +requests +lxml \ No newline at end of file