Преглед изворни кода

1、小年糕和祝福圈子增加曝光
2、中青看点请求重试

zhangliang пре 1 месец
родитељ
комит
af6dafacb0

+ 6 - 10
spider/crawler_online/xiaoniangaotuijianliu.py

@@ -25,10 +25,8 @@ from application.common.mysql import MysqlHelper
 
 
 
-video_view_count = 0
-video_view_lists = []
+
 def video_view(content_id, account_id):
-    global video_view_count
     headers = {
         "Content-Type": "application/json"
     }
@@ -64,7 +62,7 @@ def video_view(content_id, account_id):
     except json.JSONDecodeError:
         print("响应不是有效的 JSON 格式")
 
-def video_history():
+def video_history(video_view_lists):
     headers = {
         "Content-Type": "application/json"
     }
@@ -87,7 +85,6 @@ def video_history():
             msg = result.get("msg")
             # 业务逻辑处理(示例)
             if code == 0:
-                video_view_lists.clear()
                 print("请求成功")
             else:
                 print(f"请求失败,错误码: {code}, 消息: {msg}")
@@ -150,11 +147,14 @@ class XNGTJLRecommend(object):
                     message="抓取单条视频失败,请求失败"
                 ),
                 return
+            video_view_lists = []
             for index, video_obj in enumerate(response['data']['data'], 1):
                 try:
                     self.aliyun_log.logging(
                         code="1001", message="扫描到一条视频", data=video_obj
                     )
+                    vid = video_obj['id']
+                    video_view_lists.append(str(vid))
                     self.process_video_obj(video_obj, title_rule)
                 except Exception as e:
                     self.aliyun_log.logging(
@@ -165,6 +165,7 @@ class XNGTJLRecommend(object):
                     )
                 if self.limit_flag:
                     return
+                video_history(video_view_lists)
                 time.sleep(random.randint(5, 10))
 
     def process_video_obj(self, video_obj, title_rule):
@@ -251,15 +252,10 @@ class XNGTJLRecommend(object):
             self.download_cnt += 1
             self.mq.send_msg(mq_obj)
             video_view(vid, mid)
-            video_view_count += 1
-            video_view_lists.append(str(vid))
-            if video_view_count % 4 == 0:
-                video_history()
             self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
             if self.download_cnt >= int(
                     self.rule_dict.get("videos_cnt", {}).get("min", 200)
             ):
-                video_history()
                 self.limit_flag = True
 
     """

+ 7 - 12
spider/crawler_online/zhufuquanzituijianliu.py

@@ -24,10 +24,8 @@ from application.common.messageQueue import MQ
 from application.common.log import AliyunLogger
 from application.common.mysql import MysqlHelper
 
-video_view_count = 0
-video_view_lists = []
+
 def video_view(content_id, account_id):
-    global video_view_count
     headers = {
         "Content-Type": "application/json"
     }
@@ -52,8 +50,6 @@ def video_view(content_id, account_id):
             # 业务逻辑处理(示例)
             if code == 0:
                 print("请求成功")
-                video_view_count += 1
-                video_history(content_id)
             else:
                 print(f"请求失败,错误码: {code}, 消息: {msg}")
         else:
@@ -63,7 +59,7 @@ def video_view(content_id, account_id):
     except json.JSONDecodeError:
         print("响应不是有效的 JSON 格式")
 
-def video_history():
+def video_history(video_view_lists):
     headers = {
         "Content-Type": "application/json"
     }
@@ -86,7 +82,6 @@ def video_history():
             msg = result.get("msg")
             # 业务逻辑处理(示例)
             if code == 0:
-                video_view_lists.clear()
                 print("请求成功")
             else:
                 print(f"请求失败,错误码: {code}, 消息: {msg}")
@@ -153,11 +148,14 @@ class ZFQZTJLRecommend(object):
                     message="抓取单条视频失败,请求失败"
                     ),
                 return
+            video_view_lists = []
             for index, video_obj in enumerate(response['data']['data'], 1):
                 try:
                     self.aliyun_log.logging(
                         code="1001", message="扫描到一条视频", data=video_obj
                     )
+                    vid = video_obj['id']
+                    video_view_lists.append(str(vid))
                     self.process_video_obj(video_obj,title_rule)
                 except Exception as e:
                     self.aliyun_log.logging(
@@ -170,6 +168,7 @@ class ZFQZTJLRecommend(object):
                     return
                 # time.sleep(random.randint(5, 10))
                 time.sleep(random.randint(5, 30))
+            video_history(video_view_lists)
 
     def process_video_obj(self, video_obj, title_rule):
         """
@@ -255,15 +254,10 @@ class ZFQZTJLRecommend(object):
             self.mq.send_msg(mq_obj)
             self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
             video_view(vid, mid)
-            video_view_count += 1
-            video_view_lists.append(str(vid))
-            if video_view_count % 4 == 0:
-                video_history()
             self.aliyun_log.logging(code="1010", message="触发曝光", data=mq_obj)
             if self.download_cnt >= int(
                     self.rule_dict.get("videos_cnt", {}).get("min", 200)
             ):
-                video_history()
                 self.limit_flag = True
 
     def run(self):
@@ -276,6 +270,7 @@ if __name__ == '__main__':
         mode="recommend",
         rule_dict={},
         user_list=[{"uid": 75590470, "link": "zfqz推荐流_接口1", "nick_name": "做你的尾巴"}, {"uid": 75590471, "link": "zfqz推荐流_接口2", "nick_name": "能够相遇"}, {"uid": 75590472, "link": "zfqz推荐流_接口3", "nick_name": "一别两宽各生欢喜"}, {"uid": 75590473, "link": "zfqz推荐流_接口4", "nick_name": "惹火"}, {"uid": 75590475, "link": "zfqz推荐流_接口5", "nick_name": "顾九"}, {"uid": 75590476, "link": "zfqz推荐流_接口6", "nick_name": "宠一身脾气惯一身毛病"}],
+
     )
     J.get_recommend_list()
     # J.logic()