-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_extractor_daemon.py
144 lines (123 loc) · 4.91 KB
/
weather_extractor_daemon.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from pyonep import onep
import HTMLParser
import json
import urllib2
import time
from daemon import runner
class weatherParser(HTMLParser.HTMLParser):
data = {}
def __init__(self):
self.processingTag = None
self.processingData = None
self.handledTags = ['td']
self.handledData = [
'時間', '天氣狀況', '溫度(℃)', '相對溼度', '降雨機率'
]
HTMLParser.HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag in self.handledTags:
self.processingTag = tag
if tag == 'img':
if self.processingData == '天氣狀況':
for name, value in attrs:
if name == 'alt':
if self.processingData in self.data:
self.data[ self.processingData ].append(value)
else:
self.data[ self.processingData ] = [value]
def handle_endtag(self, tag):
if tag == self.processingTag:
self.processingTag = None
if tag == 'tr':
self.processingData = None
def handle_data(self, data):
if self.processingTag:
if self.processingData:
if self.processingData in self.data:
self.data[ self.processingData ].append(data)
else:
self.data[ self.processingData ] = [data]
else:
if data in self.handledData:
self.processingData = data
def dumpData(self):
print "data: %s\n\n" % (self.data)
for key in self.handledData:
print "%s: %s\n" % (key, self.data[key])
class naunnanWeather:
pm25 = 0.0
windSpeed = 0.0
curTemp = 0.0
CurHum = 0.0
preTemp = 0.0
preHum = 0.0
curStat = ''
preStat = ''
CIK = 'c7daa5a76badd48e8b8a7b71560670f66ba23c4b'
# Parse HTML documeent to get data wanted and return as a Json object
def _parseCwbHTML(self, htmlData):
return None
def _getWeatherData(self, weatherType):
if weatherType == 'epa':
f = urllib2.urlopen('http://opendata.epa.gov.tw/ws/Data/AQX/?$filter=SiteName%20eq%20%27%E5%BF%A0%E6%98%8E%27&$orderby=SiteName&$skip=0&$top=1000&format=json')
return json.loads( f.read() )[0]
elif weatherType == 'cwb':
f2 = urllib2.urlopen('http://www.cwb.gov.tw/V7/forecast/town368/3Hr/6600400.htm')
wp = weatherParser()
wp.feed( f2.read() )
return wp.data
else:
return None
# Download weather information from EPA
def getWeatherInfo(self):
epaData = self._getWeatherData('epa')
self.pm25 = epaData['PM2.5']
self.windSpeed = epaData['WindSpeed']
cwbData = self._getWeatherData('cwb')
self.curTemp = cwbData['溫度(℃)'][0]
self.curHum = cwbData['相對溼度'][0]
self.curStat = cwbData['天氣狀況'][0]
self.preTemp = cwbData['溫度(℃)'][1]
self.preHum = cwbData['相對溼度'][1]
self.preStat = cwbData['天氣狀況'][1]
def dumpData(self):
print "pm25: %s" % (self.pm25)
print "windSpeed: %s" % (self.windSpeed)
print "curTemp: %s" % (self.curTemp)
print "preTemp: %s" % (self.preTemp)
print "curHum: %s" % (float(self.curHum.strip('%')) / 100)
print "preHum: %s" % (float(self.preHum.strip('%')) / 100)
print "curStat: %s" % (self.curStat)
print "preStat: %s" % (self.preStat)
# Write data to OneP
def updateWeatherDataToCloud(self):
# Connect to Oneplatform
oc = onep.OnepV1()
oc.write(self.CIK, {"alias": "outdoor_pm25"}, self.pm25)
oc.write(self.CIK, {"alias": "outdoor_windspeed"}, self.windSpeed)
oc.write(self.CIK, {"alias": "outdoor_curTemp"}, self.curTemp)
oc.write(self.CIK, {"alias": "outdoor_preTemp"}, self.preTemp)
oc.write(self.CIK, {"alias": "outdoor_curHumidity"}, self.curHum.strip('%'))
oc.write(self.CIK, {"alias": "outdoor_preHumidity"}, self.preHum.strip('%'))
oc.write(self.CIK, {"alias": "outdoor_curStat"}, self.curStat)
oc.write(self.CIK, {"alias": "outdoor_preStat"}, self.preStat)
class DataExtractor:
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/dataExtractor.pid'
self.pidfile_timeout = 5
self.nw = naunnanWeather()
def run(self):
while True:
self.nw.getWeatherInfo()
self.nw.dumpData()
self.nw.updateWeatherDataToCloud()
time.sleep(60)
if __name__ == '__main__':
dataExtractor = DataExtractor()
daemonRunner = runner.DaemonRunner(dataExtractor)
daemonRunner.do_action()