Advertisement
Guest User

Weather

a guest
Dec 11th, 2018
1,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. #import urllib request
  2. import urllib.request
  3. #import urllib error handling
  4. '''
  5. ## create dictionary to hold 4 cities
  6. cityChoices = {"Annapolis":"KNAK","Boca Raton":"KBCT","Boston":"KBOS","Napa":"KAPC"}
  7. key = cityChoices(name)
  8. '''
  9. '''
  10. First, download the file using urllib.request and then save the file.
  11. Name file the city name + .xml
  12. open file using bs4, parse and print out info, as req'd
  13. '''
  14. #Some Variables to initiate
  15. #link = ""
  16. #url = 'https://w1.weather.gov/xml/current_obs/' +link, '.xml'
  17. #url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=' +link
  18.  
  19.  
  20. decoded = ''
  21.  
  22. print('Your Weather Report')
  23. print()
  24. print('Current observations are available for: ')
  25. print("Annapolis  |  Boca Raton  |  Boston  |  Napa  |  KAPC")
  26.  
  27. '''
  28. ## print all 4 city names
  29. for x in cityChoices.keys():
  30.    print("-", x)
  31.  
  32. print (cityChoices)
  33. print (cityChoices.keys())
  34. '''
  35.  
  36. ## let's get the user input and run it against our available cities
  37. user_input = input('Enter the city you would like a weather report for: ')
  38. '''
  39. if user_input in cityChoices.keys():
  40.    match = data(user_input)
  41.    print(match)
  42. else:
  43.    while user_input not in cityChoices.keys():
  44.        print("No data available. \n")
  45.        user_input = input("Please try another city: ")
  46. '''
  47.  
  48.  
  49. link = user_input
  50.  
  51. if user_input == "Napa":
  52.     url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KAPC'
  53.     print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  54. elif user_input == "Annapolis":
  55.     url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KNAK'
  56.     print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  57. elif user_input == "Boca Raton":
  58.     url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KBCT'
  59.     print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  60. elif user_input == "Boston":
  61.     url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KBOS'
  62.     print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  63. else:
  64.     user_input == input("No data available.. Please try another city: ")
  65.        
  66. '''
  67. if user_input == "Napa":
  68.    link = "KAPC"
  69.    print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  70. elif user_input == "Annapolis":
  71.    link == "KNAK"
  72.    print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  73. elif user_input == "Boca Raton":
  74.    link == "KBCT"
  75.    print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  76. elif user_input == "Boston":
  77.    link == "KBOS"
  78.    print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
  79. else:
  80.    user_input == input("No data available.. Please try another city: ")    
  81. '''
  82.  
  83.  
  84. #function that downloads the file
  85. # For text files, or other ascii types, just pass an empty string
  86. # for the second param aka Stuff(file_name,'',url)
  87. def downloadFile(file_name,file_mode,url):
  88.     i=0
  89.     while i < 1:
  90.         with urllib.request.urlopen(url) as f:
  91.             s = f.read().decode('utf-8')
  92.             #print("downloading ", url)
  93.             print(s)
  94.             i += 1
  95.             break
  96.        
  97.             # Open our local file for writing
  98.             local_file = open(file_name, "w" + file_mode)
  99.            
  100.             #Write to our local file
  101.             local_file.write(s)
  102.             local_file.close()
  103.  
  104.             #create file name based on known pattern
  105.             file_name =  user_input + ".xml"
  106.             downloadFile(file_name,'',url)
  107.             print(file_name, +' created successfully')
  108.             reporting_data = file_name
  109.  
  110. l = user_input
  111. e = ''
  112. u = url
  113.  
  114. downloadFile(l,e,u)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement