refactor: variable names for xml and m3u

This commit is contained in:
2025-03-23 18:44:06 -07:00
parent 075303e9b6
commit 44f7f8c6c2
2 changed files with 43 additions and 37 deletions

View File

@@ -48,9 +48,9 @@ chalk.level = 3;
Define > General Define > General
*/ */
let URLS_FILE; let FILE_URL;
let FORMATTED_FILE; let FILE_DAT;
let EPG_FILE; let FILE_XML;
let FILE_TAR; let FILE_TAR;
/* /*
@@ -69,9 +69,8 @@ const LOG_LEVEL = process.env.LOG_LEVEL || 10;
*/ */
const extURL = `${ envUrlRepo }/tvapp2-externals/raw/branch/main/urls.txt`; const extURL = `${ envUrlRepo }/tvapp2-externals/raw/branch/main/urls.txt`;
const extEPG = `${ envUrlRepo }/XMLTV-EPG/raw/branch/main/xmltv.1.xml`; const extXML = `${ envUrlRepo }/XMLTV-EPG/raw/branch/main/xmltv.1.xml`;
const extFormatted = `${ envUrlRepo }/tvapp2-externals/raw/branch/main/formatted.dat`; const extFormatted = `${ envUrlRepo }/tvapp2-externals/raw/branch/main/formatted.dat`;
const extEvents = '';
/* /*
Define > Defaults Define > Defaults
@@ -170,18 +169,18 @@ if ( process.pkg )
{ {
Log.info( `Processing Package` ); Log.info( `Processing Package` );
const basePath = path.dirname( process.execPath ); const basePath = path.dirname( process.execPath );
URLS_FILE = path.join( basePath, 'urls.txt' ); FILE_URL = path.join( basePath, 'urls.txt' );
FORMATTED_FILE = path.join( basePath, 'formatted.dat' ); FILE_DAT = path.join( basePath, 'formatted.dat' );
EPG_FILE = path.join( basePath, `${ envFileXML }` ); FILE_XML = path.join( basePath, `${ envFileXML }` );
EPG_FILE.length; FILE_XML.length;
FILE_TAR = path.join( basePath, `${ envFileTAR }` ); FILE_TAR = path.join( basePath, `${ envFileTAR }` );
} }
else else
{ {
Log.info( `Processing Locals` ); Log.info( `Processing Locals` );
URLS_FILE = path.resolve( __dirname, 'urls.txt' ); FILE_URL = path.resolve( __dirname, 'urls.txt' );
FORMATTED_FILE = path.resolve( __dirname, 'formatted.dat' ); FILE_DAT = path.resolve( __dirname, 'formatted.dat' );
EPG_FILE = path.resolve( __dirname, `${ envFileXML }` ); FILE_XML = path.resolve( __dirname, `${ envFileXML }` );
FILE_TAR = path.resolve( __dirname, `${ envFileTAR }` ); FILE_TAR = path.resolve( __dirname, `${ envFileTAR }` );
} }
@@ -526,7 +525,7 @@ async function getTokenizedUrl( channelUrl )
} }
} }
async function serveChannelPlaylist( req, res ) async function serveM3UPlaylist( req, res )
{ {
await semaphore.acquire(); await semaphore.acquire();
try try
@@ -555,7 +554,7 @@ async function serveChannelPlaylist( req, res )
const cachedUrl = getCache( decodedUrl ); const cachedUrl = getCache( decodedUrl );
if ( cachedUrl ) if ( cachedUrl )
{ {
const rewrittenPlaylist = await rewritePlaylist( cachedUrl, req ); const rewrittenPlaylist = await rewriteM3U( cachedUrl, req );
res.writeHead( 200, res.writeHead( 200,
{ {
'Content-Type': 'application/vnd.apple.mpegurl', 'Content-Type': 'application/vnd.apple.mpegurl',
@@ -583,7 +582,7 @@ async function serveChannelPlaylist( req, res )
setCache( decodedUrl, finalUrl, 4 * 60 * 60 * 1000 ); setCache( decodedUrl, finalUrl, 4 * 60 * 60 * 1000 );
const hdUrl = finalUrl.replace( 'tracks-v2a1', 'tracks-v1a1' ); const hdUrl = finalUrl.replace( 'tracks-v2a1', 'tracks-v1a1' );
const rewrittenPlaylist = await rewritePlaylist( hdUrl, req ); const rewrittenPlaylist = await rewriteM3U( hdUrl, req );
res.writeHead( 200, { res.writeHead( 200, {
'Content-Type': 'application/vnd.apple.mpegurl', 'Content-Type': 'application/vnd.apple.mpegurl',
'Content-Disposition': 'inline; filename="' + envFileM3U 'Content-Disposition': 'inline; filename="' + envFileM3U
@@ -615,7 +614,7 @@ async function serveChannelPlaylist( req, res )
Rewrites the URLs Rewrites the URLs
*/ */
async function rewritePlaylist( originalUrl, req ) async function rewriteM3U( originalUrl, req )
{ {
const rawData = await fetchRemote( originalUrl ); const rawData = await fetchRemote( originalUrl );
const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' ); const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' );
@@ -644,14 +643,14 @@ async function rewritePlaylist( originalUrl, req )
Serves IPTV .m3u playlist Serves IPTV .m3u playlist
*/ */
async function servePlaylist( response, req ) async function serveM3U( response, req )
{ {
try try
{ {
const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' ); const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' );
const host = req.headers.host; const host = req.headers.host;
const baseUrl = `${ protocol }://${ host }`; const baseUrl = `${ protocol }://${ host }`;
const formattedContent = fs.readFileSync( FORMATTED_FILE, 'utf-8' ); const formattedContent = fs.readFileSync( FILE_DAT, 'utf-8' );
const updatedContent = formattedContent const updatedContent = formattedContent
.replace( /(https?:\/\/[^\s]*thetvapp[^\s]*)/g, ( fullUrl ) => .replace( /(https?:\/\/[^\s]*thetvapp[^\s]*)/g, ( fullUrl ) =>
{ {
@@ -671,7 +670,7 @@ async function servePlaylist( response, req )
} }
catch ( error ) catch ( error )
{ {
Log.error( `Error in servePlaylist:`, chalk.white( `` ), chalk.grey( `${ error.message }` ) ); Log.error( `Error in serveM3U:`, chalk.white( `` ), chalk.grey( `${ error.message }` ) );
response.writeHead( 500, { response.writeHead( 500, {
'Content-Type': 'text/plain' 'Content-Type': 'text/plain'
@@ -685,14 +684,14 @@ async function servePlaylist( response, req )
Serves IPTV .xml guide data Serves IPTV .xml guide data
*/ */
async function serveXmltv( response, req ) async function serveXML( response, req )
{ {
try try
{ {
const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' ); const protocol = req.headers['x-forwarded-proto']?.split( ',' )[0] || ( req.socket.encrypted ? 'https' : 'http' );
const host = req.headers.host; const host = req.headers.host;
const baseUrl = `${ protocol }://${ host }`; const baseUrl = `${ protocol }://${ host }`;
const formattedContent = fs.readFileSync( EPG_FILE, 'utf-8' ); const formattedContent = fs.readFileSync( FILE_XML, 'utf-8' );
response.writeHead( 200, { response.writeHead( 200, {
'Content-Type': 'application/xml', 'Content-Type': 'application/xml',
@@ -703,7 +702,7 @@ async function serveXmltv( response, req )
} }
catch ( error ) catch ( error )
{ {
Log.error( `Error in servePlaylist:`, chalk.white( `` ), chalk.grey( `${ error.message }` ) ); Log.error( `Error in serveM3U:`, chalk.white( `` ), chalk.grey( `${ error.message }` ) );
response.writeHead( 500, { response.writeHead( 500, {
'Content-Type': 'text/plain' 'Content-Type': 'text/plain'
@@ -779,9 +778,9 @@ async function initialize()
{ {
Log.info( `Initializing server...` ); Log.info( `Initializing server...` );
await ensureFileExists( extURL, URLS_FILE ); await ensureFileExists( extURL, FILE_URL );
await ensureFileExists( extFormatted, FORMATTED_FILE ); await ensureFileExists( extFormatted, FILE_DAT );
await ensureFileExists( extEPG, EPG_FILE ); await ensureFileExists( extXML, FILE_XML );
/* /*
Create tar.gz of xml data Create tar.gz of xml data
@@ -867,7 +866,7 @@ const server = http.createServer( ( request, response ) =>
{ {
Log.info( `Received request for playlist data`, chalk.white( `` ), chalk.grey( `/playlist` ) ); Log.info( `Received request for playlist data`, chalk.white( `` ), chalk.grey( `/playlist` ) );
await servePlaylist( response, request ); await serveM3U( response, request );
return; return;
} }
@@ -875,7 +874,7 @@ const server = http.createServer( ( request, response ) =>
{ {
Log.info( `Received request for channel data`, chalk.white( `` ), chalk.grey( `/channel` ) ); Log.info( `Received request for channel data`, chalk.white( `` ), chalk.grey( `/channel` ) );
await serveChannelPlaylist( request, response ); await serveM3UPlaylist( request, response );
return; return;
} }

View File

@@ -53,9 +53,9 @@
<!-- <i class="fa fa-fw fa-solid fa-file-lines" aria-hidden="true"></i> --> <!-- <i class="fa fa-fw fa-solid fa-file-lines" aria-hidden="true"></i> -->
</td> </td>
<td class="file cell-file"> <td class="file cell-file">
<a id="playlist-url" target="_blank"></a> <a id="m3u-name" target="_blank"></a>
</td> </td>
<td class="link cell-link"><a id="playlist-link" target="_blank"></a></td> <td class="link cell-link"><a id="m3u-link" target="_blank"></a></td>
<td class="desc cell-desc">Playlist data file which contains a list of all channels, their associated group, and logo URL.</td> <td class="desc cell-desc">Playlist data file which contains a list of all channels, their associated group, and logo URL.</td>
</tr> </tr>
<tr> <tr>
@@ -66,9 +66,9 @@
<!-- <i class="fa fa-fw fa-solid fa-file-lines" aria-hidden="true"></i> --> <!-- <i class="fa fa-fw fa-solid fa-file-lines" aria-hidden="true"></i> -->
</td> </td>
<td class="file cell-file"> <td class="file cell-file">
<a id="epg-url" target="_blank"></a> <a id="xml-name" target="_blank"></a>
</td> </td>
<td class="link cell-link"><a id="epg-link" target="_blank"></a></td> <td class="link cell-link"><a id="xml-link" target="_blank"></a></td>
<td class="desc cell-desc">XML / EPG guide data which contains a list of all programs which are scheduled to play on a specific channel.</td> <td class="desc cell-desc">XML / EPG guide data which contains a list of all programs which are scheduled to play on a specific channel.</td>
</tr> </tr>
<tr> <tr>
@@ -105,13 +105,20 @@
</footer> </footer>
<script> <script>
const baseURL = window.location.origin; const urlBase = window.location.origin;
const playlistURL = baseURL + "/playlist"; const urlM3U = urlBase + "/playlist";
const epgURL = baseURL + "/epg"; const urlXML = urlBase + "/epg";
const urlTAR = urlBase + "/tar"; const urlTAR = urlBase + "/tar";
document.getElementById("playlist-url").href = playlistURL;
document.getElementById("playlist-link").textContent = playlistURL; document.getElementById("m3u-name").textContent = "<%= fileM3U %>";
document.getElementById("playlist-link").href = playlistURL; document.getElementById("m3u-name").href = urlM3U;
document.getElementById("m3u-link").textContent = urlM3U;
document.getElementById("m3u-link").href = urlM3U;
document.getElementById("xml-name").textContent = "<%= fileXML %>";
document.getElementById("xml-name").href = urlXML;
document.getElementById("xml-link").textContent = urlXML;
document.getElementById("xml-link").href = urlXML;
document.getElementById("tar-name").textContent = "<%= fileTAR %>"; document.getElementById("tar-name").textContent = "<%= fileTAR %>";
document.getElementById("tar-name").href = urlTAR; document.getElementById("tar-name").href = urlTAR;