2022-02-28 15:24:06 +00:00
//--- Modules/Imports ---//
2022-03-21 23:47:11 +00:00
import { Http } from "@capacitor-community/http" ;
import Innertube from "./innertube" ;
import constants from "./constants" ;
2022-03-28 02:07:03 +00:00
import rendererUtils from "./renderers" ;
2022-03-26 08:24:40 +00:00
import { Buffer } from "buffer" ;
2022-03-29 07:02:52 +00:00
import iconv from "iconv-lite" ;
2022-02-25 18:54:15 +00:00
2022-02-28 15:24:06 +00:00
//--- Logger Function ---//
2022-03-16 09:07:40 +00:00
function logger ( func , data , isError = false ) {
2022-03-21 23:47:11 +00:00
searchModule . logs . unshift ( {
name : func ,
time : Date . now ( ) ,
data : data ,
error : isError ,
} ) ;
2022-02-28 15:24:06 +00:00
}
2022-03-27 09:15:09 +00:00
function getEncoding ( contentType ) {
const re = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i ;
const content = re . exec ( contentType ) ;
console . log ( content ) ;
2022-03-29 07:02:52 +00:00
return content [ 1 ] . toLowerCase ( ) ;
2022-03-27 09:15:09 +00:00
}
2022-03-16 09:07:40 +00:00
const searchModule = {
2022-03-21 23:47:11 +00:00
logs : new Array ( ) ,
//--- Get YouTube's Search Auto Complete ---//
autoComplete ( text , callback ) {
2022-03-26 08:24:40 +00:00
Http . get ( {
url : ` ${ constants . URLS . YT _SUGGESTIONS } /search?q= ${ encodeURIComponent (
text
) } & client = youtube & ds = yt ` ,
responseType : "arraybuffer" ,
2022-03-21 23:47:11 +00:00
} )
. then ( ( res ) => {
2022-03-27 09:15:09 +00:00
const contentType = res . headers [ "Content-Type" ] ;
2022-03-26 08:24:40 +00:00
// make a new buffer object from res.data
const buffer = Buffer . from ( res . data , "base64" ) ;
// convert res.data from iso-8859-1 to utf-8
2022-03-29 07:02:52 +00:00
const data = iconv . decode ( buffer , getEncoding ( contentType ) ) ;
2022-03-26 08:24:40 +00:00
logger ( constants . LOGGER _NAMES . autoComplete , data ) ;
callback ( data ) ;
2022-03-21 23:47:11 +00:00
} )
. catch ( ( err ) => {
logger ( constants . LOGGER _NAMES . autoComplete , err , true ) ;
callback ( err ) ;
} ) ;
} ,
2022-02-25 18:54:15 +00:00
2022-03-21 23:47:11 +00:00
getReturnYoutubeDislike ( id , callback ) {
Http . request ( {
method : "GET" ,
url : ` https://returnyoutubedislikeapi.com/votes ` ,
params : { videoId : id } ,
} )
. then ( ( res ) => {
logger ( "rydData" , res . data ) ;
callback ( res . data ) ;
} )
. catch ( ( err ) => {
logger ( "codeRun" , err , true ) ;
callback ( err ) ;
} ) ;
} ,
} ;
2022-03-13 23:45:04 +00:00
2022-03-20 09:02:52 +00:00
//--- Recommendations ---//
2022-03-17 13:17:32 +00:00
2022-03-18 06:15:19 +00:00
let InnertubeAPI ;
2022-03-20 09:02:52 +00:00
// Loads Innertube object. This will be the object used in all future Innertube API calls. getAPI Code provided by Lightfire228 (https://github.com/Lightfire228)
2022-03-17 13:17:32 +00:00
// These are just a way for the backend Javascript to communicate with the front end Vue scripts. Essentially a wrapper inside a wrapper
2022-03-20 09:50:13 +00:00
const innertubeModule = {
2022-03-21 23:47:11 +00:00
async getAPI ( ) {
if ( ! InnertubeAPI ) {
InnertubeAPI = await Innertube . createAsync ( ( message , isError ) => {
logger ( constants . LOGGER _NAMES . innertube , message , isError ) ;
} ) ;
}
return InnertubeAPI ;
} ,
2022-03-18 06:15:19 +00:00
2022-03-21 23:47:11 +00:00
async getVid ( id ) {
try {
return await InnertubeAPI . VidInfoAsync ( id ) ;
} catch ( error ) {
logger ( constants . LOGGER _NAMES . watch , error , true ) ;
}
} ,
2022-03-23 22:15:52 +00:00
getThumbnail ( id , resolution , backupThumbnail ) {
2022-03-23 11:21:06 +00:00
if ( resolution == "max" ) {
const url = ` https://img.youtube.com/vi/ ${ id } /maxresdefault.jpg ` ;
let img = new Image ( ) ;
img . src = url ;
img . onload = function ( ) {
if ( img . height !== 120 ) return url ;
} ;
}
2022-03-23 22:15:52 +00:00
if ( backupThumbnail [ backupThumbnail . length - 1 ] )
return backupThumbnail [ backupThumbnail . length - 1 ] . url ;
else return ` https://img.youtube.com/vi/ ${ id } /mqdefault.jpg ` ;
2022-03-23 11:21:06 +00:00
} ,
2022-03-21 23:47:11 +00:00
// It just works™
// Front page recommendation
async recommend ( ) {
const response = await InnertubeAPI . getRecommendationsAsync ( ) ;
2022-03-31 02:22:22 +00:00
2022-03-21 23:47:11 +00:00
if ( ! response . success )
throw new Error ( "An error occurred and innertube failed to respond" ) ;
const contents =
response . data . contents . singleColumnBrowseResultsRenderer . tabs [ 0 ]
. tabRenderer . content . sectionListRenderer . contents ;
const final = contents . map ( ( shelves ) => {
2022-03-24 11:47:13 +00:00
const video = shelves . shelfRenderer ? . content ? . horizontalListRenderer ;
2022-03-21 23:47:11 +00:00
2022-03-23 11:21:06 +00:00
if ( video ) return video ;
2022-03-21 23:47:11 +00:00
} ) ;
2022-04-01 01:02:08 +00:00
const continuations =
response . data . contents . singleColumnBrowseResultsRenderer . tabs [ 0 ]
. tabRenderer . content . sectionListRenderer . continuations ;
2022-03-31 02:22:22 +00:00
console . log ( { continuations : continuations , contents : final } ) ;
return { continuations : continuations , contents : final } ;
} ,
2022-04-01 01:02:08 +00:00
async recommendContinuation ( continuation , endpoint ) {
const response = await InnertubeAPI . getContinuationsAsync (
continuation ,
endpoint
) ;
const contents =
response . data . continuationContents . sectionListContinuation . contents ;
2022-03-31 02:22:22 +00:00
const final = contents . map ( ( shelves ) => {
const video = shelves . shelfRenderer ? . content ? . horizontalListRenderer ;
if ( video ) return video ;
} ) ;
2022-04-01 01:02:08 +00:00
const continuations =
response . data . continuationContents . sectionListContinuation . continuations ;
2022-03-31 02:22:22 +00:00
return { continuations : continuations , contents : final } ;
} ,
2022-03-23 13:07:03 +00:00
async search ( query ) {
try {
const response = await InnertubeAPI . getSearchAsync ( query ) ;
2022-03-24 11:47:13 +00:00
return response . contents . sectionListRenderer ;
2022-03-23 13:07:03 +00:00
} catch ( err ) {
logger ( constants . LOGGER _NAMES . search , err , true ) ;
}
} ,
2022-03-29 07:02:52 +00:00
async saveApiStats ( currentPageType , id , event ) {
await InnertubeAPI . apiStats ( currentPageType , id , event ) ;
} ,
2022-03-21 23:47:11 +00:00
} ;
2022-03-20 09:50:13 +00:00
2022-02-28 15:24:06 +00:00
//--- Start ---//
2022-02-25 18:39:17 +00:00
export default ( { app } , inject ) => {
2022-03-21 23:47:11 +00:00
inject ( "youtube" , { ... searchModule , ... innertubeModule } ) ;
inject ( "logger" , logger ) ;
2022-03-28 02:07:03 +00:00
inject ( "rendererUtils" , rendererUtils ) ;
2022-03-21 23:47:11 +00:00
} ;
2022-03-19 18:26:20 +00:00
logger ( constants . LOGGER _NAMES . init , "Program Started" ) ;