|
| 1 | +import os, subprocess, json, sys, time |
| 2 | +from urllib2 import urlopen |
| 3 | +def getIP(): |
| 4 | + IPAddress = urlopen('http://ip.42.pl/raw').read() |
| 5 | + os.system('wget http://ip-api.com/json/%s -qO .raw.userlocation.json' % (IPAddress)) |
| 6 | + subprocess.Popen(['./parseJson.sh location'], shell = True) |
| 7 | +def get(): |
| 8 | + global formatted_address, zipcode, iso_country_code, city |
| 9 | + lines = [line.rstrip('\n') for line in open('.userlocation.json')] |
| 10 | + for i, line in enumerate(lines): |
| 11 | + if i == 13: |
| 12 | + zipcode = line |
| 13 | + os.system('wget http://maps.googleapis.com/maps/api/geocode/json?address=%s -qO .googlemaps.json' % (zipcode)) |
| 14 | + with open('.googlemaps.json') as data_file: |
| 15 | + data = json.load(data_file) |
| 16 | + formatted_address = data["results"][0]["formatted_address"] |
| 17 | + iso_country_code = data["results"][0]["address_components"][-1]["short_name"] |
| 18 | + city = data["results"][0]["address_components"][1]["long_name"] |
| 19 | + if iso_country_code == "US": |
| 20 | + state = data["results"][0]["address_components"][-2]["short_name"] |
| 21 | +def repl(): |
| 22 | + global formatted_address, zipcode, iso_country_code, city, state |
| 23 | + os.system('wget http://maps.googleapis.com/maps/api/geocode/json?address=%s -qO .googlemaps.json' % (zipcode)) |
| 24 | + with open('.googlemaps.json') as data_file: |
| 25 | + data = json.load(data_file) |
| 26 | + formatted_address = data["results"][0]["formatted_address"] |
| 27 | + city = data["results"][0]["address_components"][1]["long_name"] |
| 28 | + iso_country_code = data["results"][0]["address_components"][-1]["short_name"] |
| 29 | + if iso_country_code == "US": |
| 30 | + state = data["results"][0]["address_components"][-2]["short_name"] |
| 31 | + print "Address replaced with " + formatted_address + "." |
| 32 | + time.sleep(5) |
| 33 | +def check(): |
| 34 | + yes = set(['yes','y', 'ye', '']) |
| 35 | + no = set(['no','n']) |
| 36 | + global zipcode |
| 37 | + print "Is this your location?: " + formatted_address |
| 38 | + sys.stdout.write("[y/n] ") |
| 39 | + response = raw_input() |
| 40 | + if (response in yes) == False and (response in no) == False: |
| 41 | + while 1: |
| 42 | + print "Try again." |
| 43 | + response = raw_input() |
| 44 | + if response in yes or response in no: |
| 45 | + break |
| 46 | + if response in yes: |
| 47 | + print "OK." |
| 48 | + autolocate_status = "correct" |
| 49 | + elif response in no: |
| 50 | + sys.stdout.write("Sorry about that - please enter your city's ZIP code. ") |
| 51 | + while 1: |
| 52 | + zipcode = raw_input() |
| 53 | + print "So your ZIP code is %s? [y/n]" % zipcode |
| 54 | + response_b = raw_input() |
| 55 | + if response_b in yes: |
| 56 | + print "Got it." |
| 57 | + repl() |
| 58 | + break |
| 59 | + if response_b in no: |
| 60 | + print "Please enter your ZIP code."; pass |
| 61 | + os.system('wget http://maps.googleapis.com/maps/api/geocode/json?address=%s -qO .googlemaps.json' % (zipcode)) |
| 62 | +def getWeather(): |
| 63 | + global zipcode, iso_country_code |
| 64 | + os.system('wget "http://api.openweathermap.org/data/2.5/weather?zip=' + zipcode + ',' + iso_country_code + '&units=imperial" -qO .weather.json') |
| 65 | + with open('.weather.json') as data_file: |
| 66 | + weather = json.load(data_file) |
| 67 | + errorcode = weather.get("cod") |
| 68 | + if errorcode == "404": |
| 69 | + print "Sorry! Your area couldn't be found using this API. Try searching for a more general area." |
| 70 | + exit(1) |
| 71 | + condition = weather["weather"][0]["main"] |
| 72 | + temp = json.dumps(weather["main"]["temp"]) |
| 73 | + hitemp = json.dumps(weather["main"]["temp_max"]) |
| 74 | + lotemp = json.dumps(weather["main"]["temp_min"]) |
| 75 | + humidity = json.dumps(weather["main"]["humidity"]) |
| 76 | + pressure = json.dumps(weather["main"]["pressure"]) |
| 77 | + print "CONDITION %s " % condition |
| 78 | + print "TEMPERATURE %sF " % temp |
| 79 | + print "HIGH %sF " % hitemp |
| 80 | + print "LOW %sF " % lotemp |
| 81 | + print "HUMIDITY %s%% " % humidity |
| 82 | + print "PRESSURE %smbar" % pressure |
| 83 | + |
| 84 | +###### |
| 85 | +getIP() |
| 86 | +get() |
| 87 | +check() |
| 88 | +getWeather() |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments