153 lines
4.8 KiB
JavaScript
153 lines
4.8 KiB
JavaScript
var express = require('express');
|
||
var router = express.Router();
|
||
const config = require("../config");
|
||
const GraphqlRequest = require("../http");
|
||
var request = require('request');
|
||
var fs = require('fs');
|
||
|
||
router.get('/play', async function(req, res, next) {
|
||
let principalId = req.query.principalId;
|
||
let photoId = req.query.photoId;
|
||
res.json((await GraphqlRequest.post(config.feedById(principalId,photoId))).feedById.currentWork);
|
||
});
|
||
|
||
/**
|
||
* 获取视频相关的推荐
|
||
* http://127.0.0.1:3000/api/RecommendFeeds?photoId=3xucud23iv78p76
|
||
*/
|
||
router.get('/RecommendFeeds', async function(req, res, next) {
|
||
let photoId = req.query.photoId;
|
||
let result = await GraphqlRequest.post(config.RecommendFeeds(photoId))
|
||
res.json(result.videoRecommendFeeds);
|
||
});
|
||
|
||
/**
|
||
* 获取评论
|
||
* http://127.0.0.1:3000/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 GraphqlRequest.post(config.shortVideoCommentList(photoId,pcursor))).shortVideoCommentList);
|
||
});
|
||
|
||
/**
|
||
* 获取评论展开
|
||
* http://127.0.0.1:3000/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 GraphqlRequest.post(config.subCommentList(photoId, rootCommentId,pcursor))).subCommentList
|
||
res.json(ddd);
|
||
});
|
||
|
||
/**
|
||
* 获取收藏的视频
|
||
* http://127.0.0.1:3000/api/list
|
||
*/
|
||
router.get('/list', async function(req, res, next) {
|
||
let page = req.query.page || "";
|
||
res.json({
|
||
data: (await GraphqlRequest.post(config.likedFeeds(page))).likedFeeds
|
||
});
|
||
});
|
||
|
||
/**
|
||
* 下载视频
|
||
*/
|
||
router.get('/down', async function(req, res, next) {
|
||
let principalId = req.query.principalId;
|
||
let pcursors = req.query.pcursor || '';
|
||
let count = req.query.count
|
||
let result = await GraphqlRequest.post(config.publicFeeds(principalId, pcursors, count))
|
||
let { list , pcursor } = result.publicFeeds;
|
||
|
||
// 创建文件夹
|
||
mkDir(principalId)
|
||
|
||
list.forEach(async (val,index) => {
|
||
// 如果遇到 vertical 图片
|
||
if (val.workType == 'vertical' || val.workType == 'multiple' || val.workType == 'single') {
|
||
DownImage(val)
|
||
// val.imgUrls.forEach((vas,ix) => {
|
||
// DownImage(vas,vas.match(/atlas\/([\s\S]*?).webp/)[1],val.user.id)
|
||
// })
|
||
} else{
|
||
let VideoUrl = await GraphqlRequest.post(config.feedById(val.user.id, val.id))
|
||
console.log(`---------------------- ${index+1}: 请求视频: ${val.id} 的下载地址 : `, VideoUrl);
|
||
DownMp4(VideoUrl.feedById.currentWork.playUrl, val.id, val.user.id)
|
||
}
|
||
});
|
||
res.json({
|
||
data: result.publicFeeds
|
||
});
|
||
});
|
||
|
||
|
||
/**
|
||
*
|
||
* @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'
|
||
}
|
||
};
|
||
|
||
//发送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) => {
|
||
|
||
console.log("图片地址 ------------- :", val)
|
||
promise = promise.then(() => {
|
||
return new Promise(resolve => {
|
||
setTimeout(async () => {
|
||
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.user.id}/${val.match(/atlas\/([\s\S]*?).webp/)[1]}.webp`))
|
||
|
||
resolve('成功');
|
||
|
||
},1)
|
||
}).then(val => {
|
||
console.log("Promise val: ", val);
|
||
|
||
})
|
||
})
|
||
})
|
||
|
||
}
|
||
|
||
/**
|
||
* 创建文件夹
|
||
* @param {*} page
|
||
*/
|
||
function mkDir(page){
|
||
fs.mkdir(`mp4/${page}`, 0777, err => err ? console.log(err) : console.log('文件夹创建成功'))
|
||
}
|
||
|
||
module.exports = router; |