zhangyong 7 tháng trước cách đây
mục cha
commit
6ca92ee05f
1 tập tin đã thay đổi với 138 bổ sung0 xóa
  1. 138 0
      data_channel/ks_keyword.py

+ 138 - 0
data_channel/ks_keyword.py

@@ -0,0 +1,138 @@
+import requests
+import json
+
+from common import Common, AliyunLogger, Feishu
+from common.sql_help import sqlCollect
+
+
+class KsKeyword:
+    @classmethod
+    def get_key_word(cls, keyword, task_mark, mark, channel_id, name, task):
+        combo = task['combo']
+        content_type = combo[0]
+        publish_time = combo[1]
+        duration = combo[2]
+        share_count_rule = 0
+        special = 0
+        short_duration_rule = 0
+
+        url = "http://8.217.190.241:8888/crawler/kuai_shou/keyword"
+        list = []
+
+        payload = json.dumps({
+            "keyword": keyword,
+            "content_type": "综合",
+            "sort_type": content_type,
+            "publish_time": publish_time,
+            "duration": duration,
+            "cursor": ""
+        })
+        headers = {
+            'Content-Type': 'application/json'
+        }
+
+        if " 不限" == publish_time:
+            share_count_rule = 100
+            special = 0.0005
+            short_duration_rule = 25
+        elif "近1日" == publish_time:
+            share_count_rule = 0
+            special = 0.0003
+            short_duration_rule = 25
+        elif "近7日" == publish_time:
+            share_count_rule = 50
+            special = 0.0005
+            short_duration_rule = 25
+        elif "近1月" == publish_time:
+            share_count_rule = 100
+            special = 0.0005
+            short_duration_rule = 25
+        try:
+            response = requests.request("POST", url, headers=headers, data=payload)
+            response = response.json()
+            code = response['code']
+            if code != 0:
+                if code == 27006 and response['msg'] == '快手内容已被删除或无法访问':
+                    keyword_principal = task["keyword_name"]
+                    Feishu.bot(keyword_principal, '机器自动改造消息通知', f'品类关键词搜索-快手搜索,无法搜索出内容,请及时更换搜索词,搜索词为:{keyword}', keyword_principal)
+                    Common.logger("ks-key-word").info(f"快手搜索词数据获取失败,{response['msg']}\n")
+                    return list
+                Common.logger("ks-key-word").info(f"快手搜索词数据获取失败,接口为kuai_shou/keyword\n")
+                return list
+            data_list = response['data']['data']
+            for data in data_list:
+                photo_id = data["photo_id"]
+                status = sqlCollect.is_used(task_mark, photo_id, mark, "快手历史")
+
+                view_count = data["view_count"]
+                share_count = data["share_count"]
+                old_title = data["caption"]  # 标题
+
+                video_percent = '%.4f' % (int(share_count) / (view_count))
+                duration = data["duration"]
+                duration = int(duration) / 1000
+                log_data = f"user:{keyword},,video_id:{photo_id},,video_url:'',original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
+
+                AliyunLogger.logging(channel_id, name, keyword, photo_id, "扫描到一条视频", "2001", log_data)
+                if status:
+                    AliyunLogger.logging(channel_id, name, keyword, photo_id, "该视频已改造过", "2001", log_data)
+                    continue
+                if float(video_percent) < special:
+                    AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享/浏览{special}", "2003", log_data)
+
+                    Common.logger("ks-key-word").info(
+                        f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
+                    continue
+                if int(share_count) < share_count_rule:
+                    AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data)
+
+                    Common.logger("ks-key-word").info(
+                        f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
+                    continue
+                if int(duration) < short_duration_rule or (duration) > 600:
+                    AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:时长不符合规则大于600秒/小于{short_duration_rule}", "2003",
+                                         log_data)
+
+                    Common.logger("ks-key-word").info(
+                        f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
+                    continue
+                video_url, image_url = cls.get_video(photo_id)
+                if video_url:
+                    log_data = f"user:{keyword},,video_id:{photo_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
+                    AliyunLogger.logging(channel_id, name, keyword, photo_id, "符合规则等待改造", "2004", log_data)
+                    all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
+                                "rule": video_percent,
+                                "old_title": old_title}
+                    list.append(all_data)
+            return list
+        except Exception as exc:
+            Common.logger("ks-key-word").info(f"快手搜索词{keyword}获取失败{exc}\n")
+            return list
+
+    @classmethod
+    def get_video(cls, video_id):
+        url = "http://8.217.190.241:8888/crawler/kuai_shou/detail"
+
+        payload = json.dumps({
+            "content_id": str(video_id)
+        })
+        headers = {
+            'Content-Type': 'application/json'
+        }
+
+        response = requests.request("POST", url, headers=headers, data=payload)
+        response = response.json()
+        data = response["data"]["data"]
+        video_url = data["video_url_list"][0]["video_url"]
+        image_url = data["image_url_list"][0]["image_url"]
+        return video_url, image_url
+
+
+if __name__ == '__main__':
+    keyword = '毛主席故居'
+    task_mark = '1'
+    mark = 'pl-gjc'
+    channel_id = '快手搜索'
+    name = '1'
+    task = {'combo': ['最新发布', '近1日', '1分钟内']}
+    KsKeyword.get_key_word(keyword, task_mark, mark, channel_id, name, task)