123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import random
- import time
- import requests
- import json
- from common import 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.192.46:8889/crawler/kuai_shou/keyword"
- list = []
- payload = json.dumps({
- "keyword": keyword,
- "content_type": content_type,
- "sort_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:
- time.sleep(3)
- response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
- response = response.json()
- code = response['code']
- if code != 0:
- if code == 27006 and response['msg'] == '快手内容已被删除或无法访问':
- Feishu.finish_bot(f"kuai_shou/keyword {response['msg']},cookie 过期需要更换",
- "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb",
- "【快手搜索接口使用提示】")
- return list
- return list
- data_list = response['data']['data']
- for data in data_list:
- type = int(data['type'])
- if type != 1:
- continue
- photo_type = data['photoType']
- if photo_type != "VIDEO":
- continue
- photo_id = data['photoId']
- status = sqlCollect.is_used(task_mark, photo_id, mark, channel_id)
- image_url = data['webpCoverUrls'][0]['url']
- video_url = data['mainMvUrls'][0]['url']
- view_count = data.get('viewCount', 0)
- share_count = data.get('shareCount', 0)
- old_title = data['caption'] # 标题
- video_percent = '%.4f' % (int(share_count) / int(view_count))
- duration = int(int(data["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, "该视频已改造过", "2002", log_data)
- continue
- if float(video_percent) < special:
- AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享/浏览{special}", "2003", log_data)
- continue
- if int(share_count) < share_count_rule:
- AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data)
- continue
- if int(duration) < short_duration_rule or int(duration) > 720:
- AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:时长不符合规则大于720秒/小于{short_duration_rule}", "2003",
- log_data)
- continue
- 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}"
- all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
- "rule": video_percent,
- "old_title": old_title}
- list.append(all_data)
- AliyunLogger.logging(channel_id, name, keyword, photo_id, "符合规则等待改造", "2004", log_data)
- return list
- except Exception as exc:
- return list
- @classmethod
- def get_video(cls, video_id):
- url = "http://8.217.192.46:8889/crawler/kuai_shou/detail"
- payload = json.dumps({
- "content_id": str(video_id)
- })
- headers = {
- 'Content-Type': 'application/json'
- }
- time.sleep(random.uniform(1, 10))
- response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
- 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)
|