File size: 3,334 Bytes
84a1a7d
 
 
 
 
 
 
 
 
 
ad4878b
 
84a1a7d
83146ec
cc82983
84a1a7d
 
5354fdd
84a1a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad4878b
 
 
 
84a1a7d
ad4878b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84a1a7d
 
 
 
ad4878b
 
84a1a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad4878b
84a1a7d
 
 
 
 
 
 
 
ad4878b
 
84a1a7d
 
 
 
ad4878b
 
84a1a7d
 
 
 
 
 
 
 
 
 
ad4878b
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
from datetime import datetime
import requests
import os
import joblib
import pandas as pd
import json




def get_weather_csv():
    return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki?unitGroup=metric&include=days&key=FYYH5HKD9558HBXD2D6KWXDGH&contentType=csv').csv()

def get_weather_json_quick(date):
    return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/{date}/{date}?unitGroup=metric&include=days&key=J7TT2WGMUNNHD8JBEDXAJJXB2&contentType=json').json()

def get_air_quality_data():
    
    AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
    json = get_air_json(AIR_QUALITY_API_KEY)
    iaqi = json['iaqi']
    forecast = json['forecast']['daily']
    return [
        json['aqi'],                 # AQI
        json['time']['s'][:10],      # Date
        iaqi['h']['v'],
        iaqi['p']['v'],
        iaqi['pm10']['v'],
        iaqi['t']['v'],
        forecast['o3'][0]['avg'],
        forecast['o3'][0]['max'],
        forecast['o3'][0]['min'],
        forecast['pm10'][0]['avg'],
        forecast['pm10'][0]['max'],
        forecast['pm10'][0]['min'],
        forecast['pm25'][0]['avg'],
        forecast['pm25'][0]['max'],
        forecast['pm25'][0]['min'],
        forecast['uvi'][0]['avg'],
        forecast['uvi'][0]['avg'],
        forecast['uvi'][0]['avg']
    ]


def get_weather_data(json):
    #WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
    
    #csv = get_weather_csv()
    data = json['days'][0]
    print("data parsed sccessfully")
    #return [
    #    #json['address'].capitalize(),
    #    data['datetime'],
    #    data['feelslikemax'],
    #    data['feelslikemin'],
    #    data['feelslike'],
    #    data['dew'],
    #    data['humidity'],
    #    data['precip'],
    #    data['precipprob'],
    #    data['precipcover'],
    #    data['snow'],
    #    data['snowdepth'],
    #    data['windgust'],
    #    data['windspeed'],
    #    data['winddir'],
    #    data['pressure'],
    #    data['cloudcover'],
    #    data['visibility'],
    #    data['solarradiation'],
    #    data['solarenergy'],
    #    data['uvindex'],
    #    data['conditions']
    #]
    return data


def get_weather_df(data):
    col_names = [
        'name',
        'datetime',
        'tempmax',
        'tempmin',
        'temp',
        'feelslikemax',
        'feelslikemin',
        'feelslike',
        'dew',
        'humidity',
        'precip',
        'precipprob',
        'precipcover',
        'snow',
        'snowdepth',
        'windgust',
        'windspeed',
        'winddir',
        'sealevelpressure',
        'cloudcover',
        'visibility',
        'solarradiation',
        'solarenergy',
        'uvindex',
        'conditions'
    ]

    

    new_data = pd.DataFrame(
        data,
        columns=col_names
    )
    new_data.datetime = new_data.datetime.apply(timestamp_2_time1)
    #new_data.rename(columes={'pressure':'sealevelpressure'})
    return new_data

def timestamp_2_time1(x):
    dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
    dt_obj = dt_obj.timestamp() * 1000
    return int(dt_obj)

def timestamp_2_time(x):
    dt_obj = datetime.strptime(str(x), '%m/%d/%Y')
    dt_obj = dt_obj.timestamp() * 1000
    return int(dt_obj)