-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathweather.py
47 lines (35 loc) · 1.14 KB
/
weather.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
"""
Written by: Anand Reddy Pandikunta
e-mail: [email protected]
"""
import re
import urllib
import json
def get_ip():
url = "http://checkip.dyndns.org"
data = urllib.urlopen(url).read()
ip_address = data[-30:-16]
return ip_address
def get_weather(ip_address):
end_point = "http://api.worldweatheronline.com/free/v1/weather.ashx?"
query = "key=a37fae643df77aa83d88abbc9e8e96194ab242d4&q=" + str(ip_address) + "&num_of_days=0&format=json"
url = end_point + query
json_data = urllib.urlopen(url).read()
data = json.loads(json_data)
current_weather = data['data']['current_condition'][0]
return current_weather
def print_weather(data):
print """
Weather : %s
Temperatue : %s Celsius
Wind : %s Kmph %s
Humidity : %s
Precipitation : %s MM
""" % (data['weatherDesc'][0]['value'], data['temp_C'], data['windspeedKmph'], data['winddir16Point'], data['humidity'], data['precipMM'])
def main():
print "\nGetting weather information for your location..."
ip_address = get_ip()
data = get_weather(ip_address)
print_weather(data)
if __name__ == "__main__":
main()