Files
ClassContent/历届学生/front-end-team/项目练习/学生项目/陆旗/爬虫/index.js
2024-09-27 02:06:13 +08:00

105 lines
3.2 KiB
JavaScript

const express = require('express')
const { GraphQLClient, gql } = require("graphql-request");
const request = require('request');
const fs = require('fs');
const path = require('path');
const app = express()
const port = 3000
const url = "https://www.kuaishou.com/graphql";
var pcursor = "";
var userId = "";
var feeds = [];
var index = 0;
const client = new GraphQLClient(url)
app.get('/down', (req, res) => {
userId = req.query.id;
getFeeds()
res.end('Hello World!')
});
async function getFeeds() {
const query = gql`
query visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {
visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page,webPageArea: $webPageArea) {
result,llsid,webPageArea,feeds {
type,author {
id,name,following,headerUrl,headerUrls {
cdn,url
}
},
tags {
type,name
},
photo {
id,duration,caption,likeCount,realLikeCount,coverUrl,coverUrls {
cdn,url
},
photoUrls {
cdn,url
},
photoUrl,liked,timestamp,expTag,animatedCoverUrl,stereoType,videoRatio,profileUserTopPhoto
},
canAddComment,currentPcursor,llsid,status
},
hostName,pcursor
}
}
`
const variables = {
pcursor: pcursor,
userId: userId,
page: 'profile'
};
const requestHeaders = {
Cookie: 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_cf504812c13b0458cbdcc5bc9ea39691; client_key=65890b29; userId=58344290; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABq_TLWokR1HCFOInz6xuXy_kvuyQzzFhSSHR6_VuGW5G8r7X0I7jaXdIsvNLtzMihRfZy3IC6ZuErdyrv9JpRcK-I1orA8PsLE5yjof_RUiU85_sdhL_6PHbQY-gBM9-5SGcguwtpmc6gGpP6ztlEeucgq4S3KC668-TGkcoNATOo_JmyYuf1jvjxaHtwe1dznn-qQYr1ypIHiwzl00MKRBoSnIqSq99L0mk4jolsseGdcwiNIiC82HgSYpfCs7fFoo9D2LxFDjCTkWKnva615o7eqi8GgSgFMAE; kuaishou.server.web_ph=ccabd46f69a02c96deaa4092dc0d1595c15e',
Host: "www.kuaishou.com",
Origin: "https://www.kuaishou.com",
Referer: "https://www.kuaishou.com/profile/3x3f8rfq39drzey",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
}
const { visionProfilePhotoList } = await client.request(query, variables, requestHeaders)
pcursor = visionProfilePhotoList.pcursor;
feeds = visionProfilePhotoList.feeds;
downVideo()
}
function downVideo() {
if (!fs.existsSync(`mp4/${userId}`)) {
fs.mkdirSync(path.join(__dirname, `/mp4/${userId}`), 0o777)
}
request(feeds[index].photo.photoUrl).pipe(fs.createWriteStream(path.join(__dirname, `/mp4/${userId}/${feeds[index].photo.id}.mp4`)))
index += 1;
if (index < feeds.length) {
downVideo()
} else {
index = 0;
feeds = [];
if (pcursor != "no_more") {
getFeeds();
}
}
}
app.listen(port, () => {
console.log(`you app is run http://127.0.0.1:${port}`);
});