ks_pc_keyword.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import time
  2. import requests
  3. import json
  4. from common import Common, AliyunLogger, Feishu, Material
  5. from common.sql_help import sqlCollect
  6. from common.userAgent import get_random_user_agent
  7. class KsPcKeyword:
  8. @classmethod
  9. def get_key_word(cls, keyword, task_mark, mark, channel_id, name, task):
  10. list = []
  11. url = "https://www.kuaishou.com/graphql"
  12. payload = json.dumps({
  13. "operationName": "visionSearchPhoto",
  14. "variables": {
  15. "keyword": keyword,
  16. "pcursor": "",
  17. "page": "search"
  18. },
  19. "query": "fragment photoContent on PhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n musicBlocked\n riskTagContent\n riskTagUrl\n}\n\nfragment recoPhotoFragment on recoPhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n musicBlocked\n riskTagContent\n riskTagUrl\n}\n\nfragment feedContent on Feed {\n type\n author {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n }\n photo {\n ...photoContent\n ...recoPhotoFragment\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n tags {\n type\n name\n __typename\n }\n __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n ...feedContent\n __typename\n }\n searchSessionId\n pcursor\n aladdinBanner {\n imgUrl\n link\n __typename\n }\n __typename\n }\n}\n"
  20. })
  21. cookie = Material.get_cookie_data("KsoMsyP2ghleM9tzBfmcEEXBnXg", "U1gySe", "快手搜索-cookie")
  22. headers = {
  23. 'Accept-Language': 'zh-CN,zh;q=0.9',
  24. 'Cache-Control': 'no-cache',
  25. 'Connection': 'keep-alive',
  26. 'Origin': 'https://www.kuaishou.com',
  27. 'Pragma': 'no-cache',
  28. 'User-Agent': get_random_user_agent("pc"),
  29. 'accept': '*/*',
  30. 'content-type': 'application/json',
  31. 'Cookie': cookie
  32. }
  33. try:
  34. time.sleep(3)
  35. # 代理信息
  36. proxy = "http://spkbt3wnzw:cx6R=v5mQuBgqsQ4o7@cn.visitxiangtan.com:30000"
  37. proxies = {
  38. "http": proxy,
  39. "https": proxy
  40. }
  41. response = requests.request("POST", url, headers=headers, data=payload, proxies=proxies)
  42. text = response.text
  43. if text:
  44. response_dict = json.loads(text)
  45. result = response_dict.get('result', None)
  46. if result:
  47. log_type = ['liukunyu', 'wangxueke', 'xinxin']
  48. mark_name = ['刘坤宇', '王雪珂', '信欣']
  49. Feishu.bot(log_type, '快手关键词搜索', f'快手关键词搜索cookie过期,请及时更换', mark_name)
  50. time.sleep(10)
  51. return list
  52. response = response.json()
  53. data_list = response['data']['visionSearchPhoto']['feeds']
  54. for data in data_list:
  55. data = data['photo']
  56. photo_id = data["id"]
  57. status = sqlCollect.is_used(task_mark, photo_id, mark, channel_id)
  58. view_count = data["viewCount"] if "viewCount" in data and data["viewCount"] else 0
  59. like_count = data["likeCount"] if "likeCount" in data and data["likeCount"] else 0
  60. like_count = cls.convert_to_number(like_count)
  61. video_percent = '%.4f' % (int(like_count) / int(view_count))
  62. special = 0.015
  63. old_title = data["caption"] # 标题
  64. duration = data["duration"]
  65. duration = int(duration) / 1000
  66. video_url = data["photoUrl"]
  67. image_url = data["coverUrl"]
  68. log_data = f"user:{keyword},,video_id:{photo_id},,video_url:{video_url},original_title:{old_title},,like_count:{like_count},,view_count:{view_count},,duration:{duration}"
  69. AliyunLogger.logging(channel_id, name, keyword, photo_id, "扫描到一条视频", "2001", log_data)
  70. if status:
  71. AliyunLogger.logging(channel_id, name, keyword, photo_id, "该视频已改造过", "2001", log_data)
  72. continue
  73. if int(view_count) < 1000:
  74. AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:浏览小于1000", "2003", log_data)
  75. Common.logger("ks-key-word").info(
  76. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,浏览:{view_count},浏览{view_count} ,时长:{int(duration)} ")
  77. continue
  78. if float(video_percent) < special:
  79. AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:点赞/浏览{special}", "2003", log_data)
  80. Common.logger("ks-key-word").info(
  81. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,浏览:{view_count},浏览{view_count} ,时长:{int(duration)} ")
  82. continue
  83. if int(duration) < 30 or int(duration) > 600:
  84. AliyunLogger.logging(channel_id, name, keyword, photo_id,
  85. f"不符合规则:时长不符合规则大于600秒/小于30秒", "2003",
  86. log_data)
  87. Common.logger("ks-key-word").info(
  88. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,浏览:{view_count},浏览{view_count} ,时长:{int(duration)} ")
  89. continue
  90. AliyunLogger.logging(channel_id, name, keyword, photo_id, "符合规则等待改造", "2004", log_data)
  91. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  92. "rule": '',
  93. "old_title": old_title}
  94. list.append(all_data)
  95. return list
  96. except Exception as exc:
  97. Common.logger("ks-key-word").info(f"快手搜索词{keyword}获取失败{exc}\n")
  98. return list
  99. @classmethod
  100. def convert_to_number(cls, value):
  101. if value.endswith("万"):
  102. return float(value[:-1]) * 10000 # 去掉“万”并乘以 10000
  103. return int(value) # 处理其他格式
  104. if __name__ == '__main__':
  105. keyword = '毛主席故居'
  106. task_mark = '1'
  107. mark = 'pl-gjc'
  108. channel_id = '快手搜索'
  109. name = '1'
  110. task = {'combo': ['最新发布', '近1日', '1分钟内']}
  111. KsPcKeyword.get_key_word(keyword, task_mark, mark, channel_id, name, task)