1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
'use strict'
const fs = require('fs')
const { getCrawlerByName } = require('./crawlers')
const { isGroup, filterTracks, sourceSymbol } = require('./playlist-utils')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
async function processSmartPlaylist(item) {
// Object.assign is used so that we keep original properties, e.g. "name"
// or "apply". (It's also used so we return copies of original objects.)
const newItem = Object.assign({}, item)
if ('source' in newItem) {
const [ name, ...args ] = item.source
const crawlModule = getCrawlerByName(name)
if (crawlModule) {
const { crawl } = crawlModule
Object.assign(newItem, await crawl(...args))
} else {
console.error(`No crawler by name ${name} - skipped item:`, item)
newItem.failed = true
}
delete newItem.source
} else if ('items' in newItem) {
newItem.items = await Promise.all(item.items.map(processSmartPlaylist))
}
if ('filters' in newItem) filters: {
if (!isGroup(newItem)) {
console.warn('Filter on non-group (no effect):', newItem)
break filters
}
newItem.filters = newItem.filters.filter(filter => {
if ('tag' in filter === false) {
console.warn('Filter is missing "tag" property (skipping this filter):', filter)
return false
}
return true
})
Object.assign(newItem, filterTracks(newItem, track => {
for (const filter of newItem.filters) {
const { tag } = filter
let value = track
for (const key of tag.split('.')) {
if (key in Object(value)) {
value = value[key]
} else {
console.warn(`In tag "${tag}", key "${key}" not found.`)
console.warn('...value until now:', value)
console.warn('...track:', track)
console.warn('...filter:', filter)
return false
}
}
if ('gt' in filter && value <= filter.gt) return false
if ('lt' in filter && value >= filter.lt) return false
if ('gte' in filter && value < filter.gte) return false
if ('lte' in filter && value > filter.lte) return false
if ('least' in filter && value < filter.least) return false
if ('most' in filter && value > filter.most) return false
if ('min' in filter && value < filter.min) return false
if ('max' in filter && value > filter.max) return false
for (const prop of ['includes', 'contains']) {
if (prop in filter) {
if (Array.isArray(value) || typeof value === 'string') {
if (!value.includes(filter.includes)) return false
} else {
console.warn(
`Value of tag "${tag}" is not an array or string, so passing ` +
`"${prop}" does not make sense.`
)
console.warn('...value:', value)
console.warn('...track:', track)
console.warn('...filter:', filter)
return false
}
}
}
if (filter.regex) {
if (typeof value === 'string') {
let re
try {
re = new RegExp(filter.regex)
} catch (error) {
console.warn('Invalid regular expression:', re)
console.warn('...error message:', error.message)
console.warn('...filter:', filter)
return false
}
if (!re.test(value)) return false
} else {
console.warn(
`Value of tag "${tag}" is not a string, so passing "regex" ` +
'does not make sense.'
)
console.warn('...value:', value)
console.warn('...track:', track)
console.warn('...filter:', filter)
return false
}
}
}
return true
}))
delete newItem.filters
}
return newItem
}
async function main(opts) {
// TODO: Error when no file is given
if (opts.length === 0) {
console.log("Usage: smart-playlist /path/to/playlist")
} else {
const playlist = JSON.parse(await readFile(opts[0]))
console.log(JSON.stringify(await processSmartPlaylist(playlist), null, 2))
}
}
module.exports = Object.assign(main, {processSmartPlaylist})
if (require.main === module) {
main(process.argv.slice(2))
.catch(err => console.error(err))
}
|