nightr/server/nightr/scrape.py
2019-04-06 01:28:42 +02:00

46 lines
1.2 KiB
Python

import requests
import json
import os
def scrape_traffic():
r = requests.get('https://portal.opendata.dk/api/3/action/datastore_search?resource_id=b3eeb0ff-c8a8-4824-99d6-e0a3747c8b0d')
night_avr = 3.38
day_avr = None
data = r.json()
sum = 0
len = 0
for lel in data['result']['records']:
sum += lel['vehicleCount']
len += 1
curr_avg = len / sum
diff= day_avr - night_avr
if curr_avg >= day_avr:
return 0.0
elif curr_avg <= night_avr:
return 1.0
res = 1 - curr_avg / diff
assert(res < 1 and res > 0)
return res
def scrape_dmi_aarhus():
r = requests.get('https://www.dmi.dk/NinJo2DmiDk/ninjo2dmidk?cmd=obj&wmo=06074')
data = r.json()
latest_time, aarhus_latest_temp = max(data['Temperature2m'].items(), key= lambda x : x[0])
adak_r = requests.get('https://www.dmi.dk/NinJo2DmiDk/ninjo2dmidk?cmd=llj&id=5878818')
adak_data = adak_r.json()
adak_temp_latest = adak_data['timeserie'][0]['temp']
if aarhus_latest_temp-5 < adak_temp_latest:
return 1.0
return 0.0
#adak_latest_time, adak_latest_temp_aarhus = max(adak_timeserie.items(), key= lambda x : x[0])
scrape_dmi_aarhus()