Refactoring helper functions

This commit is contained in:
freearhey 2019-07-20 10:03:31 +03:00
parent 14659873d5
commit b7d6da3681
3 changed files with 141 additions and 156 deletions

View File

@ -1,81 +1,35 @@
const parsers = require('playlist-parser')
const M3U = parsers.M3U
const fs = require("fs")
const path = require('path')
const urlParser = require('url')
const util = require('./util')
let total = 0
const supportedGroups = [
'Auto',
'Business',
'CCTV',
'Classic',
'Comedy',
'Documentary',
'Education',
'Entertainment',
'Family',
'Fashion',
'Food',
'General',
'Health',
'History',
'Hobby',
'Kids',
'Legislative',
'Lifestyle',
'Local',
'Movies',
'Music',
'News',
'Quiz',
'Radio',
'Religious',
'Sci-Fi',
'Shop',
'Sport',
'Travel',
'Weather',
'XXX'
]
function init() {
let countries = loadPlaylist('index.m3u')
// countries = countries.slice(0, 4)
let countries = util.parsePlaylist('index.m3u')
countries = countries.slice(0, 1)
let channels = []
for(let country of countries) {
const playlist = loadPlaylist(country.file)
const playlist = util.parsePlaylist(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)
}
let channel = util.parseChannelData(item)
channels.push(channel)
}
channels = channels.sort(byTitle)
channels = util.sortByTitle(channels)
let outputPath = path.resolve(__dirname) + '/../' + country.file
fs.writeFileSync(outputPath, '#EXTM3U\n')
util.createFile(country.file)
channels.forEach(channel => {
writeToFile(outputPath, channel)
})
const info = `-1 tvg-id="${channel.id}" tvg-name="${channel.name}" tvg-logo="${channel.logo}" group-title="${channel.group}",${channel.title}`
// console.log(country.title, channels)
util.writeToFile(country.file, info, channel.file)
})
total += channels.length
@ -86,73 +40,3 @@ function init() {
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)
let groupTitle = (matches && matches.length > 1) ? matches[1] : ''
let groupIndex = supportedGroups.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
if(groupIndex === -1) {
groupTitle = ''
} else {
groupTitle = supportedGroups[groupIndex]
}
return groupTitle
}
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
}

View File

@ -1,44 +1,51 @@
const parsers = require('playlist-parser')
const M3U = parsers.M3U
const fs = require("fs")
const path = require('path')
const util = require('./util')
const urlParser = require('url')
let outputPath = path.resolve(__dirname) + '/../index.all.m3u'
let channels = 0
let duplicates = 0
const outputFile = 'index.full.m3u'
const debug = false
let cache = {}
fs.writeFileSync(outputPath, '#EXTM3U\n')
let stats = {
total: 0,
duplicates: 0
}
function init() {
let countries = loadPlaylist('index.m3u')
// countries = countries.slice(0, 2)
let countries = util.parsePlaylist('index.m3u')
if(debug) {
countries = countries.slice(0, 1)
}
util.createFile(outputFile)
for(let country of countries) {
const playlist = loadPlaylist(country.file)
const countryName = util.getTitle(country.title)
const countryCode = util.getBasename(country.file).toUpperCase()
const playlist = util.parsePlaylist(country.file)
for(let item of playlist) {
let title = (item.artist) ? item.length + ',' + item.artist + '-' + item.title : item.title
const channel = util.parseChannelData(item)
const groupTitle = [ countryName, channel.group ].filter(i => i).join(';')
let url = item.file
const info = `-1 tvg-id="${channel.id}" tvg-name="${channel.name}" tvg-logo="${channel.logo}" group-title="${groupTitle}",${channel.title}`
const file = channel.file
if(checkCache(url)) {
if(checkCache(file)) {
duplicates++
stats.duplicates++
} else {
writeToFile(title, url)
addToCache(url)
util.writeToFile(outputFile, info, file)
addToCache(file)
}
channels++
stats.total++
}
@ -47,16 +54,7 @@ function init() {
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')
}
console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unique: ${stats.total - stats.duplicates}`)
function addToCache(url) {
let id = getUrlPath(url)

103
helpers/util.js Normal file
View File

@ -0,0 +1,103 @@
const parsers = require('playlist-parser')
const M3U = parsers.M3U
const fs = require("fs")
const path = require('path')
function parsePlaylist(filename) {
return M3U.parse(fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" }))
}
function parseChannelData(item) {
const info = getInfo(item)
function getTvgId(info) {
const matches = info.match(/tvg\-id\=\"(.*?)\"/i)
return (matches && matches.length > 1) ? matches[1] : ''
}
function getTvgName(info) {
const matches = info.match(/tvg\-name\=\"(.*?)\"/i)
return (matches && matches.length > 1) ? matches[1] : ''
}
function getTvgLogo(info) {
const matches = info.match(/tvg\-logo\=\"(.*?)\"/i)
return (matches && matches.length > 1) ? matches[1] : ''
}
function getGroupTitle(item) {
const supportedGroups = [ 'Auto','Business', 'CCTV', 'Classic','Comedy','Documentary','Education','Entertainment', 'Family','Fashion','Food', 'General', 'Health', 'History', 'Hobby', 'Kids', 'Legislative','Lifestyle','Local', 'Movies', 'Music', 'News', 'Quiz','Radio', 'Religious','Sci-Fi', 'Shop', 'Sport', 'Travel', 'Weather', 'XXX' ]
const matches = info.match(/group\-title\=\"(.*?)\"/i)
let groupTitle = (matches && matches.length > 1) ? matches[1] : ''
const groupIndex = supportedGroups.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
if(groupIndex === -1) {
groupTitle = ''
} else {
groupTitle = supportedGroups[groupIndex]
}
return groupTitle
}
return {
title: getTitle(info),
file: item.file,
id: getTvgId(info),
name: getTvgName(info),
logo: getTvgLogo(info),
group: getGroupTitle(info)
}
}
function getInfo(item) {
return (item.artist) ? item.artist + '-' + item.title : item.title
}
function getTitle(info) {
const parts = info.split(',')
return parts[parts.length - 1].trim()
}
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
}
function sortByTitle(arr) {
return arr.sort(byTitle)
}
function writeToFile(filename, info, file) {
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, '#EXTINF:' + info + '\n' + file + '\n')
}
function createFile(filename) {
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, '#EXTM3U\n')
}
function getBasename(filename) {
return path.basename(filename, path.extname(filename))
}
module.exports = {
parsePlaylist,
parseChannelData,
getTitle,
sortByTitle,
writeToFile,
createFile,
getBasename
}