Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit 5976cdb7612fce94e2b49651b51d519ca68a442d
parent 32b72ef427123e9131be9d55057f0efca25a71a4
Author: danyisill <danyisill@users.noreply.github.com>
Date:   Mon,  6 Jul 2020 18:48:10 +0300

Switch to invidio.us API

Diffstat:
Mlib/commands/youtube.ts | 34++++++++++++++++++++++++++++------
1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/lib/commands/youtube.ts b/lib/commands/youtube.ts @@ -1,10 +1,32 @@ -import yt_search from '../api/yt_scrape'; +import fetch from "node-fetch"; + +//.yt x +//.yt x n, where n < 20 +//.yt new x +//.yt channel/playlist x + export default (home_scope: HomeScope) => { const { message, args } = home_scope; - const query = args.join(' ').trim(); - if (query.length === 0 || args.length === 0) - return message.answer('Well, what should I search for?'); - yt_search({ query }) + var query = args.join(" ").trim(); + const sort_by = (args[0] == "new") ? "upload_date" : "relevance"; + const type = (args[0] == "channel" || args[0] == "playlist") ? args.shift() : "video"; + if(query.length === 0 || args.length === 0) + query = "bruh city"; + else{ + var num_match = query.match(/[ ]+(\d+)$/); + if (num_match) + query = query.slice(0, -num_match[1].length).trim(); + else + num_match = 1; + } + + const result = await fetch(`https://invidio.us/api/v1/search?q=${query}&sort_by=${sort_by}&type=${type}`); + const res_json = await result.json(); + const res = res_json[Math.abs(num_match - 1)]; + message.answer(`Search for '${query}' (result №${num_match || 1}): https://youtu.be/${res.videoId} \n published ${res.publishedText} view count: ${res.viewCount} length: ${lengthSeconds / 60} minutes`); + //todo timestamp generation + + /*yt_search({ query }) .then(message.reply.bind(message)) - .catch(message.answer.bind(message)); + .catch(message.answer.bind(message));*/ };