Advertisement
TokyoDisco

Python WeatherForecast/Currency Converter

Apr 20th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. import pywapi
  2. import cgi, cgitb
  3.  
  4. form = cgi.FieldStorage()
  5.  
  6.  
  7.  
  8.  
  9. print("Welcome to Python Weather Forecast script and Currency Converter script \n")
  10. print("To Lauch Weather Forecast press 1. For currency converter press 2. 3 to Exit \n")
  11. countSearch = 0
  12. CityList =[]
  13. z = input ("Chooese : ")
  14. while z!='3' :
  15. if z == '1':
  16. city = input ("Enter City name: " )
  17.  
  18. if countSearch > 3:
  19. CityList[0] = city
  20. else:
  21. CityList.append(city)
  22. countSearch = countSearch + 1
  23.  
  24. lookup = pywapi.get_location_ids(city)
  25.  
  26.  
  27. for i in lookup:
  28. location_id = i
  29.  
  30.  
  31. weather_com_result = pywapi.get_weather_from_weather_com(location_id)
  32. temp = weather_com_result['current_conditions']['temperature']
  33. update = weather_com_result['current_conditions']['last_updated']
  34. station = weather_com_result['current_conditions']['station']
  35. visi = weather_com_result['current_conditions']['visibility']
  36. wind = weather_com_result['current_conditions']['wind']['speed']
  37. windD = weather_com_result['current_conditions']['wind']['direction']
  38. windText = weather_com_result['current_conditions']['wind']['text']
  39. baroText = weather_com_result['current_conditions']['barometer']['direction']
  40. baroRead = weather_com_result['current_conditions']['barometer']['reading']
  41. humi = weather_com_result['current_conditions']['humidity']
  42. cloud = weather_com_result['current_conditions']['text']
  43. fore1 = weather_com_result['forecasts'][0]
  44. fore2 = weather_com_result['forecasts'][1]
  45. fore3 = weather_com_result['forecasts'][2]
  46. fore4 = weather_com_result['forecasts'][3]
  47. feel = weather_com_result['current_conditions']['feels_like']
  48.  
  49. print ('Welcome into Python Weather Forecast script. This script is using pywAPI and weather.com for weather information \n')
  50. print ('The city you enter is : ' + city + '\n')
  51. print ('Forecast for day : ' + fore1["date"] + " " + fore1["day_of_week"])
  52. print ('Current Temperature in ' + city + ' is ' + temp + ' C. Last update on ' + update + ' wind ' + wind + ' km/h' + ' date from station ' + station)
  53. print ('Sunrise : ' + fore1["sunrise"] + ' Sunset : ' + fore1["sunset"])
  54. print ('For More option press following keys \n')
  55. print ('1 - Temperature' )
  56. print ('2 - Wind')
  57. print ('3 - Barometer')
  58. print ('4 - Visibility')
  59. print ('5 - humidity')
  60. print ('6 - Clouds and Rain chances')
  61. print ('7 - Forecast for 3 next days')
  62. print ('8 - Last 3 searches')
  63. print ('9 - exit')
  64. f = input ("Menu ")
  65. while f != '9' :
  66.  
  67.  
  68. if f == '1' :
  69. print('Current Temperature in ' + city + ' is ' + temp + ' C. Feels like ' + feel + ' C. ' + " highest temperature : " + fore1["high"] + " lowest : " + fore1["low"] )
  70. elif f == '2':
  71. print("Current Wind speed is " + wind + ' KM/h. From ' + windD + " " + windText )
  72. elif f == '3':
  73. print("Current Barometer date are " + baroRead + " " + baroText)
  74. elif f == '4':
  75. visiInt = float(visi)
  76. if visiInt > 10 :
  77. print("Current Visibility is perfect " + visi + " meteres" )
  78. elif visiInt < 10 and visi > 5:
  79. print("Current Visibility is okay " + visi + " meteres" )
  80. elif visiInt < 5 :
  81. print("Current Visibility is poor " + visi + " meteres" )
  82. elif f == '5':
  83. print("Current Humidity is " + humi + " % " )
  84. elif f == '6':
  85. print("Current sky condition is : " + cloud + " Chance for rain at day " + fore1["day"]["chance_precip"] + "% at night " + fore1["night"]["chance_precip"] + "%" )
  86. elif f == '7':
  87. print("Forecast for " + fore2["date"] + " " + fore2["day_of_week"] + " highest temperature will be " + fore2["high"] + " C. Lowest will be " + fore2["low"] + " C " )
  88. print("Sunrise at " + fore2["sunrise"] + " Sunset at " + fore2["sunset"])
  89. print("At Day :" + fore2["day"]["text"] + " Chance for rain: " + fore2["day"]["chance_precip"] + "%. Humidity : " + fore2["day"]["humidity"] + "%. Wind speed: " + fore2["day"]["wind"]["speed"] + "km/h")
  90. print("At Day :" + fore2["night"]["text"] + " Chance for rain: " + fore2["night"]["chance_precip"] + "%. Humidity : " + fore2["night"]["humidity"] + "%. Wind speed: " + fore2["night"]["wind"]["speed"] + "km/h" + "\n")
  91.  
  92. print("Forecast for " + fore3["date"] + " " + fore3["day_of_week"] + " highest temperature will be " + fore3["high"] + " C. Lowest will be " + fore3["low"] + " C " )
  93. print("Sunrise at " + fore3["sunrise"] + " Sunset at " + fore3["sunset"])
  94. print("At Day :" + fore3["day"]["text"] + " Chance for rain: " + fore3["day"]["chance_precip"] + "%. Humidity : " + fore3["day"]["humidity"] + "%. Wind speed: " + fore3["day"]["wind"]["speed"] + "km/h")
  95. print("At Day :" + fore3["night"]["text"] + " Chance for rain: " + fore3["night"]["chance_precip"] + "%. Humidity : " + fore3["night"]["humidity"] + "%. Wind speed: " + fore3["night"]["wind"]["speed"] + "km/h" + "\n")
  96.  
  97. print("Forecast for " + fore4["date"] + " " + fore4["day_of_week"] + " highest temperature will be " + fore4["high"] + " C. Lowest will be " + fore4["low"] + " C " )
  98. print("Sunrise at " + fore4["sunrise"] + " Sunset at " + fore4["sunset"])
  99. print("At Day :" + fore4["day"]["text"] + " Chance for rain: " + fore4["day"]["chance_precip"] + "%. Humidity : " + fore4["day"]["humidity"] + "%. Wind speed: " + fore4["day"]["wind"]["speed"] + "km/h")
  100. print("At Day :" + fore4["night"]["text"] + " Chance for rain: " + fore4["night"]["chance_precip"] + "%. Humidity : " + fore4["night"]["humidity"] + "%. Wind speed: " + fore4["night"]["wind"]["speed"] + "km/h" + "\n")
  101.  
  102.  
  103. elif f == '8':
  104.  
  105. cities = str(CityList)
  106. print ("You searched for this Cities : " + cities)
  107. elif f == '9':
  108. print("Goodbay, thank you for using this app")
  109.  
  110. f = 9
  111. else :
  112. print ('Wrong key, try again')
  113.  
  114. print("City you entered : " + city)
  115. print ('1 - Temperature' )
  116. print ('2 - Wind')
  117. print ('3 - Barometer')
  118. print ('4 - Visibility')
  119. print ('5 - humidity')
  120. print ('6 - Clouds and Rain chances')
  121. print ('7 - Forecast for 3 next days')
  122. print ('8 - Last 3 searches')
  123. print ('9 - exit')
  124. f = input ("Menu ")
  125. elif z == '2':
  126. import requests
  127.  
  128. a = input('Enter currency to convert from? ')
  129. a = a.upper()
  130.  
  131. b = input('Enter currency to convert to? ')
  132. b = b.upper()
  133.  
  134. c = float(input('Enter value to convert? '))
  135.  
  136. url = ('https://currency-api.appspot.com/api/%s/%s.json') % (a, b)
  137. print(url)
  138.  
  139. r = requests.get(url)
  140. rate = (r.json()['rate'])
  141. print("Current rate of exchange is:" + str(rate))
  142. result = c*float(r.json()['rate'])
  143. print(str(c) + " " + a + " is " + str(result) + " " + b )
  144.  
  145. elif z == '3':
  146. exit()
  147. else:
  148. print('Wrong key')
  149. z = input ("To Lauch Weather Forecast press 1. For currency converter press 2. 3 to Exit \n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement