-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (44 loc) · 2.12 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
from datetime import date, datetime, timedelta
import math
from wechatpy import WeChatClient
from wechatpy.client.api import WeChatMessage, WeChatTemplate
import requests
import os
import random
today = datetime.now() + timedelta(hours=8)
start_date = os.environ['START_DATE']
city = os.environ['CITY']
birthday = os.environ['BIRTHDAY']
app_id = os.environ["APP_ID"]
app_secret = os.environ["APP_SECRET"]
user_ids = os.environ["USER_ID"].split("\n")
template_id = os.environ["TEMPLATE_ID"]
def get_weather():
url = "http://autodev.openspeech.cn/csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=" + city
res = requests.get(url).json()
weather = res['data']['list'][0]
return weather['weather'], math.floor(weather['temp']), math.floor(weather['high']), math.floor(weather['low'])
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_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, highest, lowest = get_weather()
data = {"city":{"value":city,"color":get_random_color()},"date":{"value":today.strftime('%Y年%m月%d日'),"color":get_random_color()},"weather":{"value":wea,"color":get_random_color()},"temperature":{"value":temperature,"color":get_random_color()},"love_days":{"value":get_count(),"color":get_random_color()},"birthday_left":{"value":get_birthday(),"color":get_random_color()},"words":{"value":get_words(),"color":get_random_color()},"highest": {"value":highest,"color":get_random_color()},"lowest":{"value":lowest, "color":get_random_color()}}
count = 0
for user_id in user_ids:
res = wm.send_template(user_id, template_id, data)
count+=1
print("发送了" + str(count) + "条消息")