forked from AaronFoltz/cliweather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather
More file actions
executable file
·138 lines (111 loc) · 4.07 KB
/
weather
File metadata and controls
executable file
·138 lines (111 loc) · 4.07 KB
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
#!/usr/bin/env python
from xml.dom.minidom import parseString
try:
from urllib.request import urlopen
except ImportError:
from urllib import urlopen
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
from sys import argv, exit, stderr
import os.path
VERSION = '.01'
URL = 'http://www.aaronfoltz.com'
def main():
try:
# Forecast option
if "-f" in argv[1]:
# See if there is a zip with the argument
try:
place = argv[2]
getForecast(place)
# If no zip is provided, get it from the geolocation service
except IndexError:
place = getPlace()
getForecast(place)
# Else there isn't an option there, it must be a zip code if anything
else:
try:
place = argv[1]
getCurrent(place)
# No arguments are provided, get the place from the geolocation service
except IndexError:
place = getPlace()
getCurrent(place)
except IndexError:
place = getPlace()
getCurrent(place)
def getCurrent(place):
# Call to get the weather
api = 'http://www.google.com/ig/api?%s' % urlencode({'weather': place})
dom = parseString(urlopen(api).read())
items = [ 'city'
, 'condition'
, 'temp_f'
, 'temp_c'
, 'humidity'
, 'wind_condition' ]
info = {}
try:
for item in items:
info[item] = dom.getElementsByTagName(item)[0].getAttribute('data')
except IndexError:
stderr.write('Invalid Area\n')
exit(1)
print('City: %s' % info['city'])
print('Condition: %s' % info['condition'])
print('Temperature: %sC/%sF' % (info['temp_c'], info['temp_f']))
print(info['humidity'])
print(info['wind_condition'])
def getForecast(place):
api = 'http://www.google.com/ig/api?%s' % urlencode({'weather': place})
dom = parseString(urlopen(api).read())
# Information that we can get in the forecast
items = [ 'day_of_week'
, 'high'
, 'low'
, 'condition']
# A main of information with 4 sublists - one for each day in the forecast
info = []
# Add city to the list
info.insert(0, dom.getElementsByTagName('city')[0].getAttribute('data'))
# Gather the number of days
days = dom.getElementsByTagName('day_of_week')
# Gather the information for each day
for day in range(1, len(days)+1):
# Add a sublist for the day
info.append([])
# Gather the available information for each day in the forecast
for item in items:
# Add the information to the end of the sublist for the day
info[day].append(dom.getElementsByTagName(item)[day-1].getAttribute('data'))
print('City: %s' % info[0])
# Print out the information for each day
for day in range(1, len(days)+1):
print('Day: %s' % info[day][0])
print('\tHigh: %dC/%sF' % (fahToCel(info[day][1]), info[day][1]))
print('\tLow: %dC/%sF' % (fahToCel(info[day][2]), info[day][2]))
print('\tCondition: %s' % info[day][3])
def fahToCel(value):
return (100.0/(212-32)) * (int(value) - 32)
def getPlace():
import re
path = "%s/.cliweather" % os.getenv("HOME")
if os.path.exists(path):
try:
f = open(path, 'r')
return f.read()
except:
print "Could not load .cliweather"
# Get the geoip information
geoip = urlopen('http://api.ipinfodb.com/v3/ip-city/?key=29e619f84fd83f32c4987573a4ccc11fd59b268234aa50c1289230f1fff0dbd2').read()
# The returned value is perfectly fine
if re.match("OK;;", geoip):
# regex to get the zip code
expression = "OK;;([^;]+;){5}(\w+)"
result = re.search(expression, geoip)
# return the zip code from the returned data
return result.group(2)
if __name__ == "__main__":
main()