fix linting issues with python changes
This commit is contained in:
parent
0931dfc5b1
commit
7e9a993d7e
@ -1,10 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
|
from datetime import datetime, timedelta
|
||||||
from typing import Dict, Optional, Union
|
from typing import Dict, Optional, Union
|
||||||
|
|
||||||
import forecastio
|
import forecastio
|
||||||
import requests
|
|
||||||
import pytz
|
import pytz
|
||||||
from datetime import datetime, timedelta
|
import requests
|
||||||
from gpxpy.gpx import GPXTrackPoint
|
from gpxpy.gpx import GPXTrackPoint
|
||||||
|
|
||||||
from fittrackee import appLog
|
from fittrackee import appLog
|
||||||
@ -16,7 +16,8 @@ VC_API_KEY = os.getenv('VC_WEATHER_API_KEY')
|
|||||||
def get_weather(point: GPXTrackPoint) -> Optional[Dict]:
|
def get_weather(point: GPXTrackPoint) -> Optional[Dict]:
|
||||||
try:
|
try:
|
||||||
if not point.time:
|
if not point.time:
|
||||||
# if there's no time associated with the point; we cannot get weather
|
# if there's no time associated with the point;
|
||||||
|
# we cannot get weather
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
point_time = (
|
point_time = (
|
||||||
@ -44,8 +45,9 @@ def get_weather(point: GPXTrackPoint) -> Optional[Dict]:
|
|||||||
}
|
}
|
||||||
elif VC_API_KEY:
|
elif VC_API_KEY:
|
||||||
# if visualcrossing API key is present, use that
|
# if visualcrossing API key is present, use that
|
||||||
return get_visual_crossing_weather(VC_API_KEY, point.latitude,
|
return get_visual_crossing_weather(
|
||||||
point.longitude, point.time)
|
VC_API_KEY, point.latitude, point.longitude, point.time
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -54,42 +56,58 @@ def get_weather(point: GPXTrackPoint) -> Optional[Dict]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_visual_crossing_weather(api_key: str,
|
def get_visual_crossing_weather(
|
||||||
latitude: float,
|
api_key: str, latitude: float, longitude: float, time: datetime
|
||||||
longitude: float,
|
) -> Dict[str, Union[str, float]]:
|
||||||
time: datetime) -> Dict[str, Union[str, float]]:
|
|
||||||
# All requests to the Timeline Weather API use the following the form:
|
# All requests to the Timeline Weather API use the following the form:
|
||||||
|
|
||||||
# https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services
|
# https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services
|
||||||
# /timeline/[location]/[date1]/[date2]?key=YOUR_API_KEY
|
# /timeline/[location]/[date1]/[date2]?key=YOUR_API_KEY
|
||||||
|
|
||||||
# location (required) – is the address, partial address or latitude,longitude location for
|
# location (required) – is the address, partial address or
|
||||||
|
# latitude,longitude location for
|
||||||
# which to retrieve weather data. You can also use US ZIP Codes.
|
# which to retrieve weather data. You can also use US ZIP Codes.
|
||||||
|
|
||||||
# date1 (optional) – is the start date for which to retrieve weather data.
|
# date1 (optional) – is the start date for which to retrieve weather data.
|
||||||
# You can also request the information for a specific time for a single date by
|
# You can also request the information for a specific time for a single
|
||||||
# including time into the date1 field using the format yyyy-MM-ddTHH:mm:ss.
|
# date by including time into the date1 field using the format
|
||||||
# For example 2020-10-19T13:00:00.
|
# yyyy-MM-ddTHH:mm:ss. For example 2020-10-19T13:00:00.
|
||||||
|
|
||||||
# The results are returned in the ‘currentConditions’ field and are truncated to the
|
# The results are returned in the ‘currentConditions’ field and are
|
||||||
# hour requested (i.e. 2020-10-19T13:59:00 will return data at 2020-10-19T13:00:00).
|
# truncated to the hour requested (i.e. 2020-10-19T13:59:00 will return
|
||||||
|
# data at 2020-10-19T13:00:00).
|
||||||
|
|
||||||
# first, round datetime to nearest hour by truncating, and then adding an hour if
|
# first, round datetime to nearest hour by truncating, and then adding an
|
||||||
# the "real" time's number of minutes is 30 or more (we do this since the API only truncates)
|
# hour if the "real" time's number of minutes is 30 or more (we do this
|
||||||
trunc_time = (time.replace(second=0, microsecond=0, minute=0, hour=time.hour) +
|
# since the API only truncates)
|
||||||
timedelta(hours=time.minute//30))
|
trunc_time = time.replace(
|
||||||
appLog.debug(f'VC_weather: truncated time {time} ({time.timestamp()}) to '
|
second=0, microsecond=0, minute=0, hour=time.hour
|
||||||
f'{trunc_time} ({trunc_time.timestamp()})')
|
) + timedelta(hours=time.minute // 30)
|
||||||
|
appLog.debug(
|
||||||
|
f'VC_weather: truncated time {time} ({time.timestamp()}) to '
|
||||||
|
f'{trunc_time} ({trunc_time.timestamp()})'
|
||||||
|
)
|
||||||
|
|
||||||
base_url = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services'
|
base_url = (
|
||||||
url = f"{base_url}/timeline/{latitude},{longitude}/{int(trunc_time.timestamp())}?key={api_key}"
|
'https://weather.visualcrossing.com/'
|
||||||
|
+ 'VisualCrossingWebServices/rest/services'
|
||||||
|
)
|
||||||
|
url = (
|
||||||
|
f"{base_url}/timeline/{latitude},{longitude}"
|
||||||
|
f"/{int(trunc_time.timestamp())}?key={api_key}"
|
||||||
|
)
|
||||||
params = {
|
params = {
|
||||||
"unitGroup": "metric",
|
"unitGroup": "metric",
|
||||||
"contentType": "json",
|
"contentType": "json",
|
||||||
"elements": "datetime,datetimeEpoch,temp,humidity,windspeed,winddir,conditions,description,icon",
|
"elements": (
|
||||||
"include": "current"
|
"datetime,datetimeEpoch,temp,humidity,windspeed,"
|
||||||
|
"winddir,conditions,description,icon"
|
||||||
|
),
|
||||||
|
"include": "current",
|
||||||
}
|
}
|
||||||
appLog.debug(f'VC_weather: getting weather from {url}'.replace(api_key, '*****'))
|
appLog.debug(
|
||||||
|
f'VC_weather: getting weather from {url}'.replace(api_key, '*****')
|
||||||
|
)
|
||||||
r = requests.get(url, params=params)
|
r = requests.get(url, params=params)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
res = r.json()
|
res = r.json()
|
||||||
|
Loading…
Reference in New Issue
Block a user