Update update-playlist.js

This commit is contained in:
Aleksandr Statciuk 2022-02-07 07:00:48 +03:00
parent c2b557c34c
commit a728d3d5a5
2 changed files with 19 additions and 20 deletions

View File

@ -1,15 +1,18 @@
const _ = require('lodash')
const { generator, db, logger } = require('../core')
const { create: createPlaylist } = require('../core/playlist')
const { db, logger, file } = require('../core')
async function main() {
let items = await db
await db.streams.load()
let items = await db.streams
.find({})
.sort({ name: 1, 'status.level': 1, 'resolution.height': -1, url: 1 })
const files = _.groupBy(items, 'filepath')
for (const filepath in files) {
const items = files[filepath]
await generator.saveAsM3U(filepath, items)
const playlist = createPlaylist(items, { public: false })
await file.create(filepath, playlist.toString())
}
}

View File

@ -1,33 +1,29 @@
const fs = require('fs-extra')
const path = require('path')
const glob = require('glob')
const { execSync } = require('child_process')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/temp')
fs.copyFileSync('tests/__data__/input/streams.db', 'tests/__data__/temp/streams.db')
fs.copyFileSync('tests/__data__/input/database/streams.db', 'tests/__data__/temp/streams.db')
const stdout = execSync(
'DB_FILEPATH=tests/__data__/temp/streams.db node scripts/commands/update-playlists.js',
{ encoding: 'utf8' }
)
const stdout = execSync('DB_DIR=tests/__data__/temp node scripts/commands/update-playlists.js', {
encoding: 'utf8'
})
})
it('can update playlist', () => {
expect(content('tests/__data__/output/channels/ad.m3u')).toBe(
content('tests/__data__/expected/channels/ad.m3u')
)
it('can update playlists', () => {
const files = glob
.sync('tests/__data__/expected/channels/*.m3u')
.map(f => f.replace('tests/__data__/expected/', ''))
expect(content('tests/__data__/output/channels/ru.m3u')).toBe(
content('tests/__data__/expected/channels/ru.m3u')
)
expect(content('tests/__data__/output/channels/uk.m3u')).toBe(
content('tests/__data__/expected/channels/uk.m3u')
)
files.forEach(filepath => {
expect(content(`output/${filepath}`), filepath).toBe(content(`expected/${filepath}`))
})
})
function content(filepath) {
return fs.readFileSync(path.resolve(filepath), {
return fs.readFileSync(`tests/__data__/${filepath}`, {
encoding: 'utf8'
})
}