|
import mysql.connector |
|
import requests |
|
import json |
|
import os |
|
import time |
|
from datetime import datetime, timedelta |
|
import pytz |
|
from dingtalkchatbot.chatbot import DingtalkChatbot, ActionCard, FeedLink, CardItem |
|
|
|
def get_subjects_json(): |
|
|
|
mydb = mysql.connector.connect( |
|
host="localhost", |
|
port="13306", |
|
user="root", |
|
password="123456", |
|
database="wewe-rss" |
|
) |
|
|
|
mycursor = mydb.cursor() |
|
query = """SELECT a.id, a.title, a.pic_url, a.publish_time, b.mp_name |
|
FROM articles AS a, feeds AS b |
|
WHERE a.mp_id = b.id |
|
AND a.created_at >= NOW() - INTERVAL 12 HOUR |
|
ORDER BY a.publish_time DESC""" |
|
|
|
mycursor.execute(query) |
|
results = mycursor.fetchall() |
|
|
|
|
|
data = [] |
|
for result in results: |
|
subject = { |
|
"id": result[0], |
|
"title": result[1], |
|
"pic_url": result[2], |
|
"publish_time": result[3], |
|
"mp_name": result[4] |
|
} |
|
data.append(subject) |
|
|
|
json_data = json.dumps(data, indent=4) |
|
print(json_data) |
|
return json_data |
|
|
|
def dingbot_markdown(access_token, secret, rss_list): |
|
new_webhook = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}' |
|
xiaoding = DingtalkChatbot(new_webhook, secret=secret, pc_slide=True, fail_notice=False) |
|
|
|
text = [] |
|
for data in rss_list: |
|
|
|
mp_name = data['mp_name'] |
|
url = 'https://mp.weixin.qq.com/s/' + str(data["id"]) |
|
unix_timestamp = data['publish_time'] |
|
|
|
|
|
time_local = time.localtime(unix_timestamp) |
|
|
|
beijing_time = time.strftime("%Y-%m-%d %H:%M:%S",time_local) |
|
text_content = f'> **{mp_name}** [' + data["title"] + '](' + url + ') ' + str(beijing_time) + '\n' |
|
|
|
text.append(text_content) |
|
|
|
title = '## 微信公众号<最近4小时更新> \n\n' |
|
markdown_text = title + '\n'.join(text) |
|
print(markdown_text) |
|
res = xiaoding.send_markdown(title=title, text=markdown_text) |
|
print(f"send sucess, res: {res}") |
|
|
|
|
|
def send_dingtalk_msg(access_token, secret): |
|
data = get_subjects_json() |
|
rss_list = json.loads(data) |
|
if len(rss_list) != 0: |
|
dingbot_markdown(access_token, secret, rss_list) |
|
|
|
if __name__ == '__main__': |
|
|
|
access_token = '' |
|
secret = '' |
|
|
|
while True: |
|
send_dingtalk_msg(access_token, secret) |
|
time.sleep( 4 * 60 * 60 ) |
|
|