Now You Code 3: Syracuse Historical Weather Write a program to prompt a user to
ID: 3819156 • Letter: N
Question
Now You Code 3: Syracuse Historical Weather
Write a program to prompt a user to input a date and time string, and then it should report the temperature and weather conditions at that day and time for Syracuse, NY.
To lookup the weather you will need to use the Dark Sky Time Machine: https://darksky.net/dev/docs/time-machine
The date and time that the user inputs must be in the following format: YYYY-MM-DDThh:mm:ss for example January 7, 2016 at 4:30pm would be: 2016-01-07T16:30:00
Be sure to use the GPS coordinates for Syracuse, NY which are (lat=43.048122,lng=-76.147424)
Example Run (Based on the exchange rates from 2017-03-06:
Can somesone please help with this python program using API's. Specifically how to create/call a module
Explanation / Answer
Find the below solution to the above problem statement
API URL: https://api.darksky.net/forecast/a068894c26763caefa2f38ddc9686410/43.048122,-76.147424
********************
CODE
import os
import mapq
import requests as req
def key(api_key=None):
"""Save the API key in an environment variable."""
if api_key:
os.environ['DARK_SKY_API'] = api_key
else:
api_key = os.getenv('DARK_SKY_API', 'a068894c26763caefa2f38ddc9686410')
return api_key
def mapquest(api_key=None):
"""Save or request your Mapquest API key."""
if api_key:
os.environ['MAPQUEST_API_KEY'] = api_key
else:
api_key = os.getenv('MAPQUEST_API_KEY', 'a068894c26763caefa2f38ddc9686410')
return api_key
def _check_lat_lng(lat, lng):
"""Check whether the latitude and longitude of a coordinate were given."""
if not lng:
# Then `lat` is actually a string.
lat=43.048122,lng=-76.147424
lat, lng = location(lat,lng)
return (str(lat), str(lng))
def brief(lat, lng=None, api_key=None,variableindate2=None):
"""Get a brief forecast for a given location."""
if not api_key:
api_key = key()
lat, lng = _check_lat_lng(lat, lng)
url = "https://api.darksky.net/v1/brief_forecast/%s/%s,%s" % (api_key, lat, lng)
return req.get(url).json()
def date(lat, lng, date=None, api_key=None):
"""Get a forecast for a specific date."""
if not api_key:
api_key = key()
if not date:
# Then `lng` is actually the date.
lng, date = date, lng
if not isinstance(date, str):
date = date.isoformat()
lat, lng = _check_lat_lng(lat, lng)
url = "https://api.darksky.net/brief_forecastst/%s/%s,%s,%s" % (api_key, lat, lng, date)
return req.get(url).json()
//https://api.darksky.net/forecast/a068894c26763caefa2f38ddc9686410/43.048122,-76.147424
def forecast(lat, lng=None, api_key=None):
"""Get a specific forecast for a given location."""
if not api_key:
api_key = key()
lat, lng = _check_lat_lng(lat, lng)
url = "https://api.darksky.net/v1/forecast/%s/%s,%s" % (api_key, lat, lng)
return req.get(url).json()
def interesting(api_key=None):
if not api_key:
api_key = key()
url = "https://api.darksky.net/v1/interesting/%s" % api_key
return req.get(url).json()
def location(place):
"""Find the latitude and longitude for a location."""
map_key = mapquest()
if map_key == '':
raise Exception('No MAPQUEST_API_KEY found.')
results = mapq.latlng(place)
return (results['lat'], results['lng'])
**********************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.