2022-02-28 15:24:06 +00:00
|
|
|
//--- Modules/Imports ---//
|
2022-02-25 18:54:15 +00:00
|
|
|
import { Http } from '@capacitor-community/http';
|
|
|
|
|
2022-02-28 15:24:06 +00:00
|
|
|
//--- Logger Function ---//
|
|
|
|
function logger(func, data) {
|
2022-03-02 16:02:51 +00:00
|
|
|
module.logs.unshift({
|
2022-02-28 15:24:06 +00:00
|
|
|
name: func,
|
|
|
|
time: Date.now(),
|
|
|
|
data: data
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:39:17 +00:00
|
|
|
const module = {
|
2022-02-28 15:24:06 +00:00
|
|
|
logs: new Array(),
|
2022-02-25 18:54:15 +00:00
|
|
|
|
2022-02-28 15:24:06 +00:00
|
|
|
//--- Get YouTube's Search Auto Complete ---//
|
|
|
|
async autoComplete(text, callback) {
|
|
|
|
const res = await Http.request({
|
2022-02-26 19:35:36 +00:00
|
|
|
method: 'GET',
|
2022-02-25 18:54:15 +00:00
|
|
|
url: 'https://suggestqueries-clients6.youtube.com/complete/search',
|
2022-02-26 19:35:36 +00:00
|
|
|
params: { client: 'youtube', q: text }
|
2022-03-02 13:14:52 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
logger("autoComplete", err);
|
|
|
|
callback(err);
|
2022-02-26 19:35:36 +00:00
|
|
|
});
|
2022-02-28 15:24:06 +00:00
|
|
|
logger("autoComplete", res);
|
2022-03-03 17:56:17 +00:00
|
|
|
callback(res.data);
|
|
|
|
},
|
|
|
|
|
|
|
|
search(text, callback) {
|
2022-03-03 23:07:33 +00:00
|
|
|
|
2022-03-03 17:56:17 +00:00
|
|
|
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 ---//
|
2022-03-03 23:07:33 +00:00
|
|
|
html = html.split("var ytInitialData = '")[1].split("';</script>")[0];
|
2022-03-03 17:56:17 +00:00
|
|
|
//--- 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("\\\\\"", "");
|
2022-03-03 23:07:33 +00:00
|
|
|
//--- Parse JSON ---//
|
|
|
|
html = JSON.parse(html);
|
|
|
|
|
|
|
|
//--- Get Results ---// ( Thanks To appit-online On Github ) -> https://github.com/appit-online/youtube-search/blob/master/src/lib/search.ts
|
|
|
|
let results;
|
|
|
|
if (html && html.contents && html.contents.sectionListRenderer && html.contents.sectionListRenderer.contents
|
|
|
|
&& html.contents.sectionListRenderer.contents.length > 0
|
|
|
|
&& html.contents.sectionListRenderer.contents[0].itemSectionRenderer
|
|
|
|
&& html.contents.sectionListRenderer.contents[0].itemSectionRenderer.contents.length > 0) {
|
|
|
|
results = html.contents.sectionListRenderer.contents[0].itemSectionRenderer.contents;
|
|
|
|
logger("search", results);
|
|
|
|
callback(results);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
results = JSON.parse(html.split('{"itemSectionRenderer":{"contents":')[html.split('{"itemSectionRenderer":{"contents":').length - 1].split(',"continuations":[{')[0]);
|
|
|
|
logger("search", results);
|
|
|
|
callback(results);
|
|
|
|
} catch (e) {}
|
|
|
|
try {
|
|
|
|
results = JSON.parse(html.split('{"itemSectionRenderer":')[html.split('{"itemSectionRenderer":').length - 1].split('},{"continuationItemRenderer":{')[0]).contents;
|
|
|
|
logger("search", results);
|
|
|
|
callback(results);
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-03 17:56:17 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2022-03-03 18:05:50 +00:00
|
|
|
logger("search", err);
|
2022-03-03 17:56:17 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
2022-02-25 18:39:17 +00:00
|
|
|
}
|
2022-03-03 17:56:17 +00:00
|
|
|
|
2022-02-25 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 15:24:06 +00:00
|
|
|
//--- Start ---//
|
2022-02-25 18:39:17 +00:00
|
|
|
export default ({ app }, inject) => {
|
|
|
|
inject('youtube', module)
|
|
|
|
}
|
2022-02-28 15:24:06 +00:00
|
|
|
logger("Initialize","Program Started");
|