-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlxml_bw.py
More file actions
72 lines (56 loc) · 1.8 KB
/
lxml_bw.py
File metadata and controls
72 lines (56 loc) · 1.8 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 16:20:46 2019
@author: Hayuki
"""
import requests
import psycopg2
#downloading javascript
page = requests.get("https://www.hvz.baden-wuerttemberg.de/js/hvz-data-peg-db.js")
#fix encoding
pagestring = page.content.decode("utf-8")
print(pagestring)
#split the code into single lines
lines = pagestring.split('\r\n')
print(lines)
data = []
#if the length of a line is longer than 100, it can be considered as a line containing water level information
#it's then split by commas and added to the data array
for x in lines:
if (len(x)>100):
data.append(x[1:-2].split(','))
dataFixed = []
#fixing the datastrings by removing apostrophes
for x in data:
rowFixed = []
for y in x:
rowFixed.append(y.replace("'",""))
dataFixed.append(rowFixed)
#establishing conntection to the sql server
conn = psycopg2.connect("host=localhost port=5432 dbname=pegelstaende user=postgres password=project")
cur = conn.cursor()
#iterating through the strings, extracting the data and storing it in the database
for c in dataFixed:
wlevel = None
try:
wlevel = float(c[4])
except:
continue
flow = None
try:
flow = float(c[9])
except:
print('no flow')
print(c)
#fixing time format to YYYY-MM-DD HH:mm
timeSplit = c[7].split(' ')
timeDate = timeSplit[0].split('.')
time = timeDate[2]+'-'+timeDate[1]+'-'+timeDate[0]+' '+timeSplit[1]
print(time)
cur.execute("SELECT id FROM bw_data WHERE name = %s AND time = %s",(c[1],time))
indata = cur.fetchall()
print(indata)
if (len(indata)!=0):
continue
cur.execute("INSERT INTO bw_data (name,wlevel,flow,time,number,warning) VALUES(%s, %s, %s, %s, %s, %s)",(c[1],wlevel,flow,time,c[0],c[14]))
conn.commit()