''' 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="
# forecast delimiters
FRONT_DELIMITER = "
" # text that precedes forecast
REAR_DELIMITER = "
" # text that follows forecast
# Basically in the html, there's a string that might look like this:
# " Sunny with a chance of rain.
# where 'Sunny with a chance of rain' is the forecast we want.
# The front delimiter on line 16 (the first div section) is before the forecast
# and the rear delimiter on line 17 is after the forecast.
# We want to pluck the text in between since that's the forecast.
# So we find where the front and rear delimiter (where the
# rear delimiter occurs after the front delimiter)
# occur on the page (in the html string) and get what's in between to get the forecast!
# The "delimiters" mean that they surround the forecast in the webpage HTML.
# The weather website pages are created using HTML and basically in that HTML,
# somewhere in the HTML there is
# FRONT DELIMITER forecast REAR DELIMITER
# this is where the forecast goes
# delimiter lengths
LENGTH_FRONT_DELIMITER = len( FRONT_DELIMITER )
LENGTH_REAR_DELIMITER = len( REAR_DELIMITER )
# get zipcode of interest
reply = input( "Enter zipcode: " )
zipcode = reply.strip() # clean-up response
# specify complete query
query_link = WEATHER_GOV_QUERY + zipcode # this is the full link to access the forecast link for the zip code provided
#
# print( query_link )
# get response from weather.gov
page = url.get_contents( query_link ) # url.get_contents() returns the contents of the webpage
# (a string of the html)
# print( page ) # This is the HTML for the webpage - we want to look into this to find the
# FRONT_DELIMITER and REAR_DELIMITER
# to get the forecast, we need to find it within the page
# start by finding the forecast delimiters
# We want to find where the FRONT_DELIMITER and REAR_DELIMITER OCCUR in the page (the html)
# The page variable stores a giant string of the HTML.
# We can use the string function find() to look in the page.
# REMEMBER: FRONT_DELIMITER defined above is ''
# REAR_DELIMITER defined above is '/div'
# This should find where '
' is in the page (string of html)
front_index = page.find( FRONT_DELIMITER )
# This should find where '/div' is in the page (string of html)
rear_index = page.find( REAR_DELIMITER, front_index ) # The rear delimiter should occur AFTER the front delimiter
# find() takes in an optional second parameter
# find( what we wanna find, index position we wanna start searching from )
# In Html
is closed with
and so we want to find the
that closes the
tag in
# our front delimiter.
# print( front_index, rear_index )
# Remember the find() function returns -1 if the string is not found in the string we're looking in
# (mis)try to use those index delimiters to pick off the forecast
# Slicing -> string_variable[i:j]
forecast_start = front_index + LENGTH_FRONT_DELIMITER
forecast = page[ forecast_start : rear_index ]
# Get the string slice of the page (HTML) from the end of the front index to the rear index
# Just get the forecast in the HTML (we got what was in the middle of the FRONT DELIMITER and REAR DELIMITER)
# print the forecast
print( forecast )
# The point of this example was using url.get_contents( link ) to get the
# string of the contents of the webpage and to basically go into that big string
# to find where the front and rear delimiter (substrings) occur and get the string
# in the middle.
# Don't worry about the
stuff or html until you're in your 3rd year of CS or something :)
# For now, focus on string functions like find()
# and how to slice strings and string indices.
# s.find( what we wanna find ) will find the number index of where the thing we wanna find occurs in the string
# slicing is s[index1: index2] will get characters in the string from index 1 to (index 2 - 1)
# ex. s = 'CS1112'
# 012345
# s[1:3] -> s characters 1 and 2
# -> 'S1'
# s.find('1') -> 2 (the first index where the '1' occurs)