1
0
Fork 0

parseM3U tested

This commit is contained in:
boyska 2021-12-26 19:59:45 +01:00
parent 1fba07bd67
commit f55ccec939
2 changed files with 77 additions and 20 deletions

View file

@ -111,35 +111,39 @@ async function get (siteurl, options) {
} catch (e) {
true
}
if(resp !== null) {
try {
text = await resp.text()
if(resp !== null) {
try {
text = await resp.text()
const data = JSON.parse(text)
const name = data['icy-name']
if (name !== undefined) {
manifest.setName(name)
}
} catch (e) {
if (e instanceof SyntaxError) {
true
} else {
console.error('Error', e)
throw e
}
}
}
const data = JSON.parse(text)
const name = data['icy-name']
if (name !== undefined) {
manifest.setName(name)
}
} catch (e) {
if (e instanceof SyntaxError) {
true
} else {
console.error('Error', e)
throw e
}
}
}
// XXX: in base alle options fai fetch anche di altra roba
return manifest
}
function parseM3U (body) {
body.split('\n').filter((e) => {
if (e.startsWith('#')) {
return body.split('\n').filter((line) => {
if (line.startsWith('#')) {
return false
} else {
try { new URL(e); return true } catch { return false }
try {
new URL(line); return true
} catch {
return false
}
}
})
}

53
test/parser-M3U.test.js Normal file
View file

@ -0,0 +1,53 @@
const parseM3U = require('../radiomanifest.js').parsers.M3U
const chai = require('chai')
chai.use(require('chai-as-promised'))
const assert = chai.assert
const expect = chai.expect
describe('parseM3U parses basic M3U', () => {
describe('empty M3U', () => {
it('should return empty list', () => {
var r = parseM3U("")
assert.equal(r.length, 0)
})
it('should discard empty lines', () => {
var r = parseM3U("\n\n\n")
assert.equal(r.length, 0)
})
})
describe('just some lines', () => {
it('should return appropriate list', () => {
var r = parseM3U("http://foo")
assert.equal(r.length, 1)
assert.equal(r[0], "http://foo")
})
it('should work with longer list', () => {
var r = parseM3U("http://foo\nhttp://baz\nhttp://bar\n")
assert.equal(r.length, 3)
assert.equal(r[0], "http://foo")
assert.equal(r[1], "http://baz")
assert.equal(r[2], "http://bar")
})
it('should discard empty lines', () => {
var r = parseM3U("http://foo\n\nhttp://baz\nhttp://bar\n\n\n")
assert.equal(r.length, 3)
assert.equal(r[0], "http://foo")
assert.equal(r[1], "http://baz")
assert.equal(r[2], "http://bar")
})
})
describe('comments', () => {
it('comments should be ignored', () => {
var r = parseM3U("http://foo\n#asd")
assert.equal(r.length, 1)
assert.equal(r[0], "http://foo")
})
it('mid-line hash is not a comment', () => {
var r = parseM3U("http://foo#asd")
assert.equal(r.length, 1)
assert.equal(r[0], "http://foo#asd")
})
})
})