|
| 1 | +import random |
| 2 | +from time import localtime |
| 3 | +from requests import get, post |
| 4 | +from datetime import datetime, date |
| 5 | +from zhdate import ZhDate |
| 6 | +import sys |
| 7 | +import os |
| 8 | + |
| 9 | + |
| 10 | +def get_color(): |
| 11 | + # 获取随机颜色 |
| 12 | + get_colors = lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF), range(n))) |
| 13 | + color_list = get_colors(100) |
| 14 | + return random.choice(color_list) |
| 15 | + |
| 16 | + |
| 17 | +def get_access_token(): |
| 18 | + # appId |
| 19 | + app_id = config["app_id"] |
| 20 | + # appSecret |
| 21 | + app_secret = config["app_secret"] |
| 22 | + post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}" |
| 23 | + .format(app_id, app_secret)) |
| 24 | + try: |
| 25 | + access_token = get(post_url).json()['access_token'] |
| 26 | + except KeyError: |
| 27 | + print("获取access_token失败,请检查app_id和app_secret是否正确") |
| 28 | + os.system("pause") |
| 29 | + sys.exit(1) |
| 30 | + # print(access_token) |
| 31 | + return access_token |
| 32 | + |
| 33 | + |
| 34 | +def get_weather(region): |
| 35 | + headers = { |
| 36 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' |
| 37 | + 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' |
| 38 | + } |
| 39 | + key = config["weather_key"] |
| 40 | + region_url = "https://geoapi.qweather.com/v2/city/lookup?location={}&key={}".format(region, key) |
| 41 | + response = get(region_url, headers=headers).json() |
| 42 | + if response["code"] == "404": |
| 43 | + print("推送消息失败,请检查地区名是否有误!") |
| 44 | + os.system("pause") |
| 45 | + sys.exit(1) |
| 46 | + elif response["code"] == "401": |
| 47 | + print("推送消息失败,请检查和风天气key是否正确!") |
| 48 | + os.system("pause") |
| 49 | + sys.exit(1) |
| 50 | + else: |
| 51 | + # 获取地区的location--id |
| 52 | + location_id = response["location"][0]["id"] |
| 53 | + weather_url = "https://devapi.qweather.com/v7/weather/now?location={}&key={}".format(location_id, key) |
| 54 | + response = get(weather_url, headers=headers).json() |
| 55 | + # 天气 |
| 56 | + weather = response["now"]["text"] |
| 57 | + # 当前温度 |
| 58 | + temp = response["now"]["temp"] + u"\N{DEGREE SIGN}" + "C" |
| 59 | + # 风向 |
| 60 | + wind_dir = response["now"]["windDir"] |
| 61 | + return weather, temp, wind_dir |
| 62 | + |
| 63 | + |
| 64 | +def get_birthday(birthday, year, today): |
| 65 | + birthday_year = birthday.split("-")[0] |
| 66 | + # 判断是否为农历生日 |
| 67 | + if birthday_year[0] == "r": |
| 68 | + r_mouth = int(birthday.split("-")[1]) |
| 69 | + r_day = int(birthday.split("-")[2]) |
| 70 | + # 获取农历生日的今年对应的月和日 |
| 71 | + try: |
| 72 | + birthday = ZhDate(year, r_mouth, r_day).to_datetime().date() |
| 73 | + except TypeError: |
| 74 | + print("请检查生日的日子是否在今年存在") |
| 75 | + os.system("pause") |
| 76 | + sys.exit(1) |
| 77 | + birthday_month = birthday.month |
| 78 | + birthday_day = birthday.day |
| 79 | + # 今年生日 |
| 80 | + year_date = date(year, birthday_month, birthday_day) |
| 81 | + |
| 82 | + else: |
| 83 | + # 获取国历生日的今年对应月和日 |
| 84 | + birthday_month = int(birthday.split("-")[1]) |
| 85 | + birthday_day = int(birthday.split("-")[2]) |
| 86 | + # 今年生日 |
| 87 | + year_date = date(year, birthday_month, birthday_day) |
| 88 | + # 计算生日年份,如果还没过,按当年减,如果过了需要+1 |
| 89 | + if today > year_date: |
| 90 | + if birthday_year[0] == "r": |
| 91 | + # 获取农历明年生日的月和日 |
| 92 | + r_last_birthday = ZhDate((year + 1), r_mouth, r_day).to_datetime().date() |
| 93 | + birth_date = date((year + 1), r_last_birthday.month, r_last_birthday.day) |
| 94 | + else: |
| 95 | + birth_date = date((year + 1), birthday_month, birthday_day) |
| 96 | + birth_day = str(birth_date.__sub__(today)).split(" ")[0] |
| 97 | + elif today == year_date: |
| 98 | + birth_day = 0 |
| 99 | + else: |
| 100 | + birth_date = year_date |
| 101 | + birth_day = str(birth_date.__sub__(today)).split(" ")[0] |
| 102 | + return birth_day |
| 103 | + |
| 104 | + |
| 105 | +def get_ciba(): |
| 106 | + url = "http://open.iciba.com/dsapi/" |
| 107 | + headers = { |
| 108 | + 'Content-Type': 'application/json', |
| 109 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' |
| 110 | + 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' |
| 111 | + } |
| 112 | + r = get(url, headers=headers) |
| 113 | + note_en = r.json()["content"] |
| 114 | + note_ch = r.json()["note"] |
| 115 | + return note_ch, note_en |
| 116 | + |
| 117 | + |
| 118 | +def send_message(to_user, access_token, region_name, weather, temp, wind_dir, note_ch, note_en): |
| 119 | + url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(access_token) |
| 120 | + week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"] |
| 121 | + year = localtime().tm_year |
| 122 | + month = localtime().tm_mon |
| 123 | + day = localtime().tm_mday |
| 124 | + today = datetime.date(datetime(year=year, month=month, day=day)) |
| 125 | + week = week_list[today.isoweekday() % 7] |
| 126 | + # 获取在一起的日子的日期格式 |
| 127 | + love_year = int(config["love_date"].split("-")[0]) |
| 128 | + love_month = int(config["love_date"].split("-")[1]) |
| 129 | + love_day = int(config["love_date"].split("-")[2]) |
| 130 | + love_date = date(love_year, love_month, love_day) |
| 131 | + # 获取在一起的日期差 |
| 132 | + love_days = str(today.__sub__(love_date)).split(" ")[0] |
| 133 | + # 获取所有生日数据 |
| 134 | + birthdays = {} |
| 135 | + for k, v in config.items(): |
| 136 | + if k[0:5] == "birth": |
| 137 | + birthdays[k] = v |
| 138 | + data = { |
| 139 | + "touser": to_user, |
| 140 | + "template_id": config["template_id"], |
| 141 | + "url": "http://weixin.qq.com/download", |
| 142 | + "topcolor": "#FF0000", |
| 143 | + "data": { |
| 144 | + "date": { |
| 145 | + "value": "{} {}".format(today, week), |
| 146 | + "color": get_color() |
| 147 | + }, |
| 148 | + "region": { |
| 149 | + "value": region_name, |
| 150 | + "color": get_color() |
| 151 | + }, |
| 152 | + "weather": { |
| 153 | + "value": weather, |
| 154 | + "color": get_color() |
| 155 | + }, |
| 156 | + "temp": { |
| 157 | + "value": temp, |
| 158 | + "color": get_color() |
| 159 | + }, |
| 160 | + "wind_dir": { |
| 161 | + "value": wind_dir, |
| 162 | + "color": get_color() |
| 163 | + }, |
| 164 | + "love_day": { |
| 165 | + "value": love_days, |
| 166 | + "color": get_color() |
| 167 | + }, |
| 168 | + "note_en": { |
| 169 | + "value": note_en, |
| 170 | + "color": get_color() |
| 171 | + }, |
| 172 | + "note_ch": { |
| 173 | + "value": note_ch, |
| 174 | + "color": get_color() |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + for key, value in birthdays.items(): |
| 179 | + # 获取距离下次生日的时间 |
| 180 | + birth_day = get_birthday(value["birthday"], year, today) |
| 181 | + if birth_day == 0: |
| 182 | + birthday_data = "今天{}生日哦,祝{}生日快乐!".format(value["name"], value["name"]) |
| 183 | + else: |
| 184 | + birthday_data = "距离{}的生日还有{}天".format(value["name"], birth_day) |
| 185 | + # 将生日数据插入data |
| 186 | + data["data"][key] = {"value": birthday_data, "color": get_color()} |
| 187 | + headers = { |
| 188 | + 'Content-Type': 'application/json', |
| 189 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' |
| 190 | + 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' |
| 191 | + } |
| 192 | + response = post(url, headers=headers, json=data).json() |
| 193 | + if response["errcode"] == 40037: |
| 194 | + print("推送消息失败,请检查模板id是否正确") |
| 195 | + elif response["errcode"] == 40036: |
| 196 | + print("推送消息失败,请检查模板id是否为空") |
| 197 | + elif response["errcode"] == 40003: |
| 198 | + print("推送消息失败,请检查微信号是否正确") |
| 199 | + elif response["errcode"] == 0: |
| 200 | + print("推送消息成功") |
| 201 | + else: |
| 202 | + print(response) |
| 203 | + |
| 204 | + |
| 205 | +if __name__ == "__main__": |
| 206 | + try: |
| 207 | + with open("config.txt", encoding="utf-8") as f: |
| 208 | + config = eval(f.read()) |
| 209 | + except FileNotFoundError: |
| 210 | + print("推送消息失败,请检查config.txt文件是否与程序位于同一路径") |
| 211 | + os.system("pause") |
| 212 | + sys.exit(1) |
| 213 | + except SyntaxError: |
| 214 | + print("推送消息失败,请检查配置文件格式是否正确") |
| 215 | + os.system("pause") |
| 216 | + sys.exit(1) |
| 217 | + |
| 218 | + # 获取accessToken |
| 219 | + accessToken = get_access_token() |
| 220 | + # 接收的用户 |
| 221 | + users = config["user"] |
| 222 | + # 传入地区获取天气信息 |
| 223 | + region = config["region"] |
| 224 | + weather, temp, wind_dir = get_weather(region) |
| 225 | + note_ch = config["note_ch"] |
| 226 | + note_en = config["note_en"] |
| 227 | + if note_ch == "" and note_en == "": |
| 228 | + # 获取词霸每日金句 |
| 229 | + note_ch, note_en = get_ciba() |
| 230 | + # 公众号推送消息 |
| 231 | + for user in users: |
| 232 | + send_message(user, accessToken, region, weather, temp, wind_dir, note_ch, note_en) |
| 233 | + os.system("pause") |
0 commit comments