add lyrics and refactor code

adds lyrics to the page and refactors code
This commit is contained in:
Ashley 2022-02-13 14:01:27 +03:00 committed by GitHub
parent 982dd92247
commit 97e1c2094c

View file

@ -4,43 +4,49 @@ var express = require("express");
var app = express(); var app = express();
app.engine("html", require("ejs").renderFile); app.engine("html", require("ejs").renderFile);
app.set("view engine", "html"); app.set("view engine", "html");
const lyricsFinder = require("lyrics-finder");
const renderTemplate = async (res, req, template, data = {}) => { const renderTemplate = async (res, req, template, data = {}) => {
res.render( res.render(path.resolve(`${templateDir}${path.sep}${template}`), Object.assign(data)
path.resolve(`${templateDir}${path.sep}${template}`), );
Object.assign(data)
);
}; };
const ytSearch = require('youtube-search');
const search = require('youtube-search');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const search = require("youtube-search");
app.get("/watch", function(req, res) { app.get("/watch", function(req, res) {
var url = req.query.v; var url = req.query.v;
var uu = `https://www.youtube.com/watch?v=${url}`; var uu = `https://www.youtube.com/watch?v=${url}`;
var search = require("youtube-search");
var opts = { var opts = {
maxResults: 1, maxResults: 1,
key: process.env.yt key: process.env.yt
}; };
search(uu, opts, function(err, results) { search(uu, opts, async (err, results) => {
var i = results[0].id; if (err != undefined)
fetch(`https://yt-proxy-api.herokuapp.com/get_player_info?v=${i}`) return console.error(err);
.then(res => res.json()) if (results.length === 0) return;
.then(json => {
var video = results[0]; const video = results[0];
if (!video) return;
if (err) console.log(err); const json = await fetch(`https://yt-proxy-api.herokuapp.com/get_player_info?v=${video.id}`)
const tarih = json.upload_date .then((res) => res.json());
var h = json.formats[1].url;
renderTemplate(res, req, "youtube.ejs", { url: h, title: video,video:json,date:tarih }); const lyrics = await lyricsFinder(video.title);
});
if (lyrics == undefined) lyrics = "Lyrics not found";
renderTemplate(res, req, 'youtube.ejs', {
url: json.formats[1].url,
title: video,
video: json,
date: json.upload_date,
lyrics:lyrics.replace(/\n/g, ' <br> ')
}); });
});
}); });
app.get("/", function(req, res) { app.get("/", function(req, res) {
var url = req.query.url; var url = req.query.url;
var search = require("youtube-search");
if(url){ if(url){
var opts = { var opts = {
@ -62,8 +68,8 @@ if(err) return
app.get('/youtube/ara', async (req, res) => { app.get('/youtube/ara', async (req, res) => {
var url = req.query.query; var url = req.query.query;
if (!req.query.query) { if (!req.query.query) {
return res.redirect(`/`); return res.redirect(`/`);
} }