Created
February 22, 2015 01:09
-
-
Save FredrikAugust/48ef53bedb65da055a4e to your computer and use it in GitHub Desktop.
Python Weather
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=<utf-8> | |
# Weather Check | |
# Created by MrMadsenMalmo and MrTijn | |
# Import libs | |
import pywapi as weatherModule | |
import sys | |
import string | |
from time import sleep | |
# List of availible cities | |
locations = { | |
"oslo": "NOXX0029", # These are the country codes used to retrieve the weather | |
"rotterdam": "NLXX0015", | |
"new york": "USNY0996", | |
"stockholm": "SWXX0031", | |
"bergen": "NOXX0004", | |
"amsterdam": "NLXX0002", | |
"london": "UKXX0085" | |
} | |
# Check if the users input is valid | |
def CheckUserInput(userinput): | |
if userinput: | |
location = 0 # Location is a falsy value by default | |
for place in locations: # For each key-value pair in the associative array locations | |
if userinput == place: # If the users input matches one of the values | |
location = locations[place] # Set the location variable to the country code | |
return location | |
if location == 0: | |
print "There is no such location\n\nThe availible locations are:" | |
for place in locations: | |
print place | |
sleep(10) # Gives the user time to read the availible locations | |
sys.exit("\nQuit: No such location") | |
else: | |
print "You did not enter a location" | |
sleep(3) # Gives the user time to read the error message | |
sys.exit("\nQuit: You did not enter a value") | |
#Retrieve the weather | |
def RetrieveWeatherInfo(location): | |
print "Retrieving weather from Yahoo..\n" | |
weather = weatherModule.get_weather_from_yahoo(location) # Retrieve the weather using the location code defined above | |
return weather | |
#Parse the weather info | |
def ParseWeatherInfo(weather): | |
description = string.capitalize(weather["condition"]["text"]) # Traverse the XML result and find the text and then capitalize it | |
temperature = weather["condition"]["temp"] # Same as above, only this time find the temperature | |
parsedWeatherInfo = [description, temperature] | |
return parsedWeatherInfo | |
def main(): | |
location = CheckUserInput(sys.argv[1].lower()) | |
weather = RetrieveWeatherInfo(location) | |
weatherInfo = ParseWeatherInfo(weather) | |
print("The weather in " + string.capitalize(sys.argv[1]) + " is:\n" + weatherInfo[0] + " and " + weatherInfo[1] + "C") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment