-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataService.py
78 lines (66 loc) · 2.24 KB
/
DataService.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
import requests
from util import read, save
from flask import Flask
from flask_cors import CORS
import json
import os
import DataRepository
from datetime import datetime
def getSavedData():
return DataRepository.getSavedData()
def updateData():
newData = buildDataset()
DataRepository.saveData(newData)
def buildDataset():
data = []
i = 1976
oneYearAgo = datetime.now().year - 1
while i <= oneYearAgo:
[first, second] = [str(i), str(i+1)]
response = requests.get("https://www.snowyhydro.com.au/wp-content/themes/snowyhydro/inc/getData.php?yearA=" + first + "&yearB=" + second).json()
for year in response:
level = response[year]['snowyhydro']['level']
onlySnow = list(filter(lambda x: 'snow' in x, level))
yearData = list(map(lambda x: toYearData(x), onlySnow))
noNullsYearData = list(filter(lambda x: x is not None, yearData))
if len(noNullsYearData):
data.append(Data(year, noNullsYearData))
i += 2
return data
def toYearData(level):
snow = level['snow']
if not isinstance(snow, list):
snow = [snow]
onlySpencersCreek = list(filter(lambda x: x['-name'] == 'Spencers Creek', snow))
if len(onlySpencersCreek) == 0:
return None
date = level['-date']
name = onlySpencersCreek[0]['-name']
snow = float(onlySpencersCreek[0]['#text'])
return YearData(date, name, snow)
def isDataOutdated():
latestSavedDate = getLatestSavedDate()
if latestSavedDate:
newData = buildDataset()
newLastDate = getLatestDate(newData)
return latestSavedDate != newLastDate
else:
return False
def getLatestSavedDate():
savedData = getSavedData()
return getLatestDate(savedData)
def getLatestDate(data):
lastYearData = data[len(data) - 1].data
latestDate = lastYearData[len(lastYearData) - 1].date
return latestDate
class Data:
def __init__(self, year, data):
self.year = year
self.data = data
class YearData:
def __init__(self, date, name, snow):
self.date = date
self.name = name
self.snow = snow
def __repr__(self):
return "-date " + self.date + "-name" + self.name + "-snow" + str(self.snow) + "\n"