288 lines
8.6 KiB
JavaScript
288 lines
8.6 KiB
JavaScript
var express = require('express');
|
||
var router = express.Router();
|
||
const config = require("../config");
|
||
const GraphqlRequest = require("../http");
|
||
var request = require('request');
|
||
var axios = require('axios');
|
||
var fs = require('fs');
|
||
|
||
/**
|
||
* 对视频进行点赞
|
||
*/
|
||
|
||
router.get('/likeVideo', async function (req, res, next) {
|
||
let principalId = req.query.principalId;
|
||
let photoId = req.query.photoId;
|
||
let cancel = req.query.cancel;
|
||
let data = (await (new GraphqlRequest("kuaishou")).post(
|
||
config.likeVideo(principalId, photoId, cancel))
|
||
);
|
||
|
||
console.log("data: ", data);
|
||
// res.json({
|
||
// pcursor,
|
||
// list
|
||
// });
|
||
});
|
||
|
||
/**
|
||
* 获取主播首页的视频
|
||
* http://127.0.0.1:8090/api/publicFeeds?principalId=3xfjgnm9f89bave&count=200
|
||
*/
|
||
router.get('/publicFeeds', async function (req, res, next) {
|
||
let principalId = req.query.principalId;
|
||
let page = req.query.pcursor || "";
|
||
let count = req.query.count || "";
|
||
let { list, pcursor } = (await (new GraphqlRequest("kuaishou")).post(config.publicFeeds(principalId, page, count))).publicFeeds;
|
||
|
||
mkDir(principalId);
|
||
|
||
let imageNums = 0;
|
||
list.forEach(async (val, index) => {
|
||
console.log("val: ", val);
|
||
// if (val.useVideoPlayer == false) {
|
||
// imageNums += val.imgUrls.length
|
||
// console.log(val.id);
|
||
// DownImage(val)
|
||
// } else {
|
||
// getVideoData()
|
||
// }
|
||
})
|
||
|
||
res.json({
|
||
pcursor,
|
||
list
|
||
});
|
||
});
|
||
|
||
|
||
/**
|
||
* 获取视频真实播放地址
|
||
*/
|
||
router.get('/play', async function (req, res, next) {
|
||
let principalId = req.query.principalId;
|
||
let photoId = req.query.photoId;
|
||
res.json((await (new GraphqlRequest()).post(config.feedById(principalId, photoId))).feedById.currentWork);
|
||
});
|
||
|
||
/**
|
||
* 获取视频相关的推荐
|
||
* http://127.0.0.1:8090/api/RecommendFeeds?photoId=3xhzfvheg3wpsra
|
||
*/
|
||
router.get('/RecommendFeeds', async function (req, res, next) {
|
||
let photoId = req.query.photoId;
|
||
let result = await (new GraphqlRequest()).post(config.RecommendFeeds(photoId))
|
||
res.json(result.videoRecommendFeeds);
|
||
});
|
||
|
||
/**
|
||
* 获取评论
|
||
* http://127.0.0.1:8090/api/CommentList?photoId=3xz5ue2p6betigm
|
||
*/
|
||
router.get('/CommentList', async function (req, res, next) {
|
||
let photoId = req.query.photoId;
|
||
let pcursor = req.query.pcursor || "";
|
||
res.json((await (new GraphqlRequest()).post(config.shortVideoCommentList(photoId, pcursor))).shortVideoCommentList);
|
||
});
|
||
|
||
/**
|
||
* 获取评论展开
|
||
* http://127.0.0.1:8090/api/subCommentList?photoId=3xz5ue2p6betigm&rootCommentId=225526137195
|
||
*/
|
||
router.get('/subCommentList', async function (req, res, next) {
|
||
let photoId = req.query.photoId;
|
||
let rootCommentId = req.query.rootCommentId;
|
||
let pcursor = req.query.pcursor || "";
|
||
let ddd = (await (new GraphqlRequest()).post(config.subCommentList(photoId, rootCommentId, pcursor))).subCommentList
|
||
res.json(ddd);
|
||
});
|
||
|
||
|
||
/**
|
||
* 快手官网,获取无水印视频!!
|
||
* http://127.0.0.1:8090/api/likeList
|
||
*/
|
||
router.get('/likeList', async function (req, res, next) {
|
||
let pcursor = req.query.pcursor || "";
|
||
let rea = await (new GraphqlRequest("kuaishou")).post(config.visionProfileLikePhotoList, {
|
||
page: "profile",
|
||
pcursor: pcursor
|
||
});
|
||
res.json(rea.visionProfileLikePhotoList);
|
||
});
|
||
|
||
|
||
/**
|
||
* 获取收藏的视频
|
||
* http://127.0.0.1:8090/api/list
|
||
*/
|
||
router.get('/list', async function (req, res, next) {
|
||
let page = req.query.page || "";
|
||
res.json({
|
||
data: (await (new GraphqlRequest("kuaishou")).post(config.likedFeeds(page))).likedFeeds
|
||
});
|
||
});
|
||
|
||
/**
|
||
* 不是接口,主要作用是为了循环下载主播主页的视频和图片到本地
|
||
* 用的新接口,下载的是无水印的视频
|
||
* http://127.0.0.1:8090/api/down?userId=3xfjgnm9f89bave
|
||
*/
|
||
router.get('/down', async function (req, res, next) {
|
||
let userId = req.query.userId;
|
||
let pcursors = req.query.pcursor || '';
|
||
|
||
|
||
// https://live.kuaishou.com/live_api/profile/public?count=157&pcursor=&principalId=3xfjgnm9f89bave&hasMore=true
|
||
let result = await axios.get(config.visionProfilePhotoList, {
|
||
params: {
|
||
count: 220,
|
||
pcursor: pcursors,
|
||
principalId: userId,
|
||
hasMore: true
|
||
},
|
||
headers: {
|
||
"Cookie": `did=web_45504037cde380da3c57769b2726e6f2`
|
||
}
|
||
})
|
||
|
||
let list = result.data.data.list;
|
||
// 创建文件夹
|
||
mkDir(userId)
|
||
|
||
list.forEach(async (val, index) => {
|
||
// 如果遇到 图片
|
||
if (val.workType == 'vertical' || val.workType == 'multiple' || val.workType == 'single') {
|
||
DownImage(val)
|
||
} else {
|
||
console.log(val.id);
|
||
DownMp4(val.playUrl,val.id, userId)
|
||
}
|
||
});
|
||
// res.json(list);
|
||
});
|
||
|
||
/**
|
||
* 发送get请求获取视频流数据
|
||
* @param {*} url 视频地址
|
||
* @param {*} encoding 获取的类型
|
||
* @param {*} filename 文件名
|
||
* @returns
|
||
*/
|
||
const getVideoData = (url, encoding, filename) => {
|
||
return new Promise((resolve, reject) => {
|
||
let req = https.get(url, function (res) {
|
||
let result = ''
|
||
encoding && res.setEncoding(encoding)
|
||
res.on('data', function (d) {
|
||
result += d
|
||
})
|
||
res.on('end', function () {
|
||
savefileToPath(result, filename)
|
||
})
|
||
res.on('error', function (e) {
|
||
console.log("1 res.on 出错了:", e);
|
||
console.log("重新请求数据中:", url);
|
||
getVideoData(url, encoding, filename)
|
||
// reject(e)
|
||
})
|
||
})
|
||
req.end()
|
||
})
|
||
}
|
||
|
||
|
||
/**
|
||
* 保存视频到本地
|
||
* @param {*} fileData 视频流数据
|
||
* @param {*} fileName 文件名
|
||
* @returns
|
||
*/
|
||
const savefileToPath = async (fileData, fileName) => {
|
||
let fileFullName = `dist/${fileName}.mp4`;
|
||
return new Promise((resolve, reject) => {
|
||
fs.writeFile(fileFullName, fileData, 'binary', function (err) {
|
||
if (err) {
|
||
console.log('savefileToPath error:', err)
|
||
} else {
|
||
TotalCount += 1;
|
||
console.log(`第 ${TotalCount} 条视频:${fileName}.mp4 已下载!`);
|
||
Screenshot(fileName)
|
||
if (TotalCount >= TotalFeeds.length) {
|
||
console.log("");
|
||
console.log(`已下载:${TotalFeeds.length} 条视频,20秒后将开始继续下载..`);
|
||
console.log("结束 ==============================================================");
|
||
setTimeout(() => {
|
||
main();
|
||
}, 10000)
|
||
}
|
||
resolve('已下载')
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
// /**
|
||
// *
|
||
// * @param {*} url 视频下载地址
|
||
// * @param {*} id 视频文件名
|
||
// * @param {*} page 用主播id作为文件夹名称
|
||
// */
|
||
// function DownMp4(url, id, page) {
|
||
// var options = {
|
||
// url: url,
|
||
// headers: {
|
||
// 'Upgrade-Insecure-Requests': '1',
|
||
// 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
|
||
// "Cookie": `did=web_45504037cde380da3c57769b2726e6f2`
|
||
// }
|
||
// };
|
||
|
||
// //发送get请求下载视频
|
||
// request(options)
|
||
// .on('error', function (err) {
|
||
// console.log(err)
|
||
// })
|
||
// // 将视频写入到mp4文件夹里
|
||
// .pipe(fs.createWriteStream(`mp4/${page}/${id}.mp4`))
|
||
// }
|
||
|
||
function DownImage(data) {
|
||
let promise = Promise.resolve();
|
||
data.imgUrls.forEach((val, ix) => {
|
||
promise = promise.then(() => {
|
||
return new Promise(resolve => {
|
||
setTimeout(async () => {
|
||
let index = val.indexOf("atlas/") + 6
|
||
var options = {
|
||
url: val,
|
||
headers: { 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36' }
|
||
};
|
||
//发送get请求下载视频
|
||
request(options)
|
||
.on('error', function (err) {
|
||
console.log(err)
|
||
})
|
||
// 将视频写入到mp4文件夹里
|
||
.pipe(fs.createWriteStream(`mp4/${data.author.id}/${val.substring(index)}`))
|
||
|
||
resolve('成功');
|
||
|
||
}, 1)
|
||
})
|
||
.then(val => {
|
||
})
|
||
})
|
||
})
|
||
|
||
}
|
||
|
||
/**
|
||
* 创建文件夹
|
||
* @param {*} page
|
||
*/
|
||
function mkDir(page) {
|
||
fs.mkdir(`mp4/${page}`, 0777, err => err ? console.log(err) : console.log('文件夹创建成功'))
|
||
}
|
||
|
||
module.exports = router; |