-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpricegrabber.py
155 lines (132 loc) · 4.75 KB
/
pricegrabber.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import requests
import time
from time import strftime, localtime
import datetime
from bs4 import BeautifulSoup
import sqlite3
import pandas as pd
import sys
from pathlib import Path
if len(sys.argv) < 3:
print ('''Bitte Postleitzahl und Ort angeben!\n\n
#python3 pricegrabber.py PLZ Ort\n\n''')
sys.exit()
else:
plz = str(sys.argv[1])
l = len(sys.argv)
town = []
ort =''
for p in range(2,len(sys.argv)):
town.append(sys.argv[p])
for t in town:
ort += "+"+t
print(''' Serverdienst läuft und sammelt stündlich alle Kraftstoffpreise \n
im Umkreis von 50km um '''+plz +' ' +ort[1:]+'''.\n
Zum Beenden [Strg]+[C] drücken''')
#read and parse config.ini
#config = configparser.ConfigParser()
#config.read('config.ini')
#span = config['DEFAULT']['SPAN']
#period = config['DEFAULT']['PERIOD']
#plz = config['USER']['PLZ']
#town = config['USER']['TOWN']
radius = '50.0'
fuel_type = [1, 3, 4, 5, 6, 7, 8, 12]
#cheack if database exists or create one
def create_load_database():
if Path('data.sqlite').is_file():
db = sqlite3.connect('data.sqlite')
print ('Database loaded')
else:
print ('Building Database')
db = sqlite3.connect('data.sqlite')
cursor = db.cursor()
cursor.execute("""CREATE TABLE FUELS
(station text,
distance real,
date text,
price real,
fuel text)
""")
cursor.close()
return db
#load the complete content of clever-tanken.de (with config)
def load_page():
data_coll = []
page = 1
while True:
url = "http://www.clever-tanken.de/tankstelle_liste?spritsorte=" + str(type) + "&ort=" + plz + ort + "&r=" + radius + "&sort=km&page=" + str(page)
print (url)
site = requests.get(str(url))
content = BeautifulSoup(site.text, 'html.parser')
data = get_stations(content)
if data == []:
break
data_coll.append(data)
page += 1
return data_coll
#extract price-table from webpage
def get_stations(content):
sta = content.find_all(class_='price-entry')
return sta
#extract the data-pieces from html and write into database
def get_data(data_coll, db, type, timestamp):
#get adresses
for data in data_coll:
for station in data:
#get address-info in html
ad1 = station.find(class_='row fuel-station-location-name')
ad2 = station.find(id = 'fuel-station-location-street')
ad3 = station.find(id = 'fuel-station-location-city')
#strip html-tags
ad1_str = ad1.contents[0]
ad2_str = ad2.contents[0]
ad3_str = ad3.contents[0]
#combine into address-string and create Station
address = ad1_str +'\n'+ ad2_str +'\n'+ ad3_str
#print(stations)
#get prices
price_temp = station.find(class_="price")
#check if the station offers a price for the selected fuel at the selected time
if price_temp == None:
price = None
#print(prices)
else:
price = float(price_temp.contents[0])
#get distance
distance_temp = station.find(class_='fuel-station-location-address-distance')
distance = float(str(distance_temp.contents[1])[5:9])
#get fueltype
if type == 1:
f_type = 'Autogas (LPG)'
elif type == 3:
f_type = 'Diesel'
elif type == 4:
f_type = 'Bio-Ethanol'
elif type == 5:
f_type = 'Super E10'
elif type == 6:
f_type = 'Super Plus'
elif type == 7:
f_type = 'Super E5'
elif type == 8:
f_type = 'Erdgas (CNG)'
else:
f_type = 'Premium Diesel'
entry = (address, distance, timestamp, price, f_type)
print (entry)
# write the data into database
cursor = db.cursor()
cursor.execute("INSERT INTO FUELS VALUES (?,?,?,?,?)", entry)
db.commit()
cursor.close()
database = create_load_database()
while True:
dt = datetime.datetime.now()
if dt.minute == 00 or dt.minute == 30:
time = strftime('%x - %H:%M', localtime())
for type in fuel_type:
#source = 'http://www.clever-tanken.de/tankstelle_liste?spritsorte='+str(type)+'&ort='+plz+town+'&r='+radius+'&sort=km&page='+page
website = load_page()
get_data(website, database, type, time)