add `this.$youtube.search()`

THIS FEATURE IS INCOMPLETE!
This commit is contained in:
Kenny 2022-03-03 12:56:17 -05:00
parent 63df4fb355
commit 8bcad94184
2 changed files with 30 additions and 2 deletions

View File

@ -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]
});
},

View File

@ -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("</script>")[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 ---//