-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherParse.py
137 lines (105 loc) · 3.54 KB
/
WeatherParse.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
import urllib.request
import re
import json
import configparser
import os
from config import *
work_dir = os.path.dirname(os.path.abspath(__file__))
#CONF_FILE = os.path.join(work_dir,'config.ini')
#config = configparser.ConfigParser()
#config.read(CONF_FILE)
def getWeatherXML(path):
xmlTree = urllib.request.urlopen(path).read()
return xmlTree
def getValidTime(root):
validTime = {}
startTime = root.find('.//{urn:cwb:gov:tw:cwbcommon:0.1}startTime').text
endTime = root.find('.//{urn:cwb:gov:tw:cwbcommon:0.1}endTime').text
validTime['startTime'] = startTime
validTime['endTime'] = endTime
return validTime
def getWeatherConents(root):
contents ={}
count = 0
for node in root.iter('{urn:cwb:gov:tw:cwbcommon:0.1}contents'):
for subnode in node.iter():
if '{urn:cwb:gov:tw:cwbcommon:0.1}content' == subnode.tag:
count += 1
contents['content'+str(count)] = []
if '{urn:cwb:gov:tw:cwbcommon:0.1}contentText' == subnode.tag:
contents['content'+str(count)].append(subnode.text.strip())
return contents
def getWeatherHazards(root):
hazards = {}
count = 0
for node in root.iter('{urn:cwb:gov:tw:cwbcommon:0.1}hazards'):
for subnode in node.iter():
if '{urn:cwb:gov:tw:cwbcommon:0.1}hazard' == subnode.tag:
count += 1
hazards['hazard'+str(count)] = []
if '{urn:cwb:gov:tw:cwbcommon:0.1}locationName' == subnode.tag:
hazards['hazard'+str(count)].append(subnode.text)
return hazards
def getCityinfo(f):
data = json.load(f)
resultList = []
for city in data:
resultList.append(city)
return resultList
def getIdentifier(root):
content = root.find('{urn:cwb:gov:tw:cwbcommon:0.1}identifier').text
return content
def getAlarmInfo(hazards, cityInfo):
#search & match alarm city
citySet = set([])
nonCitySet = set([])
for hazard in hazards:
for location in hazards[hazard]:
bCheck = False
for city in cityInfo:
regexArea = re.search(city, location)
if regexArea:
citySet.add(city)
bCheck = True
break
if bCheck == False:
nonCitySet.add(location)
cityList = list(citySet)
nonCityList = list(nonCitySet)
alarmInfo = {}
alarmInfo['alarmCity'] = cityList
alarmInfo['nonMarkCity'] = nonCityList
return alarmInfo
def getAllData(root):
#get weather valid Time
validTime = getValidTime(root)
#get weather contents
contents = getWeatherConents(root)
#get weather hazards
hazards = getWeatherHazards(root)
#get city name
f = open(os.path.join(work_dir,CITY_FILE))
cityInfo = getCityinfo(f)
#search & match alarm city
alarmInfo = getAlarmInfo(hazards, cityInfo)
#combine data
result = []
result.append(validTime)
result.append(contents)
result.append(hazards)
result.append(alarmInfo)
resultDict = {}
resultDict['WeatherAlarm'] = result
return resultDict
def sortHazardsCity(root):
hazards = getWeatherHazards(root)
cityInfoArray = []
for data in hazards:
cityInfoArray += hazards[data]
cityInfoSet = set(cityInfoArray)
return cityInfoSet
def filterHazardCity(cityInfoSet, targetCity):
if cityInfoSet & targetCity:
return True
else:
return False