-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
124 lines (105 loc) · 3.65 KB
/
Copy pathcommon.py
File metadata and controls
124 lines (105 loc) · 3.65 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
#!/usr/lib/env python
import os.path
import urllib.request
import urllib.error
import logging
import json
import gzip
import re
from io import BytesIO
root_path = os.path.dirname(os.path.abspath(__file__))
#file path
voice_path = os.path.join(root_path, 'files/voice')
photo_path = os.path.join(root_path, 'files/photo')
#synchronized bot
token = '317386823:xxxxxxxx'
command_list = ['help', 'start', 'weather', 'joke', 'gif', 'me']
command_str = \
'''
/weather get the weather info
/joke get a joke
/gif get a random gif
/me get my info
'''
proxy = 'http://127.0.0.1:8123'
#SSH
ssh_server = 'www.lifetime.photo'
ssh_port = 22
ssh_username = 'pi'
ssh_password = 'xxxxx'
ssh_path = '/home/pi/tmp'
#api url
weather_url = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
gif_url ='http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC'
joke_url = 'http://api.icndb.com/jokes/random/'
def get_weather():
reg = r'[\u4E00-\u9FA5 ]'
request = urllib.request.Request(weather_url, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'})
try:
res = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
logging.error(e.info())
return 'get weather data failed, please try again later...'
data = res.read()
if (('Content-Encoding' in res.info()) and (res.info()['Content-Encoding'] == 'gzip')):
# data=zlib.decompress(data, 16+zlib.MAX_WBITS)
data = gzip.decompress(data)
data = str(data, 'utf-8')
json_data = json.loads(data)
status = json_data['status']
if (status == 1000):
wdata = json_data['data']
city = wdata['city']
#aqi = int(wdata['aqi'])
temperature = wdata['wendu']
forecast = wdata['forecast'][0]
date = forecast['date']
weather = forecast['type']
wind = forecast['fengli']
direction = forecast['fengxiang']
high = re.sub(reg, '', forecast['high'])
low = re.sub(reg, '', forecast['low'])
'''
level = ''
if (aqi <= 50 and aqi > 0):
level = '优秀'
elif (aqi <= 100 and aqi > 50):
level = '良好'
elif (aqi <= 150 and aqi > 100):
level = '一般'
elif (aqi <= 200 and aqi > 150):
level = '很差'
else:
level = '很差'
'''
return '{},今天是{} {}, 当前温度{}[{} ~ {} ],{}{}.'.format(city, date, weather, temperature, low,
high, direction, wind)
else:
return 'get weather data failed, please try again later...'
def get_random_gif():
request = urllib.request.Request(gif_url)
try:
res = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
logging.error(e.info())
return None
if res.getcode() == 200:
json_data = json.loads(res.read().decode('utf-8'))
if json_data['meta']['status'] == 200:
img_url = json_data['data']['image_original_url']
return img_url
return None
def get_random_joke():
request = urllib.request.Request(joke_url)
try:
res = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
logging.error(e.info())
#logging.error(e.read())
return 'get random joke failed, please try again later...'
if res.getcode() == 200:
json_data = json.loads(res.read().decode('utf-8'))
if json_data['type'] == 'success':
return json_data['value']['joke']
return 'get random joke failed, please try again later...'