forecast.py

The forecast program is a little project I worked on to see what I can do in an hour with access to an API or two. It was made for the class
HCDE310: Interactive Systems Design & Technology

The program gets data from the “weather gods” by looking up an input location with the google maps api (https://developers.google.com/maps/) and weather data with forecast.io (https://developer.forecast.io/).

Written using python2.7 (run with python forecast.py))

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import urllib
import urllib2
import json

def checkInput(input):
    if input.lower().startswith("y"):
        return True
    elif input.lower().startswith("n"):
        return False
    else:
        print "The gods are sorry. That is not a valid answer."
        return False

def pretty(obj):
    return json.dumps(obj, sort_keys=True, indent=2)

def caller(key, lat, lng):
    base = "https://api.forecast.io/forecast/"
    return base + key + "/" + str(lat) + "," + str(lng)

def isItRaining(currently):
    if currently["precipIntensity"] is 0:
        print "It is not raining"
    else:
        print "It is raining at", currently["precipIntensity"], "intensity" #whatever that means
    
def isItGoingToRain(currently):
    x = currently["precipProbability"] * 100
    print "There is a", x, "percent chance of rain"
    
def isItCloudy(currently):
    if not currently["cloudCover"] is 0:
        if currently["summary"].lower().startswith("drizzle"):
            print "There is a slight", currently["summary"].lower()
        else:
            print "It is", currently["summary"].lower()
    else:
        print "It is not cloudy"
    
def isItSnowing(currently):
    if (currently["temperature"] < 32) and (currently["precipIntensity"] > 0):
        print "It is snowing at", currently["precipIntensity"], "intensity"
    else:
        print "It is probably not snowing"
    
def temp(currently):
    print "It is currently", currently["temperature"], "degrees Fahrenheit"

def summary(d):
    isItRaining(d)
    isItGoingToRain(d)
    isItCloudy(d)
    isItSnowing(d)
    temp(d)

def userInput(x, d):
    if x == 1:
        isItRaining(d)
    elif x == 2:
        isItGoingToRain(d)
    elif x == 3:
        isItCloudy(d)
    elif x == 4:
        isItSnowing(d)
    elif x == 5:
        temp(d)
    elif x == 0:
        summary(d)
    else:
        print "The gods are not happy with that answer..."
        
def getLocation(input):
    print "Gathering location data..."
    gurl = "http://maps.googleapis.com/maps/api/geocode/json?"
    print " "
    d1 = {"address":input, "sensor":"false"}
    url = gurl + urllib.urlencode(d1)
    response = urllib2.urlopen(url)
    d2 = json.load(response)
    if len(d2["results"]) is 0:  
        print "The gods cannot find your US location..."
        return None
    else:
        print "The gods are now monitoring the weather of",
        for comp in d2["results"][0]["address_components"]:
            print comp["long_name"] + ",",
        print " "
        loc = {}
        loc[0] = d2["results"][0]["geometry"]["location"]["lat"]
        loc[1] = d2["results"][0]["geometry"]["location"]["lng"]
        return loc

def main():
    api_key = "331bcc3a449e5ced40cce116f0bf01de"
    input = raw_input("Please enter your city/town (e.g. Seattle, WA): ")
    loc = getLocation(input)
    if (not loc is None):
        runLoc = True ## determines if the location should be used again
        while runLoc == True:
            lat = loc[0]
            lng = loc[1]
            feed = urllib2.urlopen(caller(api_key, lat, lng))
            d = json.load(feed)
            ##print pretty()
            print "Please ask the weather gods something..."
            print "To inquire about current rainfall, enter 1"
            print "To inquire about future rainfall, enter 2"
            print "To inquire about current cloud cover, enter 3"
            print "To inquire about current snowfall, enter 4"
            print "To inquire about current temperature, enter 5"
            print "For a summary of all of these, press 0"
            print " "
            input = raw_input("Your inquiry? ")
            userInput(int(input), d["currently"])
            print " "
            print "The weather may have changed since you last asked..."
            input = raw_input("Inquire about the current location again? ")
            runLoc = checkInput(input)
            print " "

print "Welcome to the weather gods' domain."
print " "
runProgram = True ## determines if the program should run
while runProgram:
    main()
    uI = raw_input("Inquire about another location? ")
    print " "
    runProgram = checkInput(uI)
print "This program will now terminate..."

Download Source: http://goo.gl/i3Xwnd

More like this