-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (123 loc) · 5.45 KB
/
Copy pathmain.py
File metadata and controls
154 lines (123 loc) · 5.45 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import logging
from lxml import etree
import urllib3
import json
from time import sleep
from datetime import datetime, timedelta
logging.basicConfig(level=logging.DEBUG, filename="log", format='%(asctime)s %(filename)s : %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
from gtts_prepare import prepare
from ProxyController import ProxyController
def extract_first(html, xpath_str):
"""
:param html: html elements of lxml
:param xpath_str: xpath string
:return: string
"""
r = html.xpath(xpath_str)
if len(r) > 0:
return prepare.rm_space(html.xpath(xpath_str)[0].strip())
else:
logger.warning("None detected!")
return None
def get_loc_with_GPS_macos(path_to_whereami="/usr/local/bin/whereami"):
loc = dict()
with os.popen(path_to_whereami) as p:
for line in p.readlines():
li = [x.strip() for x in line.split(':')]
desc = li[0]
val = li[1]
loc[desc] = val
return loc
def get_loc_with_ip():
pc = ProxyController.ProxyController()
pc.env_proxy_off()
loc = dict()
__url = 'http://ipinfo.io/json'
data = pull_json_parse(__url)
loca = data['loc'].split(',')
loc['Latitude'] = loca[0]
loc['Longitude'] = loca[1]
pc.env_proxy_on()
return loc
def get_loc():
try:
loc = get_loc_with_GPS_macos()
logger.info("Location (GPS): \nLatitude: {0}, Longitude: {1}, Accuracy (m): {2}, Timestamp: {3}".format(loc["Latitude"], loc["Longitude"], loc["Accuracy (m)"], loc["Timestamp"]))
except:
loc = get_loc_with_ip()
logger.info("Location (IP): \nLatitude: {0}, Longitude: {1}".format(loc["Latitude"], loc["Longitude"]))
return loc
def pull_lxml_parse(url=""):
"""
:param url: url that is going to pull
:return: None or html elements of lxml
"""
html_parsed = etree.HTML(pull(url))
return html_parsed
def pull_json_parse(url=""):
return json.loads(pull(url))
def pull(url=""):
if url == "":
logger.error("url is empty.")
return None
logger.info("pulling url: {0}".format(url))
http = urllib3.PoolManager()
html = http.request('GET', url)
logger.info("html status: {0}".format(html.status))
if html.status != 200:
logger.error("Cannot get html data from: {0}".format(url))
return None
else:
return html.data.decode('utf-8')
if __name__ == "__main__":
normal_last_time = datetime.now() - timedelta(minutes=120)
rainy_last_time = datetime.now()
last_time = ""
while True:
loc = get_loc()
_home_url = "http://e.weather.com.cn/d/town/index?lat={0}&lon={1}".format(loc["Latitude"], loc["Longitude"])
_home_html = pull_lxml_parse(_home_url)
time = extract_first(_home_html, "/html/body/div[1]/div[2]/div[1]/time/text()")[:-2]
if not last_time == time:
last_time = time
location = extract_first(_home_html, "/html/body/div[1]/div[2]/div[1]/div/div/text()")
degree = extract_first(_home_html, "/html/body/div[1]/div[2]/div[2]/h1/span/text()")
wind = extract_first(_home_html, "/html/body/div[1]/div[2]/div[2]/h2/span[1]/text()")
humidity = extract_first(_home_html, "/html/body/div[1]/div[2]/div[2]/h2/span[2]/text()")
status = extract_first(_home_html, "/html/body/div[1]/div[2]/div[2]/h1/em/text()")
_air_json_url = "http://e.weather.com.cn/p/custom/?lat={0}&lon={1}".format(loc["Latitude"], loc["Longitude"])
_air_html = pull(_air_json_url)[13:-3]
logger.debug(_air_html)
air = json.loads(_air_html)['result']['air']
air_aqi = air['aqi']
air_lev = air['level']
_detail_json_url = "http://d3.weather.com.cn/webgis_rain_new/webgis/minute?lat={0}&lon={1}".format(loc["Latitude"], loc["Longitude"])
status_desc = prepare.rm_space(pull_json_parse(_detail_json_url)['msg'])
if "雨" in status_desc or "雨" in status:
if "不会下雨" not in status_desc:
if status_desc.find("分钟") != -1:
try:
logger.debug("time to rain parsed from string: {0}".format(int(status_desc[:status_desc.find("分钟")])))
if int(status_desc[:status_desc.find("分钟")]) <= 47:
rainy_last_time = datetime.now()
prepare.say("您好,请注意:")
prepare.say("{0}附近".format(location))
prepare.say("{0}".format(status_desc))
except:
pass
if datetime.now() - normal_last_time > timedelta(minutes=120):
normal_last_time = datetime.now()
# prepare.say("您好,接下来播报")
# prepare.say("{0}附近".format(location))
# prepare.say("的天气情况")
# prepare.say("当前温度:{0}摄氏度".format(degree))
# prepare.say("天气:{0}".format(status))
# prepare.say("{0}".format(wind))
# prepare.say("{0}".format(humidity))
# prepare.say("空气质量:{0}".format(air_lev))
# prepare.say("另外")
# prepare.say("{0}".format(status_desc))
# prepare.say("感谢收听,再见。")
sleep(900) # 15mins