import requests
import re
import os
save_path = 'vedeo'
if not os.path.exists(save_path):
os.makedirs(save_path)
url = 'https://www.kuaishou.com/graphql'
header = {'Cookie':'kpf=PC_WEB; clientid=3; did=web_cbaa864b593c0bbcb9225bfd4d63d08c; userId=2008789978; kuaishou.server.webday7_st=ChprdWFpc2hvdS5zZXJ2ZXIud2ViZGF5Ny5zdBKwATuGG9RAmaFmmlTlhcbIpJali-rdNQBCEBJ2xrU_ECDj_h19SsuDcP-l0TBzE5tlB3V35jN0C3BZCQOqJG11NfGAuxxUieCZc-xICJVxSSnLTmYbCVTT1txT1ALIDEm_q8OQNzJcsKMa2FDrDKhTKSsoSJdQwExMEY3jbvgdtiDGZAEx5IJeN6LsQO4tUJMbpG2uhzhKIiG5ZINhISrYQCtgHm_WQRfmSJ6xqVGp4BbAGhLnL33oyPAVhFFV1o7h2Db3JhgiIGtRHf31wyLh-iFn-bNL-i-1dMhWJKGisSUyBCrIYEwMKAUwAQ; kuaishou.server.webday7_ph=83afb1335a53a6dfd6e666425b7566930195; kpn=KUAISHOU_VISION',
'Host':'www.kuaishou.com',
'Origin':'https://www.kuaishou.com',
'Referer':'https://www.kuaishou.com/short-video/3xvmhp59y989k2q?authorId=3xypjh4ujxmm5cy&streamSource=profile&area=profilexxnull',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36'}
data ={"operationName":"visionProfilePhotoList",
"variables":{"userId":"3xypjh4ujxmm5cy","pcursor":"","page":"detail","webPageArea":"profilexxnull"},
"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"}
response = requests.post(url=url,headers=header,json=data)
json_data = response.json()
feeds = json_data['data']['visionProfilePhotoList']['feeds']
index = 0
for i in feeds:
manifestH265 = i['photo']['photoUrl']
title = i['photo']['caption']
new_title = re.sub(r'[\\"<>:*|\n\r]','',title)
video_content = requests.get(url=manifestH265).content
try:
with open(f'vedeo/{new_title}_{index}.mp4', mode='wb') as f:
f.write(video_content)
index += 1
except Exception as e:
print(f"Error saving video: {e}")