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 _ = require('lodash')
const { generator, db, logger } = require('../core') const { create: createPlaylist } = require('../core/playlist')
const { db, logger, file } = require('../core')
async function main() { async function main() {
let items = await db await db.streams.load()
let items = await db.streams
.find({}) .find({})
.sort({ name: 1, 'status.level': 1, 'resolution.height': -1, url: 1 }) .sort({ name: 1, 'status.level': 1, 'resolution.height': -1, url: 1 })
const files = _.groupBy(items, 'filepath') const files = _.groupBy(items, 'filepath')
for (const filepath in files) { for (const filepath in files) {
const items = files[filepath] 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 fs = require('fs-extra')
const path = require('path') const path = require('path')
const glob = require('glob')
const { execSync } = require('child_process') const { execSync } = require('child_process')
beforeEach(() => { beforeEach(() => {
fs.emptyDirSync('tests/__data__/temp') 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( const stdout = execSync('DB_DIR=tests/__data__/temp node scripts/commands/update-playlists.js', {
'DB_FILEPATH=tests/__data__/temp/streams.db node scripts/commands/update-playlists.js', encoding: 'utf8'
{ encoding: 'utf8' } })
)
}) })
it('can update playlist', () => { it('can update playlists', () => {
expect(content('tests/__data__/output/channels/ad.m3u')).toBe( const files = glob
content('tests/__data__/expected/channels/ad.m3u') .sync('tests/__data__/expected/channels/*.m3u')
) .map(f => f.replace('tests/__data__/expected/', ''))
expect(content('tests/__data__/output/channels/ru.m3u')).toBe( files.forEach(filepath => {
content('tests/__data__/expected/channels/ru.m3u') expect(content(`output/${filepath}`), filepath).toBe(content(`expected/${filepath}`))
) })
expect(content('tests/__data__/output/channels/uk.m3u')).toBe(
content('tests/__data__/expected/channels/uk.m3u')
)
}) })
function content(filepath) { function content(filepath) {
return fs.readFileSync(path.resolve(filepath), { return fs.readFileSync(`tests/__data__/${filepath}`, {
encoding: 'utf8' encoding: 'utf8'
}) })
} }