From de37a7efad4b96c6b10c223a62b75f9616c971c4 Mon Sep 17 00:00:00 2001 From: freearhey Date: Tue, 30 Apr 2019 16:52:38 +0300 Subject: [PATCH] Added formatter script --- helpers/formatter.js | 117 +++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 helpers/formatter.js diff --git a/helpers/formatter.js b/helpers/formatter.js new file mode 100644 index 000000000..481d1345b --- /dev/null +++ b/helpers/formatter.js @@ -0,0 +1,117 @@ +const parsers = require('playlist-parser') +const M3U = parsers.M3U +const fs = require("fs") +const path = require('path') +const urlParser = require('url') + +let total = 0 + +function init() { + + let countries = loadPlaylist('index.m3u') + // countries = countries.slice(0, 10) + + let channels = [] + + for(let country of countries) { + + const playlist = loadPlaylist(country.file) + + for(let item of playlist) { + + let channel = { + title: parseTitle(item), + file: item.file, + id: parseTvgId(item), + name: parseTvgName(item), + logo: parseTvgLogo(item), + group: parseGroupTitle(item) + } + + channels.push(channel) + + } + + channels = channels.sort(byTitle) + + let outputPath = path.resolve(__dirname) + '/../' + country.file + + fs.writeFileSync(outputPath, '#EXTM3U\n') + + channels.forEach(channel => { + writeToFile(outputPath, channel) + }) + + // console.log(country.title, channels) + + total += channels.length + + channels = [] + } +} + +init() + +console.log(`Total: ${total}.`) + +function loadPlaylist(filename) { + return M3U.parse(fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })) +} + +function writeToFile(outputPath, channel) { + let info = `#EXTINF:-1 tvg-id="${channel.id}" tvg-name="${channel.name}" tvg-logo="${channel.logo}" group-title="${channel.group}",${channel.title}` + + fs.appendFileSync(outputPath, info + '\n' + channel.file + '\n') +} + +function getInfo(item) { + return (item.artist) ? item.artist + '-' + item.title : item.title +} + +function parseTitle(item) { + let info = getInfo(item) + let parts = info.split(',') + + return parts[parts.length - 1].trim() +} + +function parseTvgId(item) { + let info = getInfo(item) + let matches = info.match(/tvg\-id\=\"(.*?)\"/i) + + return (matches && matches.length > 1) ? matches[1] : '' +} + +function parseTvgName(item) { + let info = getInfo(item) + let matches = info.match(/tvg\-name\=\"(.*?)\"/i) + + return (matches && matches.length > 1) ? matches[1] : '' +} + +function parseTvgLogo(item) { + let info = getInfo(item) + let matches = info.match(/tvg\-logo\=\"(.*?)\"/i) + + return (matches && matches.length > 1) ? matches[1] : '' +} + +function parseGroupTitle(item) { + let info = getInfo(item) + let matches = info.match(/group\-title\=\"(.*?)\"/i) + + return (matches && matches.length > 1) ? matches[1] : '' +} + +function byTitle(a, b) { + var nameA = a.title.toLowerCase() + var nameB = b.title.toLowerCase() + if (nameA < nameB) { + return -1 + } + if (nameA > nameB) { + return 1 + } + + return 0 +} \ No newline at end of file diff --git a/package.json b/package.json index 36765c1e1..7a55d86c4 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "main": "index.m3u", "scripts": { "test": "node test/index.js", - "generate": "node helpers/generate.js" + "generate": "node helpers/generate.js", + "format": "node helpers/formatter.js" }, "repository": { "type": "git",