-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherGetter.py
104 lines (76 loc) · 2.88 KB
/
WeatherGetter.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
#!python
from urllib3 import PoolManager
from bs4 import BeautifulSoup
"""
WeatherGetter: retrieve, and store parsed XML data in a more comfortable interface.
version 0.1.0: initial version.
"""
__desc__ = """
Moncton.py: retrieve, and store parsed XML data in a more comfortable interface.
USAGE: from Moncton.py import WeatherGetter, or run it from the command line to
display current conditions.
"""
class WeatherGetter:
"""A simple WeatherGetter class to fetch the weather for a given URL
TODO: Refactor this into a generic WeatherGetter class.
"""
moncton = 'https://weather.gc.ca/rss/city/nb-36_e.xml'
def __init__(self):
self.http = PoolManager()
self.location = self.moncton # TODO generic
self.weather = ''
self.lastUpdated = ''
self.warnings = ''
self.condition = ''
self.lastUpdated = ''
self.forecasts = []
def get_page(self):
self.response = self.http.request('GET', self.location)
if self.response.status == 200:
self.weather = BeautifulSoup(self.response.data, "lxml-xml")
return True
return False
def get_warning(self):
"""Return the text of the current weather warnings, otherwise empty string"""
return self.warnings
def get_condition(self):
"""Return the text of the current weather condition, otherwise empty string"""
return self.condition
def get_last_updated(self):
return self.lastUpdated
def parse_weather(self):
"""private method, used to parse weather page and extract current conditions and alerts"""
if not self.weather:
return False
self.lastUpdated = self.weather.find("updated").get_text()
entries = self.weather.find_all("entry")
self.warnings = ''
self.condition = ''
self.forecasts = []
for entry in entries:
category = entry.find("category").get("term")
if category == 'Warnings and Watches':
self.warnings = entry.find("title").get_text()
continue
if category == 'Current Conditions':
self.condition = entry.find("summary").get_text()
continue
if category == 'Weather Forecasts':
self.forecasts.append(entry)
continue
print(f"Unknown entry: {category}")
return True
# Main function, primarily for testing.
def main(args):
wg = WeatherGetter()
if wg.get_page():
if wg.parse_weather():
print(f"Last Updated: {wg.get_last_updated()}")
print(f"Warnings: {wg.get_warning()}")
print(f"Current Conditions: {wg.get_condition()}")
else:
print("No Weather Data")
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))