前阵子闲得无聊 公司又心血来潮准备用python重构代码(一脸问号 二脸懵逼 三问自己不是前端吗),
学习使用python做了一个小工具 大神互喷 注释一些 凑活看吧
import json
import requests
from zhdate import ZhDate
import datetime
# 登录网址 http://mp.weixin.qq.com/
# 微信测试公众号app_id(需配置)
app_id = "xxxxxxx"
# 微信测试公众号secret(需配置)
secret = "xxxxxxx"
# 高德接口天气权限开通(需配置) https://lbs.amap.com/ (参考:https://blog.csdn.net/qq_51055690/article/details/126885110)
weather_key = "xxxxxxx"
# 微信测试公众号 扫描测试号二维码(需配置)
open_id = ['xxxxxxx', 'xxxxxxxx']
# 模板id 模板在下方可参考(需配置)
template_id = ["xxxxxxx",
"xxxxxxx"]
# 模板每日一题详情页 需要配置自己的服务器地址 http://xxx/checkout_examination
tepmlate_detail_url = "http://snowlove.synology.me:5131/checkout_examination"
# 天气查询城市码 作者山东青岛 码到高德去搜(需配置)
city_code = "370200"
# 转星期常量
WEEK_LIST = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
# 距春节 每年不一样(需配置)
festival_day = [2023, 1, 22]
# 在一起的时间点 自己算(需配置)
together_days = [2021, 6, 14]
# 生日(需配置) 自己算(需配置)
birthday = [int(datetime.datetime.today().year) + 1, 1, 5]
# 主方法 发送请求
def main(body):
# token
# print("access_token",access_token,token.json())
access_token = requests.get(
url='https://api.weixin.qq.com/cgi-bin/token',
params={
"grant_type": "client_credential",
"appid": app_id,
"secret": secret
}
).json()['access_token']
# print("access_token",access_token)
r = requests.post(
url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s" % str(
access_token),
data=json.dumps(body)
)
print(json.loads(r.text))
return json.loads(r.text)
# 彩虹屁 (刻在模板中加入该项)
def get_words():
return '\n' + requests.get("https://api.shadiao.pro/chp").json()['data']['text']
# 获取天气信息
def get_weather():
result = ['', '']
json_data = json.loads(requests.get(
url="https://restapi.amap.com/v3/weather/weatherInfo",
params={
"city": city_code,
"key": weather_key,
"extensions": "all"
}
).text)
print(json_data)
if json_data["status"] == "1":
for forecast in json_data["forecasts"][0].items():
for casts in forecast:
if isinstance(casts, list):
for i, cast in enumerate(casts):
if i == 0:
result[i] = [cast["dayweather"],
cast["daytemp"], cast["daywind"]]
elif i == 1:
result[i] = [cast["dayweather"], cast["daytemp"],
cast["nightweather"], cast["nighttemp"]]
return result
return [['未知', '0', '无'], ['未知', '0', '未知', '0']]
# 转农历方法
def get_zh_date():
toady = datetime.date.today()
date = [toady.year, toady.month, toady.day]
print("date", date)
zh_date = ZhDate.from_datetime(
datetime.datetime(date[0], date[1], date[2]))
print(zh_date.chinese())
return zh_date
# 获取距春节的天气
def get_festival():
return (datetime.date(festival_day[0], festival_day[1], festival_day[2]) - datetime.date.today()).days
# 获取诗词
def get_poetry_words():
poetry = requests.get("https://v1.jinrishici.com/all.json").json()
return "%s \n————《%s》 %s" % (poetry['content'], poetry['origin'], poetry['author'])
# 获取备忘录
def get_memorandum():
try:
memorandum = requests.get(
url="http://snowlove.synology.me:5131/get_memorandum"
).json()
print("memorandum", memorandum)
if (memorandum['code'] == 0):
return memorandum['memorandum'], memorandum['examination']
else:
return '暂无'
except Exception as e:
print("get_memorandum")
print(e)
return '暂无', '暂无'
# 计算生日 避免新一年计算数据问题
def calculate_birthday():
if datetime.date.today().month == 1 and datetime.date.today().day <= 5 and datetime.datetime.now().timestamp() >= datetime.datetime.strptime(str((birthday[0] - 2)) + '-12-31 00:00:00', '%Y-%m-%d %H:%M:%S').timestamp():
birthday[0] = int(datetime.datetime.today().year)
# def read_acquaintance():
# with open(filename, 'r', encoding='utf-8') as fileobj:
# for line in fileobj:
# global acquaintance
# acquaintance = int(line.rstrip())
# def write_acquaintance():
# with open(filename, 'w', encoding='utf-8') as fileobj:
# fileobj.write(str(acquaintance + 1))
# 模板拼接参数 可添加对应参数 也可注释掉对应参数
def json_params(open_id, temp_id, memorandum, examination):
weather = get_weather()
th_days = (datetime.date.today(
) - datetime.date(together_days[0], together_days[1], together_days[2])).days
# 计算跨年时间
calculate_birthday()
bh_days = (datetime.date(
birthday[0], birthday[1], birthday[2]) - datetime.date.today()).days
bh_days = '有个屁啦~宝宝今天过生日啦' if bh_days == 0 else (str(bh_days) + ' 天')
data = {
"touser": open_id,
"template_id": temp_id,
"url": tepmlate_detail_url,
"topcolor": "#FF0000",
"data": {
'date': {
'value': datetime.datetime.now().strftime('%Y-%m-%d'),
'color': '#228B22'
},
'zhdate': {
'value': get_zh_date().chinese(),
'color': '#73767a'
},
'festival': {
'value': get_festival(),
'color': '#228B22'
},
'week': {
'value': WEEK_LIST[datetime.datetime.now().weekday()],
'color': '#228B22'
},
'weather': {
'value': weather[0][0],
'color': '#FF8C00'
},
'temperature': {
'value': weather[0][2] + '℃',
'color': '#eebe77'
},
'wind': {
'value': weather[0][2] + '风',
'color': '#FF69B4'
},
'dayweather': {
'value': weather[1][0],
'color': '#00BFFF'
},
'daytemp': {
'value': weather[1][3] + '℃',
'color': '#00BFFF'
},
'nightweather': {
'value': weather[1][2],
'color': '#00BFFF'
},
'nighttemp': {
'value': weather[1][3] + '℃',
'color': '#00BFFF'
},
'togetherdays': {
'value': th_days,
'color': '#FF4500'
},
'birthdays': {
'value': bh_days,
'color': '#FFD700'
},
# 'caihongpi': { # 情话
# 'value': get_words(),
# 'color': '#F56C6C'
# },
'poetry': { # 诗词
'value': get_poetry_words(),
'color': '#00BFFF'
},
'examination': {
'value': examination,
'color': '#228B22'
},
'memorandum': {
'value': memorandum,
'color': '#00BFFF'
}
}
}
print(data)
return data
# 主方法
if __name__ == '__main__':
memorandum, examination = get_memorandum()
for open_id in open_id:
for temp_id in template_id:
main(json_params(open_id, temp_id, memorandum, examination))
# get_memorandum()
最终效果
放在最后 好久没更新了 这阵子工作太忙了 学到了好多东西也没空写 之后会陆续不上的
评论 (0)