Update generate.js

This commit is contained in:
Aleksandr Statciuk 2021-09-17 14:25:23 +03:00
parent 11cc75fa2a
commit fb427d5bd3
1 changed files with 58 additions and 33 deletions

View File

@ -36,13 +36,16 @@ function createNoJekyllFile() {
function generateIndex() { function generateIndex() {
log.print('Generating index.m3u...\n') log.print('Generating index.m3u...\n')
const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().removeOffline().get()
const guides = channels.map(channel => channel.tvg.url)
const filename = `${ROOT_DIR}/index.m3u` const filename = `${ROOT_DIR}/index.m3u`
file.create(filename, '#EXTM3U\n') const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n`)
const nsfwFilename = `${ROOT_DIR}/index.nsfw.m3u` const nsfwFilename = `${ROOT_DIR}/index.nsfw.m3u`
file.create(nsfwFilename, '#EXTM3U\n') file.create(nsfwFilename, `#EXTM3U url-tvg="${urlTvg}"\n`)
const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().removeOffline().get()
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { if (!channel.isNSFW()) {
file.append(filename, channel.toString()) file.append(filename, channel.toString())
@ -53,14 +56,17 @@ function generateIndex() {
function generateCategoryIndex() { function generateCategoryIndex() {
log.print('Generating index.category.m3u...\n') log.print('Generating index.category.m3u...\n')
const filename = `${ROOT_DIR}/index.category.m3u`
file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['category', 'name', 'url']) .sortBy(['category', 'name', 'url'])
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
.get() .get()
const guides = channels.map(channel => channel.tvg.url)
const filename = `${ROOT_DIR}/index.category.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n`)
for (const channel of channels) { for (const channel of channels) {
file.append(filename, channel.toString()) file.append(filename, channel.toString())
} }
@ -68,50 +74,56 @@ function generateCategoryIndex() {
function generateCountryIndex() { function generateCountryIndex() {
log.print('Generating index.country.m3u...\n') log.print('Generating index.country.m3u...\n')
const filename = `${ROOT_DIR}/index.country.m3u`
file.create(filename, '#EXTM3U\n')
const guides = []
const lines = []
for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) { for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.forCountry(country) .forCountry(country)
.removeDuplicates() .removeDuplicates()
.removeNSFW()
.removeOffline() .removeOffline()
.get() .get()
for (const channel of channels) { for (const channel of channels) {
const groupTitle = channel.group.title const groupTitle = channel.group.title
const nsfw = channel.isNSFW()
channel.group.title = country.name || '' channel.group.title = country.name || ''
if (!nsfw) { lines.push(channel.toString())
file.append(filename, channel.toString())
}
channel.group.title = groupTitle channel.group.title = groupTitle
guides.push(channel.tvg.url)
} }
} }
const filename = `${ROOT_DIR}/index.country.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n${lines.join('')}`)
} }
function generateLanguageIndex() { function generateLanguageIndex() {
log.print('Generating index.language.m3u...\n') log.print('Generating index.language.m3u...\n')
const filename = `${ROOT_DIR}/index.language.m3u`
file.create(filename, '#EXTM3U\n')
const guides = []
const lines = []
for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) { for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.forLanguage(language) .forLanguage(language)
.removeDuplicates() .removeDuplicates()
.removeNSFW()
.removeOffline() .removeOffline()
.get() .get()
for (const channel of channels) { for (const channel of channels) {
const groupTitle = channel.group.title const groupTitle = channel.group.title
const nsfw = channel.isNSFW()
channel.group.title = language.name || '' channel.group.title = language.name || ''
if (!nsfw) { lines.push(channel.toString())
file.append(filename, channel.toString())
}
channel.group.title = groupTitle channel.group.title = groupTitle
guides.push(channel.tvg.url)
} }
} }
const filename = `${ROOT_DIR}/index.language.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n${lines.join('')}`)
} }
function generateCategories() { function generateCategories() {
@ -120,15 +132,17 @@ function generateCategories() {
file.createDir(outputDir) file.createDir(outputDir)
for (const category of [...db.categories.all(), { id: 'other' }]) { for (const category of [...db.categories.all(), { id: 'other' }]) {
const filename = `${outputDir}/${category.id}.m3u`
file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.forCategory(category) .forCategory(category)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
.get() .get()
const guides = channels.map(channel => channel.tvg.url)
const filename = `${outputDir}/${category.id}.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n`)
for (const channel of channels) { for (const channel of channels) {
file.append(filename, channel.toString()) file.append(filename, channel.toString())
} }
@ -141,19 +155,20 @@ function generateCountries() {
file.createDir(outputDir) file.createDir(outputDir)
for (const country of [...db.countries.all(), { code: 'undefined' }]) { for (const country of [...db.countries.all(), { code: 'undefined' }]) {
const filename = `${outputDir}/${country.code}.m3u`
file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.forCountry(country) .forCountry(country)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
.removeNSFW()
.get() .get()
const guides = channels.map(channel => channel.tvg.url)
const filename = `${outputDir}/${country.code}.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n`)
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { file.append(filename, channel.toString())
file.append(filename, channel.toString())
}
} }
} }
} }
@ -164,19 +179,20 @@ function generateLanguages() {
file.createDir(outputDir) file.createDir(outputDir)
for (const language of [...db.languages.all(), { code: 'undefined' }]) { for (const language of [...db.languages.all(), { code: 'undefined' }]) {
const filename = `${outputDir}/${language.code}.m3u`
file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.forLanguage(language) .forLanguage(language)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
.removeNSFW()
.get() .get()
const guides = channels.map(channel => channel.tvg.url)
const filename = `${outputDir}/${language.code}.m3u`
const urlTvg = generateUrlTvg(guides)
file.create(filename, `#EXTM3U url-tvg="${urlTvg}"\n`)
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { file.append(filename, channel.toString())
file.append(filename, channel.toString())
}
} }
} }
} }
@ -197,4 +213,13 @@ function showResults() {
) )
} }
function generateUrlTvg(guides) {
const output = guides.reduce((acc, curr) => {
if (curr && !acc.includes(curr)) acc.push(curr)
return acc
}, [])
return output.sort().join(',')
}
main() main()