poke/src/fetcher.js

64 lines
2 KiB
JavaScript
Raw Normal View History

2022-03-02 21:05:55 +01:00
/*
2022-03-02 22:00:46 +01:00
2022-05-17 22:19:35 +02:00
Copyright (C) 2021-2022 POKETUBE (https://github.com/iamashley0/poketube)
2022-03-02 21:05:55 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
*/
const fetch = require("node-fetch"); //2.5.x
2022-05-17 22:19:35 +02:00
const { toJson } = require("xml2json")
2022-03-02 21:05:55 +01:00
var youtube_url = `https://www.youtube.com/watch?v=`;
var dislike_api = `https://returnyoutubedislikeapi.com/votes?videoId=`
2022-03-06 14:53:46 +01:00
var new_api_url = `https://lighttube.herokuapp.com/api/player`
2022-03-02 21:05:55 +01:00
module.exports = async function(video_id){
2022-03-06 14:53:46 +01:00
const dislike = await fetch(`${dislike_api}${video_id}`).then((res) => res.json());
2022-05-17 22:19:35 +02:00
const dislikes = dislike.dislikes || "none"
/*
* Parses and fetches an xml
*/
async function parsexml(id){
const player = await fetch(`${new_api_url}?v=${id}`)
var h = await player.text()
var j = toJson(h);
2022-03-06 14:53:46 +01:00
return JSON.parse(j);
}
2022-03-02 21:05:55 +01:00
/*
2022-03-02 22:00:46 +01:00
* Returner object
2022-03-02 21:05:55 +01:00
*/
const returner = {
2022-03-06 14:53:46 +01:00
video:await parsexml(video_id),
engagement:dislike,
video_url_youtube:`${youtube_url}${video_id}`
2022-03-02 21:05:55 +01:00
}
2022-03-06 14:53:46 +01:00
return returner
}
module.exports.searcher = async function searcher(query,res){
const search = await fetch(`https://lighttube.herokuapp.com/api/search?query=${query}`)
const text = await search.text()
2022-05-17 22:19:35 +02:00
const j = JSON.parse(toJson(text));
if(query.length > 2) {
2022-03-06 14:53:46 +01:00
for (item of j.Search.Results.Video) {
const videoid = item.id;
return res.redirect(`/watch?v=${videoid}`);
}
2022-05-17 22:19:35 +02:00
if(query.length < 2){
res.redirect("/")
}
}
2022-03-06 14:53:46 +01:00
}