forked from 13812851221/-rxrw-daily_morning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (78 loc) · 3.54 KB
/
main.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
from datetime import date, datetime
import math
from wechatpy import WeChatClient
from wechatpy.client.api import WeChatMessage, WeChatTemplate
import requests
import os
import random
today = datetime.now()
start_date = os.environ['START_DATE']
city = os.environ['CITY']
city_name = os.environ['CITY_NAME']
pig_city = os.environ['PIG_CITY']
pig_city_name = os.environ['PIG_CITY_NAME']
birthday = os.environ['BIRTHDAY']
pig_birthday = os.environ['PIG_BIRTHDAY']
app_id = os.environ['APP_ID']
app_secret = os.environ['APP_SECRET']
user_id = os.environ['USER_ID']
user_id_pig = os.environ['USER_ID_PIG']
template_id = os.environ['TEMPLATE_ID']
def get_weather():
url = "https://restapi.amap.com/v3/weather/weatherInfo?key=d38b5352dc107824ff7a345e210f55cf&city=" + city
res = requests.get(url).json()
weather = res['lives'][0]['weather']
temperature = res['lives'][0]['temperature']
wind = res['lives'][0]['winddirection']
wind_power = res['lives'][0]['windpower']
return weather, temperature, wind, wind_power
def get_pig_weather():
url = "https://restapi.amap.com/v3/weather/weatherInfo?key=d38b5352dc107824ff7a345e210f55cf&city=" + pig_city
res = requests.get(url).json()
pig_weather = res['lives'][0]['weather']
pig_temperature = res['lives'][0]['temperature']
pig_wind = res['lives'][0]['winddirection']
pig_wind_power = res['lives'][0]['windpower']
return pig_weather, pig_temperature, pig_wind, pig_wind_power
def get_count():
delta = today - datetime.strptime(start_date, "%Y-%m-%d")
return delta.days
def get_birthday():
next = datetime.strptime(str(date.today().year) + "-" + birthday, "%Y-%m-%d")
if next < datetime.now():
next = next.replace(year=next.year + 1)
return (next - today).days
def get_pig_birthday():
next = datetime.strptime(str(date.today().year) + "-" + pig_birthday, "%Y-%m-%d")
if next < datetime.now():
next = next.replace(year=next.year + 1)
return (next - today).days
def get_words():
words = requests.get("https://api.shadiao.pro/chp")
if words.status_code != 200:
return get_words()
return words.json()['data']['text']
def get_random_color():
return "#%06x" % random.randint(0, 0xFFFFFF)
client = WeChatClient(app_id, app_secret)
wm = WeChatMessage(client)
wea, temperature, wind, wind_power = get_weather()
pig_wea, pig_temperature, pig_wind, pig_wind_power = get_weather()
data = {"city": {"value": city_name, "color": get_random_color()},
"weather": {"value": wea, "color": get_random_color()},
"temperature": {"value": temperature, "color": get_random_color()},
"wind": {"value": wind, "color": get_random_color()},
"wind_power": {"value": wind_power, "color": get_random_color()},
"pig_city": {"value": pig_city_name, "color": get_random_color()},
"pig_weather": {"value": pig_wea, "color": get_random_color()},
"pig_temperature": {"value": pig_temperature, "color": get_random_color()},
"pig_wind": {"value": pig_wind, "color": get_random_color()},
"pig_wind_power": {"value": pig_wind_power, "color": get_random_color()},
"love_days": {"value": get_count(), "color": get_random_color()},
"birthday_left": {"value": get_birthday(), "color": get_random_color()},
"birthday_right": {"value": get_pig_birthday(), "color": get_random_color()},
"words": {"value": get_words(), "color": get_random_color()}}
res = wm.send_template(user_id, template_id, data)
res_pig = wm.send_template(user_id_pig, template_id, data)
print(res)
print(res_pig)