Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { from, Observable, empty, bindNodeCallback } = require("rxjs")
- const { mergeMap, filter, expand, map, reduce } = require("rxjs/operators")
- const fs = require("fs")
- const path = require("path")
- const readFile = bindNodeCallback(fs.readFile)
- const TRACK_PATH = `C:/Users/Linus/Downloads/Songs`
- const ls = dir => from(fs.readdirSync(dir)).pipe(
- mergeMap(name => new Observable(subscriber => {
- const fullname = path.join(dir, name)
- fs.stat(fullname, (e, st) => {
- if (e) {
- return subscriber.error(e)
- }
- subscriber.next({ name: fullname, stats: st })
- subscriber.complete()
- })
- }))
- )
- const HEADER_SEPARATOR = Buffer.from([ 0x0D, 0x0A, 0x0D, 0x0A ])
- const SEPARATOR = Buffer.from([ 0x3B, 0x0D, 0x0A ])
- const FIELD_SEPARATOR = 0x3A
- const HASH_POUND = 0x23
- const readSMinfo = filename => readFile(filename).pipe(
- mergeMap(buffer => new Observable(subscriber => {
- let offset = buffer.indexOf(HASH_POUND) // skip utf-8 bom if it exists
- const headerStop = buffer.indexOf(HEADER_SEPARATOR)
- let index = buffer.indexOf(SEPARATOR, offset)
- while (index !== -1 && index < headerStop) {
- const field = buffer.slice(offset + 1, index)
- const colonIndex = field.indexOf(FIELD_SEPARATOR)
- subscriber.next([
- field.toString('utf8', 0, colonIndex),
- field.toString('utf8', colonIndex + 1)
- ])
- offset = index + 3
- index = buffer.indexOf(SEPARATOR, offset)
- }
- subscriber.complete()
- })),
- reduce((obj, [key, value]) => {
- obj[key] = value
- return obj
- }, {}),
- map(info => {
- let title = info.TITLE;
- if (title === undefined) {
- return null;
- }
- if (info.SUBTITLE !== undefined && info.SUBTITLE !== '') {
- title += " " + info.SUBTITLE
- }
- if (info.ARTIST !== undefined && info.ARTIST !== '') {
- title += " - " + info.ARTIST
- }
- return title
- }),
- filter(title => title !== null)
- )
- ls(TRACK_PATH).pipe(
- expand(f => f.stats.isDirectory() ? ls(f.name) : empty()),
- filter(f => path.extname(f.name) === ".sm"),
- map(f => f.name),
- mergeMap(readSMinfo)
- )
- .subscribe(console.log)
Advertisement
Add Comment
Please, Sign In to add comment