-
Notifications
You must be signed in to change notification settings - Fork 0
/
mausam.py
196 lines (171 loc) · 7.05 KB
/
mausam.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
#
# A Python Program to Get the Weather Info Using Yahoo Web Services
# Copyright (C) 2013 Saurav Haloi [[email protected]]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# * Steps :
# * Accept the name of the city from command line
# * Using Yahoo Map API, get the latitude & longitude of the city
# * Get the WOEID (Where On Earth ID) of the city with the latutide & longitude by using Flickr API
# * Use the WOEID to get the Weather info of the city through Yahoo Weather API
#
import os
import sys
import urllib
import urllib2
import xml.dom.minidom
from urllib2 import Request, urlopen, URLError
from xml.dom import Node
from xml.etree import ElementTree
def clearScreen():
if os.name == "posix":
os.system("clear")
elif os.name in ("nt", "dos", "ce"):
os.system("cls")
def printLatLonInfo(CITY):
MyAppID="fmIN.drV34HOC8FaDjSoo6ArkYvtwg1p8WYMf2xnUVBPqHTZkq4raMB0_0.1NcRV.AaKHFoa"
LatLonURL="http://local.yahooapis.com/MapsService/V1/geocode?appid="+MyAppID+"&city="+CITY
try:
response=urllib2.urlopen(LatLonURL)
except URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
parseLatLonXML(response)
def parseLatLonXML(response):
document=response.read()
dom = xml.dom.minidom.parseString(document)
Latitude = []
Longitude = []
City = []
State = []
Country = []
for node in dom.getElementsByTagName('Latitude'):
#print 'Latitude: ' + node.firstChild.nodeValue
Latitude.append(node.firstChild.nodeValue)
for node in dom.getElementsByTagName('Longitude'):
Longitude.append(node.firstChild.nodeValue)
for node in dom.getElementsByTagName('City'):
City.append(node.firstChild.nodeValue)
for node in dom.getElementsByTagName('State'):
State.append(node.firstChild.nodeValue)
for node in dom.getElementsByTagName('Country'):
Country.append(node.firstChild.nodeValue)
count = int(len(City))
if count == 0:
print "No Results Found"
exit(1)
else:
print str(count) + " Results Found.\n"
if count > 1:
for i in range(0,count):
print str(i) + ". " + City[i] + " " + State[i] + " " + Country[i]
print '--------------------------------------'
sys.stdout.write("Enter Your Choice :\t")
choice = int(raw_input())
if choice < 0 or choice >= count:
print 'Error: Out Of Range !!! Exiting...'
exit()
else:
print "### Weather Infomation for " + City[choice] + " " + State[choice] + " " + Country[choice] + " ###\n"
print 'Latitude: ' + Latitude[choice]
print 'Longitude: ' + Longitude[choice]
print 'City: ' + City[choice]
print 'State: ' + State[choice]
print 'Country: ' + Country[choice]
woeid=findWOEID(Latitude[choice],Longitude[choice])
print 'Where On Earth ID: ' + woeid + "\n"
weather(woeid)
print '--------------------------------------'
else:
count = count - 1
print "### Weather Infomation for " + City[count] + " " + State[count] + " " + Country[count] + " ###\n"
print 'Latitude: ' + Latitude[count]
print 'Longitude: ' + Longitude[count]
print 'City: ' + City[count]
print 'State: ' + State[count]
print 'Country: ' + Country[count]
woeid=findWOEID(Latitude[count],Longitude[count])
print 'Where On Earth ID: ' + woeid + "\n"
weather(woeid)
print '--------------------------------------'
def findWOEID(lat,lon):
WOEID_YQL="select%20place.woeid%20from%20flickr.places%20where%20lat%3D"+lat+"%20and%20lon%3D+"+lon
WOEID_URL="http://query.yahooapis.com/v1/public/yql?q="+WOEID_YQL+"&diagnostics=true"
try:
response=urllib2.urlopen(WOEID_URL)
except URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
#tree = ElementTree.parse(response)
#node = tree.findall("/place")
#for item in node:
#print item
document=response.read()
dom = xml.dom.minidom.parseString(document)
for node in dom.getElementsByTagName('place'):
WOEID = node.getAttribute("woeid")
return WOEID
def weather(woeid):
WEATHER_URL="http://weather.yahooapis.com/forecastrss?w="+woeid+"&u=c"
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
try:
response=urllib2.urlopen(WEATHER_URL)
except URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
dom = xml.dom.minidom.parse(response)
for node in dom.getElementsByTagNameNS(WEATHER_NS, 'condition'):
print 'Date: ' + node.getAttribute('date') + "\n"
print 'Current Condition: ' + node.getAttribute('text')
print 'Current Temperature: ' + node.getAttribute('temp') + ' C'
for node in dom.getElementsByTagNameNS(WEATHER_NS, 'atmosphere'):
print "Humidity: " + node.getAttribute('humidity') + "%"
for node in dom.getElementsByTagNameNS(WEATHER_NS, 'astronomy'):
print "Sun Rise: " + node.getAttribute('sunrise')
print "Sun Set: " + node.getAttribute('sunset')
for node in dom.getElementsByTagNameNS(WEATHER_NS, 'forecast'):
print "\n"
print 'Forcast for Date: ' + node.getAttribute('day') + " " + node.getAttribute('date')
print 'Minimum Temperature: ' + node.getAttribute('low') + ' C'
print 'Maximum Temperature: ' + node.getAttribute('high') + ' C'
print 'Condition: ' + node.getAttribute('text')
def main():
clearScreen()
if len(sys.argv) != 2:
print "Usage: " + sys.argv[0] + " <Name of City>"
exit(1)
else:
CITY = sys.argv[1]
print "You Asked for Weather Information of city \"" + CITY + "\"."
printLatLonInfo(CITY)
print "Good Bye..."
main()