1
+ import requests
2
+ import datetime
3
+ import os
4
+ import json
5
+ import time
6
+ from parse_fullarticle import fetch_article_text
7
+
8
+ # 改变工作目录到脚本所在的目录
9
+ os .chdir (os .path .dirname (os .path .abspath (__file__ )))
10
+
11
+ # 读取配置文件
12
+ with open ('config.json' , 'r' ) as config_file :
13
+ config = json .load (config_file )
14
+
15
+ # 定义浏览器的 User-Agent 头
16
+ headers = {
17
+ "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36" ,
18
+ "Content-Type" : "application/json"
19
+ }
20
+
21
+ def query_fullarticle_data_from_eastmoney (pageindex ):
22
+ url = "https://i.eastmoney.com/api/guba/fullarticlelist"
23
+
24
+ # 获取当前时间的时间戳(秒)
25
+ current_timestamp = time .time ()
26
+ # 将时间戳转换为毫秒
27
+ current_timestamp_ms = int (current_timestamp * 1000 )
28
+
29
+ params = {
30
+ "pageindex" : pageindex ,
31
+ "uid" : config ['uid' ],
32
+ "_" : current_timestamp_ms
33
+ }
34
+
35
+ response = requests .get (url , params = params , headers = headers )
36
+
37
+ if response .status_code == 200 :
38
+ data = response .json ()
39
+ reply_list = data .get ('result' ).get ('list' )
40
+ else :
41
+ response .raise_for_status ()
42
+
43
+ return reply_list
44
+
45
+ def pull_fullarticle_data ():
46
+ try :
47
+ with open ('last_fullarticle_id.txt' , 'r' ) as file :
48
+ last_fullarticle_id = int (file .read ().strip ())
49
+ except FileNotFoundError :
50
+ last_fullarticle_id = 0
51
+ except ValueError :
52
+ last_fullarticle_id = 0
53
+
54
+ new_fullarticle_list = []
55
+
56
+ # 遍历回帖数据,找出最新的回帖保存到new_reply_list中
57
+ meet_old_record = False
58
+ for pageindex in range (1 , 3 ):
59
+ if meet_old_record :
60
+ break
61
+ else :
62
+ fullarticle_list = query_fullarticle_data_from_eastmoney (pageindex )
63
+
64
+ for item in fullarticle_list :
65
+ post_id = int (item .get ('post_source_id' ))
66
+ if post_id > last_fullarticle_id :
67
+ new_fullarticle_list .append (item )
68
+ else :
69
+ meet_old_record = True
70
+ break
71
+
72
+ # 将最新的回帖ID保存到文件中
73
+ if len (new_fullarticle_list ) > 0 :
74
+ latest_post_id = new_fullarticle_list [0 ].get ('post_source_id' )
75
+ with open ('last_fullarticle_id.txt' , 'w' ) as file :
76
+ file .write (str (latest_post_id ))
77
+ else :
78
+ print (f"{ datetime .datetime .now ()} No new full article data found." )
79
+ print ("-" * 40 )
80
+
81
+ send_msg_to_feishu_bot (new_fullarticle_list )
82
+
83
+ def send_msg_to_feishu_bot (new_fullarticle_list ):
84
+ # post_id post_title post_content post_pic_url[] post_publish_time post_user->user_nickname post_guba->stockbar_name stockbar_code
85
+ for post in new_fullarticle_list :
86
+ post_id = post .get ('post_source_id' )
87
+ post_title = post .get ('post_title' )
88
+ post_content = post .get ('post_content' )
89
+ post_pic_url = post .get ('post_pic_url' )
90
+ post_publish_time = post .get ('post_publish_time' )
91
+ post_user_nickname = post .get ('post_user' ).get ('user_nickname' )
92
+ post_guba_name = post .get ('post_guba' ).get ('stockbar_name' )
93
+ post_guba_stockbar_code = post .get ('post_guba' ).get ('stockbar_code' )
94
+
95
+
96
+ url = f"https://caifuhao.eastmoney.com/news/{ post_id } "
97
+ fullarticle_text = fetch_article_text (url )
98
+
99
+ msg = f"🌶🌶🌶长文更新:{ post_id } \n 帖子标题:{ post_title } \n 帖子内容摘要:{ post_content } \n 帖子图片:{ post_pic_url } \n 发布时间:{ post_publish_time } \n 发布用户:{ post_user_nickname } \n 股吧:{ post_guba_name } ({ post_guba_stockbar_code } )\n 长文内容:{ fullarticle_text } "
100
+
101
+ url = config ['feishu_teamchat_bot_url' ]
102
+ data = {
103
+ "msg_type" : "text" ,
104
+ "content" : {
105
+ "text" : msg
106
+ }
107
+ }
108
+ response = requests .post (url , json = data )
109
+
110
+ if response .status_code == 200 :
111
+ print (f"{ datetime .datetime .now ()} { msg } " )
112
+ print ("-" * 40 )
113
+ else :
114
+ response .raise_for_status ()
115
+
116
+ # feishu_post_bot_url = config['feishu_post_bot_url']
117
+ # post_text = f"{post_id}#{post_title}#{post_content}#{post_pic_url}#{post_publish_time}#{post_user_nickname}#{post_guba_name}({post_guba_stockbar_code})"
118
+
119
+ # payload = {
120
+ # "text": post_text
121
+ # }
122
+ # response = requests.post(feishu_post_bot_url, json=payload, headers=headers)
123
+ # if response.status_code != 200:
124
+ # print(f"Failed to send message to Feishu Doc bot: {response.status_code}, {response.text}")
125
+
126
+ if __name__ == "__main__" :
127
+ pull_fullarticle_data ()
0 commit comments