kuaishou.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import random
  2. import time
  3. import requests
  4. import json
  5. import urllib3
  6. from requests.adapters import HTTPAdapter
  7. from common import Feishu, Material, AliyunLogger
  8. from common.sql_help import sqlCollect
  9. from data_channel.data_help import dataHelp
  10. class KS:
  11. @classmethod
  12. def get_share_count(cls, v_id):
  13. url = "http://8.217.192.46:8889/crawler/kuai_shou/detail"
  14. payload = json.dumps({
  15. "content_id": v_id
  16. })
  17. headers = {
  18. 'Content-Type': 'application/json'
  19. }
  20. try:
  21. time.sleep(random.uniform(1, 10))
  22. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  23. response = response.json()
  24. if response["code"] == 0:
  25. data = response["data"]["data"]
  26. share_count = data.get("share_count")
  27. return int(share_count)
  28. except KeyError as e:
  29. return 0
  30. @classmethod
  31. def get_ks_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name):
  32. list = []
  33. pcursor = ""
  34. url = "https://www.kuaishou.com/graphql"
  35. if not url_id:
  36. return
  37. for i in range(3):
  38. cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id)
  39. # cookie = 'did=web_a94fee6e0b894eb38745ef812fdf314a; didv=1736341169000; kpf=PC_WEB; clientid=3; app_id=ks686235996059836592; expire_time=1800; kwpsecproductname=PCLive; bUserId=1000056684959; access_token=ChFvYXV0aC5hY2Nlc3NUb2tlbhIwCpNkDedyeTOnyF6fL2IlPENNi5bQdrPgDsV4INu70fwXCgBXmCHp3F04dQ4LCpiaGhI2peqRSTFPt7N70Y9cxFbAjFEiIDMdxlMuUHe6NJ5kM2-WyLdY6Jtsklm9PgyjnCxsEiH4KAUwAQ; nc_user_id=CiVhZC5ub3RpZnkuY2VudGVyLm9hdXRoLnVzZXIuaWQuc2VjcmV0EiCwCf1wvXTpU2nCbHei0fczAgAXnj+Pf9LbcvP2Gz/hqhoSjat9hhX1MrpXWGsErDhRE45PIiBqC9fPKmCLd6OML6dxKYkKz2AgLB0EGfSYATBVhZUoHCgFMAE=; userId=1403607127; kuaishou.server.webday7_st=ChprdWFpc2hvdS5zZXJ2ZXIud2ViZGF5Ny5zdBKwAd-0867UCfMUH6aOOSJxz97ZDBklvcirozWVw93XIXj01qT1pJ767jOWauwWyCm3r6kypbdQJ3Yx_77zOa7PJIBWqkYQgc1y2Fc-GePNHe-M0Wb9wdVskiRyMSssJ6p0ur_k2dAaP1c4ghPiVVPdEo0vcE9XtdroCOOUtyzmT1dZADP0tAszUTquRdMtgefwpgYiIfFbLpuvIhaoDRv0eiNnW9YKhET1-idMusFQCQgPGhI8tsmuBlpRuNU4fo_gMPQVxSUiIOYMGUScG3ahVgb8T1-W4yncYAY2g1gciR7rtM3Q8MkCKAUwAQ; kuaishou.server.webday7_ph=fb6c0744c6bc4d31a111feb76837b2d7a8dc; kpn=KUAISHOU_VISION'
  40. time.sleep(random.randint(1, 5))
  41. payload = json.dumps({
  42. "operationName": "visionProfilePhotoList",
  43. "variables": {
  44. "userId": url_id,
  45. "pcursor": pcursor,
  46. "page": "profile"
  47. },
  48. "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 visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {\n visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n ...feedContent\n __typename\n }\n hostName\n pcursor\n __typename\n }\n}\n"
  49. })
  50. headers = {
  51. 'accept': '*/*',
  52. 'content-type': 'application/json',
  53. 'Origin': 'https://www.kuaishou.com',
  54. 'Cookie': cookie,
  55. 'Accept-Language': 'zh-CN,zh;q=0.9',
  56. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
  57. 'Referer': f'https://www.kuaishou.com/profile/{url_id}',
  58. 'Accept-Encoding': 'gzip, deflate, br',
  59. 'Connection': 'keep-alive'
  60. }
  61. urllib3.disable_warnings()
  62. s = requests.session()
  63. s.mount('http://', HTTPAdapter(max_retries=3))
  64. s.mount('https://', HTTPAdapter(max_retries=3))
  65. # response = requests.request("POST", url, headers=headers, data=payload, timeout=10)
  66. try:
  67. response = s.post(url=url, headers=headers, data=payload, verify=False, timeout=10)
  68. response.close()
  69. if response.status_code != 200:
  70. return list
  71. elif response.status_code == 200 and "error_msg" in response.text:
  72. Feishu.bot("xinxin", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', 'xinxin')
  73. Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', '刘兆恒')
  74. time.sleep(600)
  75. return list
  76. elif "visionProfilePhotoList" not in response.json()["data"]:
  77. if name == '快手品类账号'or name == 'Top溯源账号':
  78. Feishu.bot("xinxin", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', 'xinxin')
  79. Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', '刘兆恒')
  80. time.sleep(600)
  81. else:
  82. Feishu.bot(mark, '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', name)
  83. return list
  84. elif "feeds" not in response.json()["data"]["visionProfilePhotoList"]:
  85. if name == '快手品类账号' or name == 'Top溯源账号':
  86. Feishu.bot("xinxin", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', 'xinxin')
  87. Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', '刘兆恒')
  88. time.sleep(600)
  89. else:
  90. Feishu.bot(mark, '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', name)
  91. return list
  92. elif len(response.json()["data"]["visionProfilePhotoList"]["feeds"]) == 0:
  93. if name == '快手品类账号' or name == 'Top溯源账号':
  94. Feishu.bot("xinxin", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', 'xinxin')
  95. Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', '刘兆恒')
  96. time.sleep(600)
  97. else:
  98. Feishu.bot(mark, '机器自动改造消息通知', f'快手-{name}cookie使用频繁无法获取到数据,请及时更换', name)
  99. return list
  100. pcursor = response.json()['data']['visionProfilePhotoList']['pcursor']
  101. feeds = response.json()['data']['visionProfilePhotoList']['feeds']
  102. for i in range(len(feeds)):
  103. # try:
  104. # video_id = feeds[i].get("photo", {}).get("videoResource").get("h264", {}).get("videoId", "")
  105. # except KeyError:
  106. # video_id = feeds[i].get("photo", {}).get("videoResource").get("hevc", {}).get("videoId", "")
  107. # status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  108. # if status:
  109. # continue
  110. video_id = feeds[i].get("photo", {}).get("id", "")
  111. day_count = Material.get_count_restrict(channel_id)
  112. if day_count:
  113. status = sqlCollect.is_used_days(video_id, mark, channel_id, day_count)
  114. else:
  115. status = sqlCollect.is_used(video_id, mark, channel_id)
  116. old_title = feeds[i].get("photo", {}).get("caption")
  117. cover_url = feeds[i].get('photo', {}).get('coverUrl', "")
  118. video_url = feeds[i].get('photo', {}).get('photoUrl', "")
  119. view_count = int(feeds[i].get('photo', {}).get('viewCount', 0))
  120. realLikeCount = int(feeds[i].get('photo', {}).get('realLikeCount', 0))
  121. duration = dataHelp.video_duration(video_url)
  122. log_data = f"user:{url_id},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,view_count:{view_count},,duration:{duration}"
  123. # log_data = f"user:{url_id},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,view_count:{view_count},,duration:{duration}"
  124. AliyunLogger.logging(channel_id, name, url_id, video_id, "扫描到一条视频", "2001", log_data)
  125. if status:
  126. AliyunLogger.logging(channel_id, name, url_id, video_id, "该视频已改造过", "2002", log_data)
  127. continue
  128. share_count = cls.get_share_count(video_id)
  129. video_percent = '%.4f' % (share_count / view_count)
  130. special = float(0.001)
  131. if float(video_percent) < special:
  132. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享/浏览小于0.001", "2003", log_data)
  133. continue
  134. if share_count < 500:
  135. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享小于500", "2003", log_data)
  136. continue
  137. if duration < 30 or duration > 720:
  138. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  139. continue
  140. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent, "old_title": old_title}
  141. list.append(all_data)
  142. AliyunLogger.logging(channel_id, name, url_id, video_id, "符合规则等待改造", "2004", log_data)
  143. if len(list) == int(number):
  144. return list
  145. except Exception as exc:
  146. # Feishu.bot("wangxueke", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', 'wangxueke')
  147. # Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'快手-{name}cookie过期,请及时更换', '刘兆恒')
  148. return list
  149. return list
  150. if __name__ == '__main__':
  151. KS.get_ks_url("1","3x3cjyikngeeuui",1,"1",'WuoQsVFXChVMK4tDHqLcwLWgnjh','Sjk8p8','','')