# This code is from: # https://fulmanski.pl/tutorials/computer-science/big-data/use-python-to-download-weather-data-and-upload-them-to-hbase/#python_download_data import json import urllib.request def getDataFromServer(year, month, day): if day < 10: day = f"0{day}" if month < 10: month = f"0{month}" url = f"https://fulmanski.pl/data/weather/home/get/{year}/{month}/{day}" dataURL = urllib.request.urlopen(url) data = dataURL.read() dataJSON = data.decode('utf-8') data = json.loads(dataJSON) if data["result"] == "ok": dataToReturn = [] for row in data["data"].split("\n"): if not row == "": rowJSON = json.loads(row) timeServer = rowJSON["timestamp_server"] time = rowJSON["data"]["timestamp"] temperature_DS18B20_outside = None temperature_DS3231 = None humidity_dht = None temperature_dht = None heat_index_dht = None preasure_MPL3115A2 = None temperature_MPL3115A2 = None adc_value_light_10kO = None for sensor in rowJSON["data"]["data"]: if sensor["type"] == "DS18B20" and sensor["id_rom"] == "281c0489060000bc": temperature_DS18B20_outside = sensor["temperature_c"] elif sensor["type"] == "DS3231": temperature_DS3231 = sensor["temperature_c"] elif sensor["type"] == "dht": humidity_dht = sensor["humidity"] temperature_dht = sensor["temperature_c"] heat_index_dht = sensor["heat_index_c"] elif sensor["type"] == "MPL3115A2": preasure_MPL3115A2 = sensor["preasure_hPa"] temperature_MPL3115A2 = sensor["temperature_c"] elif sensor["type"] == "light_10kO": adc_value_light_10kO = sensor["ADC_VALUE"] dataRow = {"timeServer": timeServer, "time": time, "temperature_DS18B20_outside": temperature_DS18B20_outside, "temperature_DS3231": temperature_DS3231, "humidity_dht": humidity_dht, "temperature_dht": temperature_dht, "heat_index_dht": heat_index_dht, "preasure_MPL3115A2": preasure_MPL3115A2, "temperature_MPL3115A2": temperature_MPL3115A2, "adc_value_light_10kO": adc_value_light_10kO} dataToReturn.append(dataRow) return dataToReturn if __name__ == '__main__': data = getDataFromServer(2023, 9, 5) for d in data: print(f'{d["timeServer"]} \ {d["time"]} \ {d["temperature_DS18B20_outside"]} \ {d["temperature_DS3231"]} \ {d["humidity_dht"]} \ {d["temperature_dht"]} \ {d["heat_index_dht"]} \ {d["preasure_MPL3115A2"]} \ {d["temperature_MPL3115A2"]} \ {d["adc_value_light_10kO"]}')