-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet_Netatmo_Records_From_LocationX.py
170 lines (138 loc) · 4.46 KB
/
Get_Netatmo_Records_From_LocationX.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
## Juan Manuel López Torralba 2019
## This script downloads historical data from
## X City Weather Map by using the Netatmo API
# -*- coding: utf-8 -*-
import os
import sys
import datetime
import time
import requests
from requests.auth import HTTPBasicAuth
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from pprint import pprint
import json
import logging
import pandas as pd
from pandas.io.json import json_normalize
import numpy as np
import re
# Global variables definition
date_begin = 11111111 # Your start date in epoch.
step_time = 86400 # Set the step time for retrieving data.
headers = {'Content-Type': 'application/json; charset=utf-8',
'X-Requested-With':'XMLHttpRequest'}
payload = {'grant_type': 'password',
'username': "Your Username",
'password': "Your Password",
'client_id':"Your Client Id",
'client_secret': "Your Client Secret",
'scope': 'read_station'}
def GetToken(*args):
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
access_token=response.json()["access_token"]
refresh_token=response.json()["refresh_token"]
scope=response.json()["scope"]
return access_token
except requests.exceptions.HTTPError as error:
logging.warning('The authentication stage has risen an HTTP error %s %s',error.response.status_code, error.response.text)
raise
def GetNetatmoData(methodRequest,parametros):
try:
url = "https://api.netatmo.com/api/"+methodRequest
pprint(url)
pprint(parametros)
response = requests.post(url, params = parametros)
response.raise_for_status()
data = response.json()["body"]
dataToParsing = response.json()
return dataToParsing
except requests.exceptions.HTTPError as error:
logging.warning("The data downloading stage has risen an HTTP error %s %s", error.response.status_code, error.response.text)
raise
except requests.exceptions.ConnectionError as connErr:
logging.warning("The data downloading stage has risen a Connection Error %s", connErr)
raise
except Exception as e:
pprint(e)
raise
def parsingIdNetatmoData(data):
df = pd.DataFrame({'id': devices["_id"], 'modulos': devices["modules"]} for devices in data["body"])
return df
def parsingTemperatureData(data, _id, modulo):
df = pd.DataFrame({'id': _id,'modulo': modulo,'beg_time': devices["beg_time"], 'temperature': devices["value"]} for devices in data["body"])
return df
def dataToFile(data):
try:
filename = "YourXLocationNetatmoRecord" + ".txt"
fileStation = open(filename,"a")
fileStation.write(str(data))
fileStation.write("\n")
fileStation.close()
except Exception as e:
logging.debug("Rising writing exceptions: %s%", e)
raise e
def main():
currentMinute = datetime.datetime.now().minute
df2 = pd.DataFrame(columns=['id','modulo','beg_time','temperature'])
df_temperature = pd.DataFrame(columns=['id','modulo','beg_time','temperature'])
try:
access_token = GetToken(payload)
except requests.exceptions.HTTPError as error:
logging.warning('Cannot get token')
else:
token = {
'access_token': access_token
}
regiones = ({
'access_token': access_token,
'lat_ne': 11.1111,
'lon_ne': -8.88,
'lat_sw': 11.12,
'lon_sw': -8.88,
'filter': "true"
}) # Change the coordinates for real ones.
try:
data = GetNetatmoData('getpublicdata',regiones)
except requests.exceptions.HTTPError as error:
logging.warning('Cannot get the data from the stations')
else:
df = parsingIdNetatmoData(data)
### getcommondata
for row in df.itertuples(index=False):
_id = row.id
for modulo in row.modulos:
pprint(modulo)
try:
parametros = ({
'access_token': access_token,
'device_id': _id,
'module_id': modulo,
#'scale': '1day',
'scale': '1hour',
'type': 'Temperature',
'filter': "true",
'date_begin':date_begin,
'optimize': 'true',
'real_time': 'true'
})
data = GetNetatmoData('getmeasure',parametros)
except Exception as e:
pass
else:
df2.append(df2, ignore_index = True)
if data is not None:
df2 = parsingTemperatureData(data, _id,modulo)
df_temperature = pd.concat([df_temperature,df2], ignore_index = True)
try:
dataToFile(df_temperature)
except Exception as e:
print(e)
pass
else:
df_temperature.to_csv('YourDataFrameInCsv.csv')
df_temperature.to_excel("YourDataFrameInExcel.xlsx")
if __name__ == '__main__':
main()