前幾個月寫了一個自動推送的文章,由於作者接口出現問題不能使用,大概是上周四出現問題,一直到現在還不能使用。可能是提醒習慣了,我每天的工作日報有時候就忘記寫。想起來調用server醬,雖然免費5條夠用,但是折疊起來,看起來很麻煩,通過搜索,找到一篇文章,不需要部署服務,直接通過企業微信接口調用。
接口原作者:https://ley.best/push-msgs-to-wechat-and-dingding/
原作者的文章,用的是圖文消息,但推送的字體很小,而且不能超過512字節,發送的消息文字顯示不全;文本消息可以達到2048字節,所以改成文本。
自己想要什么都可以更改,看企業微信接口文檔:https://work.weixin.qq.com/api/doc/90000/90135/90236
代碼寫的很爛,不能看的就關閉!
from lxml import etree from datetime import datetime import requests, sys, urllib, json class Worker: def __init__(self): self.now_time = datetime.today() self.headers= {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'} self.url = 'http://api.tianapi.com/txapi/jiejiari/index?key=自己的&date=' def my_job(self): url = self.url + str(self.now_time) r = requests.get(url=url, headers=self.headers) content = eval(r.text) # print(content) newlist = content['newslist'][0] infos = '' if int(content['code']) == 200: if newlist['info'] == '工作日': infos = '\n' + '今天是工作日,要寫日報!!!!' + '\n' return infos elif newlist['info'] == '雙休日': infos = '今天是雙休日,要寫下周的工作計划' return infos elif newlist['tip'] != '': infos = '國家法定節假日,可以好好休息,別忘了周工作計划和月工作計划' return infos else: pass else: infos = '接口錯誤' return infos # print(infos) return infos class YouJia: def __init__(self): self.url = 'http://api.tianapi.com/txapi/oilprice/index?key=自己的&prov=河南' self.data = '' def money(self): r = requests.get(url=self.url).text content = eval(r) #print(content) if content['code'] == 200: print(content['newslist'][0]['p92']) self.data = '92號油價:' + content['newslist'][0]['p92'] + ' 95號油價:' + content['newslist'][0]['p95'] else: self.data = '油價接口錯誤!!!' return self.data class Weather(): def __init__(self): self.data={ "location":"自己的城市", "key":"自己的" } def get_url(self): # API地址用S6,也可以用V7 data_list = requests.get('https://free-api.heweather.com/s6/weather/forecast',params=self.data).json() daily_forecast = data_list["HeWeather6"][0]["daily_forecast"] forecast_list = [] for i in daily_forecast: # print(daily_forecast.index(i)) forecast = {} # 取列表索引 if daily_forecast.index(i) == 0: forecast['時間'] = '今天 ' elif daily_forecast.index(i) == 1: forecast['時間'] = '明天 ' else: forecast['時間'] = '后天 ' all_forecast = forecast['時間'] + ' 白天:'+ i['cond_txt_d'] + ' 晚上:' + i['cond_txt_n'] + ' 最高溫度:'+ i['tmp_max'] + '°C' + ' 最低溫度:'+ i['tmp_min'] + '°C' + ' 風力:' + i['wind_dir'] + i['wind_sc'] + '級' + '!' forecast_list.append(all_forecast) select_forecast = "".join(forecast_list) new_data = select_forecast.replace('!', '\n') return new_data class YiQing: def __init__(self): self.url = 'http://api.tianapi.com/txapi/ncov/index?key=自己的' self.all_data = '' self.henan_news = '' def request_data(self): r = requests.get(self.url).text content = eval(r) # print(type(content)) news_list = content["newslist"][0] all_news = news_list['news'] news_num = len(all_news) nationwide = all_news[0]['summary'] # print(nationwide) for i in range(news_num): if all_news[i]['id'] == 150879: self.henan_news = all_news[i]['summary'] break else: self.henan_news = '河南—————————暫無疫情' self.all_data = nationwide + '\n' + '-'*34 + '\n' + '\n' + self.henan_news return self.all_data class Weixin(): def __init__(self,myjob_data, youjia_data, tianqi_data, yiqing_data): self.corpid = '自己的' self.corpsecret = '自己的' self.HEADERS = {"Content-Type": "application/json ;charset=utf-8"} self.myjob = myjob_data self.youjia = youjia_data self.tianqi = tianqi_data self.yiqing = yiqing_data def news(self): send_data = self.myjob + '\n' + '-'*34 + '\n' + self.youjia + '\n' + '-'*34 + self.tianqi + '-'*34 + self.yiqing r = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + self.corpid + '&corpsecret=' + self.corpsecret).text # print(r) js = json.loads(r) token = js['access_token'] data = { 'touser': '@all', 'msgtype': 'text', 'agentid': 1000002, #自己更改 'text':{ 'content': send_data }, 'safe': 0, 'enable_id_trans': 0, 'enable_duplicate_check': 0, 'duplicate_check_interval': 1800 } String_testMsg = json.dumps(data) wechaturl = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}' # print(wechaturl) req = requests.post(wechaturl, data=String_testMsg, headers=self.HEADERS) # print(req.text) def main(): workers = Worker() youjias = YouJia() tianqis = Weather() yiqings = YiQing() wechat = Weixin(workers.my_job(), youjias.money(), tianqis.get_url(), yiqings.request_data()) wechat.news() if __name__ == '__main__': main()