Update create-database.js

This commit is contained in:
Aleksandr Statciuk 2022-02-05 07:25:56 +03:00
parent fa4bad3dca
commit 46deb7f004
8 changed files with 34 additions and 27 deletions

View File

@ -50,7 +50,7 @@ async function saveToDatabase() {
for (const item of chunk) {
const stream = store.create()
stream.set('id', { id: item.tvg.id })
stream.set('display_name', { display_name: item.name })
stream.set('title', { title: item.name })
stream.set('filepath', { filepath: item.filepath })
stream.set('resolution', { title: item.name })
stream.set('status', { title: item.name })

View File

@ -1,11 +1,12 @@
const file = require('./file')
const parser = require('./parser')
const transliteration = require('transliteration')
const cid = {}
cid.generate = function (title, filepath) {
const name = parseChannelName(title)
const code = parseCountryCode(filepath)
const name = parser.parseChannelName(title)
const code = parser.parseCountryCode(filepath)
if (name && code) {
const slug = transliteration
@ -20,22 +21,3 @@ cid.generate = function (title, filepath) {
}
module.exports = cid
function parseCountryCode(filepath) {
if (!filepath) return null
const basename = file.basename(filepath)
const [code] = basename.split('_') || [null]
return code
}
function parseChannelName(title) {
return title
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}

View File

@ -28,4 +28,23 @@ parser.parseNumber = function (string) {
return parsed
}
parser.parseChannelName = function (string) {
return string
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}
parser.parseCountryCode = function (filepath) {
if (!filepath) return null
const basename = file.basename(filepath)
const [code] = basename.split('_') || [null]
return code
}
module.exports = parser

View File

@ -2,3 +2,4 @@ exports.is_broken = require('./is_broken')
exports.resolution = require('./resolution')
exports.status = require('./status')
exports.url = require('./url')
exports.title = require('./title')

View File

@ -0,0 +1,5 @@
const { parser } = require('../../core')
module.exports = function ({ title }) {
return parser.parseChannelName(title)
}

View File

@ -1,2 +0,0 @@
{"display_name":"ATV (720p) [Offline]","id":"ATV.ad","filepath":"tests/__data__/input/channels/ad.m3u","resolution":{"height":720,"width":null},"status":{"label":"Offline","code":"offline","level":5},"url":"https://iptv-all.lanesh4d0w.repl.co/andorra/atv","http":{"referrer":"","user-agent":""},"is_broken":true,"updated":false,"cluster_id":1,"_id":"verufR2ehwdsfou3"}
{"display_name":"Fox Sports 2 Asia (Thai) (720p)","id":"FoxSports2AsiaThai.us","filepath":"tests/__data__/input/channels/us_blocked.m3u","resolution":{"height":720,"width":null},"status":{"label":"","code":"online","level":1},"url":"https://example.com/playlist.m3u8","http":{"referrer":"","user-agent":""},"is_broken":false,"updated":true,"cluster_id":1,"_id":"sLG04kZhqlEcYc25"}

View File

@ -0,0 +1,2 @@
{"title":"ATV","id":"ATV.ad","filepath":"tests/__data__/input/channels/ad.m3u","resolution":{"height":720,"width":null},"status":{"label":"Offline","code":"offline","level":5},"url":"https://iptv-all.lanesh4d0w.repl.co/andorra/atv","http":{"referrer":"","user-agent":""},"is_broken":true,"updated":false,"cluster_id":1,"_id":"verufR2ehwdsfou3"}
{"title":"Fox Sports 2 Asia (Thai)","id":"FoxSports2AsiaThai.us","filepath":"tests/__data__/input/channels/us_blocked.m3u","resolution":{"height":720,"width":null},"status":{"label":"","code":"online","level":1},"url":"https://example.com/playlist.m3u8","http":{"referrer":"","user-agent":""},"is_broken":false,"updated":true,"cluster_id":1,"_id":"sLG04kZhqlEcYc25"}

View File

@ -6,14 +6,14 @@ beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
const stdout = execSync(
'DB_FILEPATH=tests/__data__/output/channels.db node scripts/commands/create-database.js --input-dir=tests/__data__/input/channels --max-clusters=1',
'DB_FILEPATH=tests/__data__/output/streams.db node scripts/commands/create-database.js --input-dir=tests/__data__/input/channels --max-clusters=1',
{ encoding: 'utf8' }
)
})
it('can create database', () => {
let output = content('tests/__data__/output/channels.db')
let expected = content('tests/__data__/expected/channels.db')
let output = content('tests/__data__/output/streams.db')
let expected = content('tests/__data__/expected/streams.db')
output = output.map(i => {
i._id = null