kuaishou.py 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, Common
  8. from common.sql_help import sqlCollect
  9. from data_channel.data_help import dataHelp
  10. class KS:
  11. @classmethod
  12. def get_ks_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name):
  13. list = []
  14. pcursor = ""
  15. url = "https://www.kuaishou.com/graphql"
  16. for i in range(3):
  17. cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id)
  18. time.sleep(random.randint(5, 10))
  19. payload = json.dumps({
  20. "operationName": "visionProfilePhotoList",
  21. "variables": {
  22. "userId": url_id,
  23. "pcursor": pcursor,
  24. "page": "profile"
  25. },
  26. "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"
  27. })
  28. headers = {
  29. 'accept': '*/*',
  30. 'content-type': 'application/json',
  31. 'Origin': 'https://www.kuaishou.com',
  32. 'Cookie': cookie,
  33. 'Accept-Language': 'zh-CN,zh;q=0.9',
  34. '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',
  35. 'Referer': f'https://www.kuaishou.com/profile/{url_id}',
  36. 'Accept-Encoding': 'gzip, deflate, br',
  37. 'Connection': 'keep-alive'
  38. }
  39. urllib3.disable_warnings()
  40. s = requests.session()
  41. s.mount('http://', HTTPAdapter(max_retries=3))
  42. s.mount('https://', HTTPAdapter(max_retries=3))
  43. # response = requests.request("POST", url, headers=headers, data=payload, timeout=10)
  44. response = s.post(url=url, headers=headers, data=payload, verify=False, timeout=10)
  45. response.close()
  46. if response.status_code != 200:
  47. return list
  48. elif "visionProfilePhotoList" not in response.json()["data"]:
  49. Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie过期,请及时更换', name)
  50. time.sleep(900)
  51. continue
  52. elif "feeds" not in response.json()["data"]["visionProfilePhotoList"]:
  53. Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie过期,请及时更换', name)
  54. time.sleep(900)
  55. continue
  56. elif len(response.json()["data"]["visionProfilePhotoList"]["feeds"]) == 0:
  57. Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie使用频繁无法获取到数据,请及时更换', name)
  58. time.sleep(900)
  59. continue
  60. pcursor = response.json()['data']['visionProfilePhotoList']['pcursor']
  61. feeds = response.json()['data']['visionProfilePhotoList']['feeds']
  62. for i in range(len(feeds)):
  63. try:
  64. video_id = feeds[i].get("photo", {}).get("videoResource").get("h264", {}).get("videoId", "")
  65. except KeyError:
  66. video_id = feeds[i].get("photo", {}).get("videoResource").get("hevc", {}).get("videoId", "")
  67. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  68. if status:
  69. cover_url = feeds[i].get('photo', {}).get('coverUrl', "")
  70. video_url = feeds[i].get('photo', {}).get('photoUrl', "")
  71. duration = dataHelp.video_duration(video_url)
  72. if int(duration) >= 45:
  73. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url}
  74. list.append(all_data)
  75. if len(list) == int(number):
  76. Common.logger("log").info(f"获取快手视频总数:{len(list)}\n")
  77. return list
  78. return list