''' Purpose: provide weather forecast by accessing US Weather Service web service
Usage: user provides a zipcode
Output: current forecast for that zipcode
'''
# get url access capability from cs 1112 module
import url
# define weather.gov base query
WEATHER_GOV_QUERY = "https://forecast.weather.gov/zipcity.php?inputstring="
# We're going to complete this link by putting the specific zip code
# the user enters at the end.
# forecast delimiters
FRONT_DELIMITER = "
" # text that precedes forecast
REAR_DELIMITER = "
" # text that follows forecast
# When we hit "inspect page source", we saw the HTML for the webpage.
# What we want to do is find the front and rear delimiter in the
# HTML because in the middle of that, we will find the forecast!
# delimiter lengths
LENGTH_FRONT_DELIMITER = len( FRONT_DELIMITER )
LENGTH_REAR_DELIMITER = len( REAR_DELIMITER )
# get zipcode of interest
reply = input( "Enter zipcode: " ) # Remember that input is returned as a string
zipcode = reply.strip() # clean-up response
# specify complete query
query_link = WEATHER_GOV_QUERY + zipcode # THE FULL LINK FOR THE WEBPAGE
# get response from weather.gov
page = url.get_contents( query_link ) # The string of what's on the webpage
# to get the forecast, we need to find it within the page
# REMEMBER THE FORECAST IS IN BETWEEN THE FRONT AND REAR DELIMITER IN THE
# PAGE SOURCE (what get_contents returns as a string for the webpage)
# start by finding the forecast delimiters
# How do I go into the string (page) and find where the FRONT_DELIMITER
# and REAR_DELIMITER IS?
front_index = page.find( FRONT_DELIMITER )
# print( front_index )
rear_index = page.find( REAR_DELIMITER , front_index)
# Now we're saying hey! go into the string page (the HTML page source)
# and find where the REAR_DELIMITER occurs after the front_index (where the
# FRONT_DELIMITER occurs)
# print( rear_index )
# Remember that .find( whatwewannafind) returns the index where the thing
# we wanna find FIRST occurs.
# We want to find a REAR DELIMITER that occurs AFTER the FRONT DELIMITER
#print( front_index, read_index )
# (mis)try to use those index delimiters to pick off the forecast
# Remember the page is a giant string of the HTML (the page source)
# We want to slice into the string and get the forecast that's between
# the FRONT DELIMITER AND REAR DELIMITER
# forecast = page[ front_index : rear_index ]
# Slicing goes from the front_index to the rear_index - 1
# print( forecast )
print()
# get the indices for the front and rear of the forecast
forecast_start = front_index + len( FRONT_DELIMITER )
forecast_rear = rear_index
# ready to get and print the forecast
forecast = page[ forecast_start: forecast_rear ]
# print the forecast
print( forecast )