2022-12-19 12:52:57 +01:00
const {
fetcher ,
core ,
wiki ,
musicInfo ,
modules ,
version ,
initlog ,
init ,
} = require ( "../libpoketube-initsys.js" ) ;
2023-09-06 22:48:42 +02:00
2022-11-09 17:54:00 +01:00
const {
IsJsonString ,
convert ,
getFirstLine ,
capitalizeFirstLetter ,
turntomins ,
getRandomInt ,
getRandomArbitrary ,
} = require ( "../ptutils/libpt-coreutils.js" ) ;
2023-09-06 22:48:42 +02:00
const path = require ( "path" ) ;
const fs = require ( "node:fs" ) ;
const CleanCSS = require ( "clean-css" ) ;
2023-06-12 21:11:00 +02:00
2022-11-09 17:54:00 +01:00
const sha384 = modules . hash ;
2023-09-06 22:48:42 +02:00
const notice =
"/* the code is Licensed in gpl-3.0-or-later. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public License for more detailsYou should have received a copy of the GNU General Public Licensealong with this program. If not, see <https://www.gnu.org/licenses/>. - add the param nomin to view source code. (eg poketube.fun/css/poketube.css?nomin=true) */" ;
2022-11-09 17:54:00 +01:00
2024-02-02 21:54:22 +01:00
function getJson ( str ) {
try {
return JSON . parse ( str ) ;
} catch {
return null ;
}
}
2022-11-09 17:54:00 +01:00
module . exports = function ( app , config , renderTemplate ) {
2023-09-06 22:48:42 +02:00
var html _location = "./css/" ;
2023-09-30 22:19:31 +02:00
var location _pwa = "./pwa/" ;
2022-12-19 12:52:57 +01:00
2023-09-06 22:48:42 +02:00
app . get ( "/privacy" , function ( req , res ) {
if ( req . hostname == "poketube.fun" ) {
renderTemplate ( res , req , "priv.ejs" , {
isMobile : req . useragent . isMobile ,
} ) ;
} else {
renderTemplate ( res , req , "priv-custom.ejs" ) ;
}
2022-12-19 12:52:57 +01:00
} ) ;
2024-02-25 03:53:25 +01:00
app . get ( "/502" , function ( req , res ) {
renderTemplate ( res , req , "502.ejs" ) ;
} ) ;
2023-09-06 22:48:42 +02:00
app . get ( "/143" , function ( req , res ) {
var number _easteregg = getRandomArbitrary ( 0 , 143 ) ;
2022-12-19 12:52:57 +01:00
2023-09-06 22:48:42 +02:00
if ( number _easteregg == "143" ) {
renderTemplate ( res , req , "143.ejs" , {
something : req . query . something ,
} ) ;
2022-12-19 12:52:57 +01:00
}
2023-08-29 11:06:36 +02:00
2023-09-06 22:48:42 +02:00
if ( req . query . number == "143" ) {
renderTemplate ( res , req , "143.ejs" , {
something : req . query . something ,
} ) ;
2023-08-29 11:06:36 +02:00
}
2023-09-06 22:48:42 +02:00
if ( req . query . something == "143" ) {
renderTemplate ( res , req , "143.ejs" , {
something : req . query . something ,
} ) ;
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if ( number _easteregg != "143" ) {
return res . redirect ( "/" + "?number=" + number _easteregg ) ;
}
} ) ;
2023-08-30 23:22:41 +02:00
2023-12-25 16:51:54 +01:00
app . get ( "/rewind" , function ( req , res ) {
renderTemplate ( res , req , "rewind.ejs" ) ;
} ) ;
2024-01-13 21:56:59 +01:00
app . get ( "/translate" , async function ( req , res ) {
const { fetch } = await import ( "undici" ) ;
const api _url = "https://simplytranslate.org/api/translate" ;
// Fetch translation data
const translationResponse = await fetch (
` ${ api _url } ?from= ${ req . query . from _language } &to= ${ req . query . to _language } &text= ${ req . query . input } &engine=google `
) ;
// Check if the request was successful (status code 200)
const translationData = await translationResponse . json ( ) ;
// Extract translated_text from the response
const translatedText = translationData . translated _text ;
// Render the template with the translated text
renderTemplate ( res , req , "translate.ejs" , {
translation : translatedText ,
text : req . query . input || "enter text here" ,
from _language : req . query . from _language ,
to _language : req . query . to _language ,
2024-01-14 09:11:54 +01:00
isMobile : req . useragent . isMobile ,
2024-01-13 21:56:59 +01:00
} ) ;
} ) ;
2023-09-06 22:48:42 +02:00
app . get ( "/domains" , function ( req , res ) {
renderTemplate ( res , req , "domains.ejs" ) ;
} ) ;
2023-08-30 23:22:41 +02:00
2024-01-14 21:13:46 +01:00
app . get ( "/apps" , function ( req , res ) {
renderTemplate ( res , req , "apps.ejs" ) ;
} ) ;
2024-02-02 21:54:22 +01:00
app . get ( "/playlist" , async function ( req , res ) {
const { fetch } = await import ( "undici" ) ;
if ( ! req . query . list ) res . redirect ( "/" ) ;
if ( req . useragent . isMobile ) res . redirect ( "/" ) ;
const playlist = await fetch (
` https://invid-api.poketube.fun/api/v1/playlists/ ${ req . query . list } ?hl=en-us `
) ;
const p = getJson ( await playlist . text ( ) ) ;
var mediaproxy = config . media _proxy ;
2024-03-08 05:23:31 +01:00
if ( req . useragent . source . includes ( "Pardus" ) ) {
var media _proxy = "https://media-proxy.ashley0143.xyz" ;
}
2024-02-02 21:54:22 +01:00
renderTemplate ( res , req , "playlist.ejs" , {
p ,
mediaproxy ,
} ) ;
} ) ;
2024-01-14 21:13:46 +01:00
2023-09-06 22:48:42 +02:00
app . get ( "/license" , function ( req , res ) {
renderTemplate ( res , req , "license.ejs" ) ;
} ) ;
2023-04-12 22:41:37 +02:00
2024-01-13 21:56:59 +01:00
app . get ( "/map" , function ( req , res ) {
2023-11-03 17:02:46 +01:00
renderTemplate ( res , req , "map.ejs" ) ;
} ) ;
2024-01-13 21:56:59 +01:00
2023-09-06 22:48:42 +02:00
app . get ( "/credits" , function ( req , res ) {
renderTemplate ( res , req , "want-you-gone.ejs" ) ;
} ) ;
2024-01-13 21:56:59 +01:00
2023-09-23 17:32:01 +02:00
app . get ( "/settings" , function ( req , res ) {
renderTemplate ( res , req , "content-settings.ejs" ) ;
} ) ;
2024-01-13 21:56:59 +01:00
2023-12-15 06:08:34 +01:00
app . get ( "/offline" , function ( req , res ) {
res . sendFile ( "offline.html" , { root : location _pwa } ) ;
} ) ;
2024-01-13 21:56:59 +01:00
2023-12-15 06:08:34 +01:00
app . get ( "/manifest.json" , function ( req , res ) {
res . sendFile ( "manifest.json" , { root : location _pwa } ) ;
} ) ;
2024-01-13 21:56:59 +01:00
2023-09-06 22:48:42 +02:00
app . get ( "/customize" , function ( req , res ) {
const tab = req . query . tab ;
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
renderTemplate ( res , req , "custom-css.ejs" , {
tab ,
2023-04-12 22:41:37 +02:00
} ) ;
2023-02-22 17:02:06 +01:00
} ) ;
2022-12-27 00:54:55 +01:00
2023-09-06 22:48:42 +02:00
const cssDir = "./css/" ;
2023-08-30 23:22:41 +02:00
2024-01-30 17:51:59 +01:00
app . get ( "/favicon.ico" , function ( req , res ) {
res . sendFile ( "favicon.ico" , { root : cssDir } ) ;
} ) ;
2024-02-13 23:40:58 +01:00
app . get ( "/bg-full.webm" , function ( req , res ) {
res . sendFile ( "bg-full.webm" , { root : cssDir } ) ;
} ) ;
app . get ( "/bg-720.webm" , function ( req , res ) {
res . sendFile ( "bg-720.webm" , { root : cssDir } ) ;
} ) ;
app . get ( "/bg-480.webm" , function ( req , res ) {
res . sendFile ( "bg-480.webm" , { root : cssDir } ) ;
} ) ;
2024-01-30 17:51:59 +01:00
2023-09-06 22:48:42 +02:00
app . get ( "/css/:id" , ( req , res ) => {
const filePath = path . join ( cssDir , req . params . id ) ;
if ( ! fs . existsSync ( filePath ) ) {
res . status ( 404 ) . send ( "File not found" ) ;
return ;
2023-08-29 11:06:36 +02:00
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if ( req . params . id . endsWith ( ".css" ) && ! req . query . nomin ) {
// Minimize the CSS file
const css = fs . readFileSync ( filePath , "utf8" ) ;
const minimizedCss = new CleanCSS ( ) . minify ( css ) . styles ;
// Serve the minimized CSS file
res . header ( "Content-Type" , "text/css" ) ;
2024-02-02 21:54:22 +01:00
res . send (
notice +
" " +
minimizedCss . replace ( "https://p.poketube.fun" , config . p _url )
) ;
2023-08-30 23:22:41 +02:00
} else {
2023-09-06 22:48:42 +02:00
// Serve the original file
res . sendFile ( req . params . id , { root : html _location } ) ;
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if ( req . params . id . endsWith ( ".js" ) ) {
res . redirect ( "/static/" + req . params . id ) ;
}
} ) ;
2023-08-30 23:22:41 +02:00
2024-01-13 21:56:59 +01:00
app . get ( "/game-hub" , function ( req , res ) {
2024-01-06 21:22:23 +01:00
renderTemplate ( res , req , "gamehub.ejs" , {
2024-01-13 21:56:59 +01:00
game : req . query . game ,
} ) ;
2024-01-06 21:22:23 +01:00
} ) ;
2024-01-13 21:56:59 +01:00
app . get ( "/static/:id" , ( req , res ) => {
const id = req . params . id ;
if ( id . endsWith ( ".css" ) ) {
res . redirect ( "/css/" + id ) ;
} else if ( id . endsWith ( ".js" ) ) {
if ( id . endsWith ( ".bundle.js" ) ) {
const jsFiles = [ "app.js" , "custom-css.js" , "emojis.js" ] ;
const combinedContent = jsFiles
. map ( ( fileName ) => {
const filePath = path . join ( html _location , fileName ) ;
return fs . existsSync ( filePath )
? fs . readFileSync ( filePath , "utf-8" )
: "" ;
} )
2024-02-03 22:36:58 +01:00
. join ( "\n" + "\n" ) ;
2024-01-13 21:56:59 +01:00
const minimizedJs = require ( "uglify-js" ) . minify ( combinedContent ) . code ;
res . header ( "Content-Type" , "text/javascript" ) ;
res . send (
"// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0-or-later" +
` \n ` +
` // Includes app.js, emojis.js, and custom-css.js. Source code can be found for these 3 files in https://codeberg.org/Ashley/poketube/src/branch/main/css/ ` +
` \n ` +
minimizedJs +
` \n ` +
"// @license-end"
) ;
} else {
const filePath = path . join ( html _location , id ) ;
if ( ! fs . existsSync ( filePath ) ) {
res . status ( 404 ) . send ( "File not found" ) ;
return ;
}
const js = fs . readFileSync ( filePath , "utf8" ) ;
const minimizedJs = require ( "uglify-js" ) . minify ( js ) . code ;
2024-02-02 21:54:22 +01:00
2024-01-13 21:56:59 +01:00
res . header ( "Content-Type" , "text/javascript" ) ;
res . send (
"// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0-or-later" +
` \n ` +
` // Source code can be found in: https://codeberg.org/Ashley/poketube/src/branch/main/css/ ${ id } ` +
` \n ` +
minimizedJs +
` \n ` +
"// @license-end"
) ;
2023-08-30 23:22:41 +02:00
}
2024-01-13 21:56:59 +01:00
} else {
res . sendFile ( id , { root : html _location } ) ;
2023-08-29 11:06:36 +02:00
}
2024-01-13 21:56:59 +01:00
} ) ;
2022-12-19 12:52:57 +01:00
} ;