Created
May 2, 2023 14:12
-
-
Save LtGlahn/8d444304a8a07ed0103e513af5bc9913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import sys | |
from datetime import datetime, timedelta | |
# import pandas as pd | |
# import geopandas as gpd | |
# from shapely.geometry import Point | |
# from shapely import wkt | |
# Henter NVDB data med biblioteket mitt | |
# Ref https://github.com/LtGlahn/nvdbapi-V3/ | |
# Evt pip install nvdbapi-v3 | |
if not [ k for k in sys.path if 'nvdbapi' in k]: | |
print( "Adding NVDB api library to python search path") | |
sys.path.append( '/mnt/c/data/leveranser/nvdbapi-V3' ) | |
import nvdbapiv3 | |
with open( 'SECRET.json' ) as f: | |
secret = json.load( f ) | |
myKey = secret['myAutopassAPARKey'] | |
url = 'https://apar.autopassops.no/api' | |
if __name__ == '__main__': | |
headers = { 'Accept' : 'application/json', | |
'Authorization': myKey } | |
# r = requests.get( url + '/operators/100120/tollstations', headers=headers) | |
# print("Operatør:", r.status_code, r.text ) | |
# params = { "DFrom" : "2023-01-01T09:00:00Z" } | |
# r2 = requests.get( url + '/tollstations', params=params, headers=headers) | |
# print( "Endret etter:", r2.status_code, r2.text ) | |
# Finner alle NVDB bomstasjoner | |
nvdbBomst = pd.DataFrame( nvdbapiv3.nvdbFagdata(45).to_records() ) | |
# alle operatør ID | |
operatorId = list( nvdbBomst[ ~nvdbBomst['Operatør_Id'].isnull() ]['Operatør_Id'].unique() ) | |
operatorId = [ int(x ) for x in operatorId ] | |
data = [] | |
for operator in operatorId: | |
print( f"henter operatør {operator}") | |
r = requests.get( url + '/operators/' + str(operator) + '/tollstations', headers=headers) | |
if r.ok: | |
data.extend( r.json()) | |
else: | |
print( f"Fant ingen data for operatørID {operator}: {r.status_code} {r.text} ") | |
with open( 'apardump.json', 'w') as f: | |
json.dump( data, f, ensure_ascii=False, indent=4 ) | |
# Henter endringer | |
r = requests.get( url + '/tollstations', headers=headers, params={'DFrom' : '2023-01-01T09:00:00Z' } ) | |
if r.ok: | |
endret_alle = r.json() | |
with open( 'endret_bomstasjoner_alle.json', 'w' ) as f: | |
json.dump( endret_alle, f, indent=4, ensure_ascii=False ) | |
# Henter endringer etter 1. mars | |
r = requests.get( url + '/tollstations', headers=headers, params={'DFrom' : '2023-03-01T00:00:00Z' } ) | |
if r.ok: | |
endret_mars = r.json() | |
with open( 'endret_etter20230301.json', 'w' ) as f: | |
json.dump( endret_mars, f, indent=4, ensure_ascii=False ) | |
# Henter ferske endringer | |
now = datetime.now() | |
to_uker_siden = now - timedelta( weeks=2 ) | |
to_uker_siden = to_uker_siden.replace( hour=0, minute=0, second=0, microsecond=0 ) | |
r = requests.get( url + '/tollstations', headers=headers, params={'DFrom' : to_uker_siden.isoformat() + 'Z' } ) | |
if r.ok: | |
endret_sisteuker = r.json() | |
with open( 'endret_bomstasjoner_sisteuker.json', 'w' ) as f: | |
json.dump( endret_sisteuker, f, indent=4, ensure_ascii=False ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment