summaryrefslogtreecommitdiff
path: root/tab_main.py
blob: 46ef699662b66245e0a5e19c19f8a1cc37aa5895 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
import logging

from pyinflux.client import Line

import jobs.clever_tanken
import jobs.davis_vantage
import jobs.esg
import jobs.hplq1300n
import jobs.prix_carburant
import jobs.swr_wetter
import jobs.tankerkoenig
import jobs.telexoo
import jobs.transferwise
import scheduler.influxdb

logging.basicConfig(level=logging.INFO)

s = scheduler.Scheduler()

transform_swr = lambda v: Line(v['name'], {}, {'value': v['value']})
s.add_job(scheduler.at(minute='0,15,30,45', name='SWR Wetter',
                       action=lambda: map(transform_swr, jobs.swr_wetter.job('DE0008834'))))

transform_laserjet = lambda v: Line('hplq1300n.toner.{}'.format(v.hostname), {}, {'value': v.value})
s.add_job(scheduler.at(minute='*/5', name="Laserjet Status",
                       action=lambda: transform_laserjet(jobs.hplq1300n.job('10.1.0.10'))))

transform_telexoo = lambda qoute: Line('telexoo.{}{}_X'.format(qoute.curr_from, qoute.curr_to), {},
                                       {'value': qoute.rate})
s.add_job(scheduler.every(minutes=10, name="Telexoo.com-CHFGBP",
                          action=lambda: transform_telexoo(jobs.telexoo.execute("CHF", "GBP"))))
s.add_job(scheduler.every(minutes=10, name="Telexoo.com-CHFEUR",
                          action=lambda: transform_telexoo(jobs.telexoo.execute("CHF", "EUR"))))
s.add_job(scheduler.every(minutes=10, name="Telexoo.com-EURCHF",
                          action=lambda: transform_telexoo(jobs.telexoo.execute("EUR", "CHF"))))
s.add_job(scheduler.every(minutes=10, name="Telexoo.com-CHFPLN",
                          action=lambda: transform_telexoo(jobs.telexoo.execute("CHF", "PLN"))))

transform_transferwise = lambda d: Line('transferwise.{}{}_X'.format(d.curr_from, d.curr_to), {}, {
    'value': d.rate})
s.add_job(scheduler.every(minutes=10, name='Transferwise-CHFEUR',
                          action=lambda: transform_transferwise(jobs.transferwise.job('CHF', 'EUR'))))
s.add_job(scheduler.every(minutes=10, name='Transferwise-EURCHF',
                          action=lambda: transform_transferwise(jobs.transferwise.job('EUR', 'CHF'))))

transform_esg = lambda products: [Line('esg', {'sku': product.sku, 'product_name': product.name},
                                       {'price': product.price}) for product in products]
s.add_job(scheduler.at(minute="0", hour="8,10,12,14,16,18,20", name="ESG",
                       action=lambda: transform_esg(jobs.esg.execute())))

s.add_job(scheduler.every(hours=2, name="Wettermichel.de", action=
lambda: [Line('wettermichel.{}'.format(name), {}, {'value': value})
         for name, value in
         jobs.davis_vantage.load('http://wettermichel.de/davis/con_davis.php').items()]))


def execute_prix_carburant():
    for station in jobs.prix_carburant.execute('1630001', '1210003', '1630003', '1210002', '1710001',
                                               '67760001', '67240002', '67452001',
                                               '68740001',  # Fessenheim
                                               '67500009',  # Hagenau
                                               '67116002'):  # Reichstett
        for fuelname, price in station.prices.items():
            tags = {'name': station.station_name, 'id': 'prix_carburant:{}'.format(station.id)}
            fields = {'value': price}
            if fuelname == "SP95":
                yield Line('tankstelle.SP95-E5', tags, fields)
            elif fuelname == "SP95-E10":
                yield Line('tankstelle.SP95-E10', tags, fields)
            elif fuelname == "Gazole":
                yield Line('tankstelle.Diesel', tags, fields)
            elif fuelname == "E85":
                yield Line('tankstelle.E85', tags, fields)


s.add_job(scheduler.at(minute='10', hour='5-22', name="prix_carburant",
                       action=execute_prix_carburant))


def transform_clever(tankstelle: jobs.clever_tanken.Tankstelle):
    for fuelname, price in tankstelle.preise.items():
        tags = {'name': tankstelle.name, 'id': 'clever_tanken:{}'.format(tankstelle.id)}
        fields = {'value': price}
        if fuelname == "Super E5":
            yield Line('tankstelle.SP95-E5', tags, fields)
        elif fuelname == "Super E10":
            yield Line('tankstelle.SP95-E10', tags, fields)
        elif fuelname == "Diesel":
            yield Line('tankstelle.Diesel', tags, fields)


s.add_job(scheduler.at(minute='*/15', hour='5-24', name='Clever-Tanken', action=
lambda: [line for station in map(transform_clever, map(jobs.clever_tanken.execute, [
    '20219', '11985', '17004',
    '19715',  # Kaiserst. Mineralölvertrieb Schwärzle
    '54296',  # ESSO Endingen
    '10355',  # ARAL Tiengen
    '20144',  # bft Rankackerweg
    '27534',  # EXTROL Freiburg
    '55690',  # Rheinmünster
    '15220',  # Esso Achern
    '5853',  # JET Rastatt
    '24048',  # Bodersweier
    '3819',  # JET Freiburg
])) for line in station]))


def transform_tankerkoenig(job):
    api_key = job.properties['api_key']
    for data in jobs.tankerkoenig.execute(api_key, 48.651822, 7.927891, 15.0):
        yield Line("tankerkoenig.{}".format(data.type), {'name': data.name, 'id': data.id}, {'value': data.price})


s.add_job(scheduler.at(minute='*/10', hour='5-24', name='Tankerkönig',
                       action=transform_tankerkoenig))

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--influx-url', nargs=1, default=None)
    parser.add_argument('--tankerkoenig', nargs=1, default='00000000-0000-0000-0000-000000000002')
    args = parser.parse_args()

    tanker: scheduler.Job = s.get_job_by_name('Tankerkönig')
    tanker.properties['api_key'] = args.tankerkoenig[0]

    if args.influx_url is not None:
        s.add_processor(scheduler.influxdb.Inserter(args.influx_url[0]))
    else:
        s.add_processor(scheduler.influxdb.Dumper())
    s.start(True)