SHARE
TWEET

Untitled

a guest Jul 21st, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { from, Observable, empty, bindNodeCallback } = require("rxjs")
  2. const { mergeMap, filter, expand, map, reduce } = require("rxjs/operators")
  3. const fs = require("fs")
  4. const path = require("path")
  5.  
  6. const readFile = bindNodeCallback(fs.readFile)
  7.  
  8. const TRACK_PATH = `C:/Users/Linus/Downloads/Songs`
  9.  
  10. const ls = dir => from(fs.readdirSync(dir)).pipe(
  11.     mergeMap(name => new Observable(subscriber => {
  12.         const fullname = path.join(dir, name)
  13.         fs.stat(fullname, (e, st) => {
  14.             if (e) {
  15.                 return subscriber.error(e)
  16.             }
  17.             subscriber.next({ name: fullname, stats: st })
  18.             subscriber.complete()
  19.         })
  20.     }))
  21. )
  22.  
  23. const HEADER_SEPARATOR = Buffer.from([ 0x0D, 0x0A, 0x0D, 0x0A ])
  24. const SEPARATOR = Buffer.from([ 0x3B, 0x0D, 0x0A ])
  25. const FIELD_SEPARATOR = 0x3A
  26. const HASH_POUND = 0x23
  27.  
  28. const readSMinfo = filename => readFile(filename).pipe(
  29.     mergeMap(buffer => new Observable(subscriber => {
  30.         let offset = buffer.indexOf(HASH_POUND) // skip utf-8 bom if it exists
  31.         const headerStop = buffer.indexOf(HEADER_SEPARATOR)
  32.         let index = buffer.indexOf(SEPARATOR, offset)
  33.         while (index !== -1 && index < headerStop) {
  34.             const field = buffer.slice(offset + 1, index)
  35.             const colonIndex = field.indexOf(FIELD_SEPARATOR)
  36.             subscriber.next([
  37.                 field.toString('utf8', 0, colonIndex),
  38.                 field.toString('utf8', colonIndex + 1)
  39.             ])
  40.             offset = index + 3
  41.             index = buffer.indexOf(SEPARATOR, offset)
  42.         }
  43.         subscriber.complete()
  44.     })),
  45.     reduce((obj, [key, value]) => {
  46.         obj[key] = value
  47.         return obj
  48.     }, {}),
  49.     map(info => {
  50.         let title = info.TITLE;
  51.         if (title === undefined) {
  52.             return null;
  53.         }
  54.         if (info.SUBTITLE !== undefined && info.SUBTITLE !== '') {
  55.             title += " " + info.SUBTITLE
  56.         }
  57.         if (info.ARTIST !== undefined && info.ARTIST !== '') {
  58.             title += " - " + info.ARTIST
  59.         }
  60.         return title
  61.     }),
  62.     filter(title => title !== null)
  63. )
  64.  
  65. ls(TRACK_PATH).pipe(
  66.     expand(f => f.stats.isDirectory() ? ls(f.name) : empty()),
  67.     filter(f => path.extname(f.name) === ".sm"),
  68.     map(f => f.name),
  69.     mergeMap(readSMinfo)
  70. )
  71. .subscribe(console.log)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top