Add files via upload

This commit is contained in:
Luke Prior 2022-01-26 16:02:26 +11:00 committed by GitHub
parent 693e5f064e
commit 112e21f701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,8 +9,12 @@ params = "?token={}&period=2".format(apiKey)
url = "https://radiosondy.info/api/v1/sonde-logs{}".format(params)
recoveryUrl = "https://api.v2.sondehub.org/recovered"
searchUrl = "https://api.v2.sondehub.org/sondes"
response = urllib.request.urlopen(url)
data = json.load(response)
# Main function
def handler(event,context):
response = urllib.request.urlopen(url)
data = json.load(response)
processReports()
# Check exisiting SondeHub recovery reports
def checkExisting(serial, recovered):
@ -58,57 +62,58 @@ def findSonde(recovery, lat, lon):
return serial
# Process each recovery report from Radiosondy.info
for recovery in data["results"]:
def processReports():
for recovery in data["results"]:
# Get recovery status
if recovery["status"] == "FOUND":
recovered = True
elif recovery["status"] == "NEED ATTENTION":
recovered = False
else:
continue
# Get finder if available
if recovery["log_info"]["finder"] is not None:
recovered_by = recovery["log_info"]["finder"]
else:
continue
# Import time
recovered_time = datetime.strptime(recovery["log_info"]["log_added"], "%Y-%m-%d %H:%M:%S")
# Get comment and add attribution
description = recovery["log_info"]["comment"]
description += " [via Radiosondy.info]"
description = description.lstrip()
if recovery["log_info"]["found_coordinates"]["latitude"] != "0" and recovery["log_info"]["found_coordinates"]["longitude"] != "0":
lat = float(recovery["log_info"]["found_coordinates"]["latitude"])
lon = float(recovery["log_info"]["found_coordinates"]["longitude"])
else:
continue
# Use the reported serial number for RS41/RS92
if "RS41" in recovery["type"] or "RS92" in recovery["type"]:
serial = recovery["sonde_number"]
# Try to find serial in SondeHub database for others
else:
serial = findSonde(recovery, lat, lon)
if serial is None:
print("{}: could not match to SondeHub serial".format(recovery["sonde_number"]))
# Get recovery status
if recovery["status"] == "FOUND":
recovered = True
elif recovery["status"] == "NEED ATTENTION":
recovered = False
else:
continue
# Check if a valid recovery already exists
if checkExisting(serial, recovered) == False:
print("{}: recovery already exists".format(serial))
continue
# Get finder if available
if recovery["log_info"]["finder"] is not None:
recovered_by = recovery["log_info"]["finder"]
else:
continue
# Format data for upload
recoveryPutData = {"datetime": recovered_time.isoformat(), "serial": serial, "lat": lat, "lon": lon, "recovered": recovered, "recovered_by": recovered_by, "description": description}
recoveryPutData = str(json.dumps(recoveryPutData)).encode('utf-8')
print("{}: {}".format(serial, recoveryPutData))
# Upload data
recoveryPutRequest = urllib.request.Request(recoveryUrl, data=recoveryPutData, method="PUT")
print("{}: {}".format(serial, urllib.request.urlopen(recoveryPutRequest).read().decode('utf-8')))
# Import time
recovered_time = datetime.strptime(recovery["log_info"]["log_added"], "%Y-%m-%d %H:%M:%S")
# Get comment and add attribution
description = recovery["log_info"]["comment"]
description += " [via Radiosondy.info]"
description = description.lstrip()
if recovery["log_info"]["found_coordinates"]["latitude"] != "0" and recovery["log_info"]["found_coordinates"]["longitude"] != "0":
lat = float(recovery["log_info"]["found_coordinates"]["latitude"])
lon = float(recovery["log_info"]["found_coordinates"]["longitude"])
else:
continue
# Use the reported serial number for RS41/RS92
if "RS41" in recovery["type"] or "RS92" in recovery["type"]:
serial = recovery["sonde_number"]
# Try to find serial in SondeHub database for others
else:
serial = findSonde(recovery, lat, lon)
if serial is None:
print("{}: could not match to SondeHub serial".format(recovery["sonde_number"]))
continue
# Check if a valid recovery already exists
if checkExisting(serial, recovered) == False:
print("{}: recovery already exists".format(serial))
continue
# Format data for upload
recoveryPutData = {"datetime": recovered_time.isoformat(), "serial": serial, "lat": lat, "lon": lon, "recovered": recovered, "recovered_by": recovered_by, "description": description}
recoveryPutData = str(json.dumps(recoveryPutData)).encode('utf-8')
print("{}: {}".format(serial, recoveryPutData))
# Upload data
recoveryPutRequest = urllib.request.Request(recoveryUrl, data=recoveryPutData, method="PUT")
print("{}: {}".format(serial, urllib.request.urlopen(recoveryPutRequest).read().decode('utf-8')))