From 8bcad94184c8878996ef18ccc8139aeaf4a89451 Mon Sep 17 00:00:00 2001 From: Kenny <27463495+Frontesque@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:56:17 -0500 Subject: [PATCH] add `this.$youtube.search()` THIS FEATURE IS INCOMPLETE! --- NUXT/components/searchOverlay.vue | 2 +- NUXT/plugins/youtube.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NUXT/components/searchOverlay.vue b/NUXT/components/searchOverlay.vue index cb99a33..1f40a57 100644 --- a/NUXT/components/searchOverlay.vue +++ b/NUXT/components/searchOverlay.vue @@ -41,7 +41,7 @@ export default { methods: { textChanged() { this.$youtube.autoComplete(this.text, (res) => { - const data = res.data.replace(/^.*?\(/,'').replace(/\)$/,''); //Format Response + const data = res.replace(/^.*?\(/,'').replace(/\)$/,''); //Format Response this.response = JSON.parse(data)[1] }); }, diff --git a/NUXT/plugins/youtube.js b/NUXT/plugins/youtube.js index 0eee5b4..c3ea0df 100644 --- a/NUXT/plugins/youtube.js +++ b/NUXT/plugins/youtube.js @@ -25,8 +25,36 @@ const module = { callback(err); }); logger("autoComplete", res); - callback(res); + callback(res.data); + }, + + search(text, callback) { + Http.request({ + method: 'GET', + url: 'https://youtube.com/results', + params: { q: text, hl: "en" } + }) + .then((res) => { + //--- Get HTML Only ---// + let html = res.data; + //--- Isolate The Script Containing Video Information ---// + html = html.split("var ytInitialData =")[1].split("")[0].slice(0, -1); + //--- Replace Encoded Characters ---/// + html = html.replace(/\\x([0-9A-F]{2})/ig, (...items) => { return String.fromCharCode(parseInt(items[1], 16)); }); + //--- Properly Format JSON ---// + html = html.replaceAll("\\\\\"", ""); + //--- Parse JSON & Get Results ---// + let results = JSON.parse(html); + results = results.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents; + //--- Output ---// + callback(results); + }) + .catch((err) => { + logger("autoComplete", err); + callback(err); + }); } + } //--- Start ---//