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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| import random from time import time, localtime import requests from datetime import datetime import re
users = ["###",'####']
template_id='####'
city_id='####'
city="上海"
app_id = "####" app_secret = "####"
to_time=[2017,9,1]
def get_access_token(): post_url = ( f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={app_id}&secret={app_secret}") try:
access_token = requests.get(post_url).json()['access_token'] print('[INFO]获取access_token成功!') return access_token except:print('[INFO]获取access_token失败请检查app_id和app_secret')
def color(): color_ra = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])] return color_ra[0]
def yiyan(): url = 'http://api.botwl.cn/api/yiyan?type=' try: yy = requests.get(url).text print('[INFO]一言模块加载正常') return yy except:print('[INFO]一言模块加载失败,请检查接口')
def cqyq(): url = f'https://opendata.baidu.com/data/inner?tn=reserved_all_res_tn&resource_id=28565&query={city}新型肺炎最新动态' try: data = requests.get(url).json()['data'][0]['result']['items'] data_print = '\n' for i in range(4): title = data[i]['eventDescription'] data_print += title+"\n" print('[INFO]疫情模块加载正常') return data_print except:print('[INFO]疫情模块加载失败,请重新调试')
def get_weather(city_id): t = (int(round(time() * 1000))) headers = { "Referer": "http://www.weather.com.cn/", 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' } url = f"http://d1.weather.com.cn/weather_index/{city_id}.html?_={t}" try: response = requests.get(url, headers=headers) response.encoding = "utf-8" response_data_0 = eval(re.findall("var cityDZ =(.*?);var", response.text)[0]) response_data_2 = eval(re.findall("var dataSK =(.*?);var", response.text)[0]) response_data_3 = eval(re.findall("var dataZS =(.*?);var", response.text)[0]) weather_data = response_data_0["weatherinfo"] city_name = weather_data['city'] max_wd = weather_data["temp"] min_wd = weather_data["tempn"] weather = weather_data["weather"] shidu = response_data_2['SD'] zs=response_data_3['zs'] tips = '\n晨练'+zs['cl_hint']+':'+zs['cl_des_s']+'\n穿衣小贴士:'+zs['ct_des_s'] print('[INFO]天气模块加载正常') return city_name, weather, max_wd, min_wd,shidu, tips except:print('[INFO]天气模块加载失败,请检查和调试接口')
def send(template_id,user, access_token, city_name , weather, max_wd, min_wd,shidu, tips, yy,cqyq): url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(access_token) week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"] today = datetime.date(datetime(year=localtime().tm_year, month=localtime().tm_mon, day=localtime().tm_mday)) week = week_list[today.isoweekday() % 7] data = { "touser": user, "template_id": template_id, "url": "http://linxi.tk", "topcolor": "#FF0000", "data": { "date": { "value": "{} {}".format(today, week), "color": color() }, "time": { "value": (datetime.today() - datetime(to_time[0],to_time[1],to_time[2])).days, "color": color() }, "city": { "value": city_name, "color": color() }, "weather": { "value": weather, "color": color() }, "min_wd": { "value": min_wd, "color": color() }, "max_wd": { "value": max_wd, "color": color() }, "tips": { "value": tips, "color": color() }, "shidu": { "value": shidu, "color": color() }, "yy": { "value": yy, "color": color() }, "yiqing": { "value": cqyq, "color": color() }, "wh": { "value": "希望收到消息的你永远开心!", "color": color() }, } } headers = { 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' } response = requests.post(url, headers=headers, json=data).json() if response["errcode"] == 0: print("[INFO]推送消息成功") else: print(f"[INFO]微信推送失败:{response}")
if __name__ == "__main__": access_token = get_access_token() yy = yiyan() cqyq = cqyq() for user in users: city_name, weather, max_wd, min_wd,shidu,tips = get_weather(city_id) send(template_id,user, access_token, city_name, weather, max_wd, min_wd,shidu,tips, yy,cqyq)
|