From 097d66bf21e78cfb012c53182e595600e8da5905 Mon Sep 17 00:00:00 2001 From: freearhey Date: Tue, 23 Apr 2019 21:49:09 +0300 Subject: [PATCH] Added script to generate "all in one" playlist --- package.json | 3 ++- src/generate.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ src/helper.js | 12 +++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/generate.js create mode 100644 src/helper.js diff --git a/package.json b/package.json index 539fd6968..fb1091d85 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ ], "main": "index.m3u", "scripts": { - "test": "node test/index.js" + "test": "node test/index.js", + "generate": "node src/generate.js" }, "repository": { "type": "git", diff --git a/src/generate.js b/src/generate.js new file mode 100644 index 000000000..18df3301d --- /dev/null +++ b/src/generate.js @@ -0,0 +1,71 @@ +const parsers = require('playlist-parser') +const M3U = parsers.M3U +const fs = require("fs") +const path = require('path') +const helper = require('./helper') + +let outputPath = path.resolve(__dirname) + '/../index.all.m3u' +let channels = 0 +let duplicates = 0 +let cache = {} + +fs.writeFileSync(outputPath, '#EXTM3U\n') + +function init() { + + let countries = loadPlaylist('index.m3u') + // countries = countries.slice(0, 2) + + for(let country of countries) { + + const playlist = loadPlaylist(country.file) + + for(let item of playlist) { + + let title = (item.artist) ? item.length + ',' + item.artist + '-' + item.title : item.title + + let url = item.file + + if(checkCache(url)) { + + duplicates++ + + } else { + + writeToFile(title, url) + + addToCache(url) + + } + + channels++ + + } + + } +} + +init() + +console.log(`Total: ${channels}. Duplicates: ${duplicates}.`) + + +function loadPlaylist(filename) { + return M3U.parse(fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })) +} + +function writeToFile(title, file) { + fs.appendFileSync(outputPath, '#EXTINF:' + title + '\n' + file + '\n') +} + +function addToCache(url) { + let id = helper.getUrlPath(url) + + cache[id] = true +} + +function checkCache(url) { + let id = helper.getUrlPath(url) + + return cache.hasOwnProperty(id) +} \ No newline at end of file diff --git a/src/helper.js b/src/helper.js new file mode 100644 index 000000000..d15c5eaf6 --- /dev/null +++ b/src/helper.js @@ -0,0 +1,12 @@ +const parser = require('url') + +function getUrlPath(u) { + let parsedUrl = parser.parse(u) + let searchQuery = parsedUrl.search || '' + + return parsedUrl.host + parsedUrl.pathname + searchQuery +} + +module.exports = { + getUrlPath +}