@sequencemedia/music-library-parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # @sequencemedia/itunes-library-parser
2
+
3
+ JavaScript functions and XSL stylesheets to parse an Apple Music `Library.xml` file and transform it to [`m3u`](https://en.wikipedia.org/wiki/M3U) files, JSON, or JavaScript.
4
+
5
+ Requires [Java](https://www.oracle.com/java/technologies/javase-downloads.html) and [Saxon PE](https://www.saxonica.com/welcome/welcome.xml).
6
+
7
+ ## Library
8
+
9
+ Transforms the entire library.
10
+
11
+ ```javascript
12
+ import { toM3U } from './src/js/library/index.mjs'
13
+ import {
14
+ toJSON,
15
+ toJS,
16
+ toES
17
+ } from './src/js/library/transform/index.mjs'
18
+ ```
19
+
20
+ ### `toM3U`
21
+
22
+ Requires the arguments `jar`, `xml`, and `destination`.
23
+
24
+ - `jar` - the path to the Saxon binary on your device
25
+ - `xml` - the path to the Apple Music `Library.xml` file
26
+ - `destination` - the path for the `m3u` files to be written
27
+
28
+ Returns a `Promise` resolving when all `m3u` files are written.
29
+
30
+ ### `toJSON`
31
+
32
+ Requires the arguments `jar`, and `xml`.
33
+
34
+ - `jar` - the path to the Saxon binary on your device
35
+ - `xml` - the path to the Apple Music `Library.xml` file
36
+
37
+ Returns a `Promise` resolving to a `JSON` string.
38
+
39
+ ### `toJS`
40
+
41
+ Requires the arguments `jar`, and `xml`.
42
+
43
+ - `jar` - the path to the Saxon binary on your device
44
+ - `xml` - the path to the Apple Music `Library.xml` file
45
+
46
+ Returns a `Promise` resolving to a JavaScript object.
47
+
48
+ ### `toES`
49
+
50
+ Requires the arguments `jar`, and `xml`.
51
+
52
+ - `jar` - the path to the Saxon binary on your device
53
+ - `xml` - the path to the Apple Music `Library.xml` file
54
+
55
+ Returns a `Promise` resolving to a collection of JavaScript `Map` and `Set` instances.
56
+
57
+ ## Playlists
58
+
59
+ Transforms the playlists.
60
+
61
+ ```javascript
62
+ import { toM3U } from './src/js/library/playlists/index.mjs'
63
+ import {
64
+ toJSON,
65
+ toJS,
66
+ toES
67
+ } from './src/js/library/playlists/transform/index.mjs'
68
+ ```
69
+
70
+ See **Library**.
71
+
72
+ ## Tracks
73
+
74
+ Transforms the tracks.
75
+
76
+ ```javascript
77
+ import { toM3U } from './src/js/library/tracks/index.mjs'
78
+ import {
79
+ toJSON,
80
+ toJS,
81
+ toES
82
+ } from './src/js/library/tracks/transform/index.mjs'
83
+ ```
84
+
85
+ See **Library**.
@@ -0,0 +1,24 @@
1
+ const presets = [
2
+ [
3
+ '@babel/env',
4
+ {
5
+ targets: {
6
+ node: 'current'
7
+ },
8
+ useBuiltIns: 'usage',
9
+ corejs: 3
10
+ }
11
+ ]
12
+ ]
13
+
14
+ const plugins = [
15
+ '@babel/proposal-export-default-from',
16
+ '@babel/proposal-export-namespace-from'
17
+ ]
18
+
19
+ module.exports = {
20
+ compact: true,
21
+ comments: false,
22
+ presets,
23
+ plugins
24
+ }
package/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ //
2
+ import * as musicLibraryParser from '#music-library-parser'
3
+
4
+ export default musicLibraryParser
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@sequencemedia/music-library-parser",
3
+ "version": "1.0.0",
4
+ "description": "Parse Apple Music Library XML to JSON, JS, ES, or M3U",
5
+ "keywords": [
6
+ "music",
7
+ "library",
8
+ "xml",
9
+ "parser",
10
+ "music",
11
+ "audio",
12
+ "video",
13
+ "track",
14
+ "playlist",
15
+ "json",
16
+ "m3u",
17
+ "m3u8"
18
+ ],
19
+ "main": "./index.mjs",
20
+ "type": "module",
21
+ "author": "Jonathan Perry for Sequence Media Limited <sequencemedia@sequencemedia.net>",
22
+ "license": "MIT",
23
+ "engines": {
24
+ "node": ">=16.13.0"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git://github.com/sequencemedia/music-library-parser"
29
+ },
30
+ "homepage": "https://github.com/sequencemedia/music-library-parser",
31
+ "bugs": {
32
+ "url": "https://github.com/sequencemedia/music-library-parser/issues"
33
+ },
34
+ "scripts": {
35
+ "lint": "eslint . --ext .mjs --ext .cjs",
36
+ "lint:fix": "npm run lint -- --fix",
37
+ "start": "cross-env DEBUG=@sequencemedia/music-library-parser* NODE_ENV=production node .",
38
+ "dev": "cross-env DEBUG=@sequencemedia/music-library-parser* NODE_ENV=development node ."
39
+ },
40
+ "dependencies": {
41
+ "cross-env": "^7.0.3",
42
+ "debug": "^4.3.3",
43
+ "rimraf": "^3.0.2",
44
+ "supports-color": "^9.2.1"
45
+ },
46
+ "devDependencies": {
47
+ "@babel/cli": "^7.16.0",
48
+ "@babel/core": "^7.16.5",
49
+ "@babel/eslint-parser": "^7.16.5",
50
+ "@babel/plugin-proposal-export-default-from": "^7.16.5",
51
+ "@babel/plugin-proposal-export-namespace-from": "^7.16.5",
52
+ "@babel/preset-env": "^7.16.5",
53
+ "core-js": "^3.20.1",
54
+ "eslint": "7.32.0",
55
+ "eslint-config-standard": "^16.0.3",
56
+ "eslint-plugin-import": "^2.25.3",
57
+ "eslint-plugin-node": "^11.1.0",
58
+ "eslint-plugin-promise": "5.2.0"
59
+ },
60
+ "imports": {
61
+ "#music-library-parser": "./src/js/index.mjs",
62
+ "#music-library-parser/library": "./src/js/library/index.mjs",
63
+ "#music-library-parser/library/transform": "./src/js/library/transform/index.mjs",
64
+ "#music-library-parser/library/playlists": "./src/js/library/playlists/index.mjs",
65
+ "#music-library-parser/library/playlists/transform": "./src/js/library/playlists/transform/index.mjs",
66
+ "#music-library-parser/library/tracks": "./src/js/library/tracks/index.mjs",
67
+ "#music-library-parser/library/tracks/transform": "./src/js/library/tracks/transform/index.mjs",
68
+ "#music-library-parser/transform": "./src/js/transform/index.mjs"
69
+ }
70
+ }
@@ -0,0 +1,10 @@
1
+ import path from 'path'
2
+ import rimraf from 'rimraf'
3
+
4
+ export const clear = (destination = './Music Library') => (
5
+ new Promise((resolve, reject) => {
6
+ rimraf(`${path.resolve(destination)}/*`, (e) => (!e) ? resolve() : reject(e))
7
+ })
8
+ )
9
+
10
+ export * as library from '#music-library-parser/library'
@@ -0,0 +1,122 @@
1
+ import {
2
+ exec
3
+ } from 'child_process'
4
+
5
+ import path from 'path'
6
+
7
+ import {
8
+ fileURLToPath
9
+ } from 'url'
10
+
11
+ import debug from 'debug'
12
+
13
+ import {
14
+ clear
15
+ } from '#music-library-parser'
16
+
17
+ const log = debug('@sequencemedia/music-library-parser:to-m3u')
18
+ const error = debug('@sequencemedia/music-library-parser:to-m3u:error')
19
+
20
+ const cwd = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..')
21
+ const xsl = path.resolve(cwd, 'src/xsl/library.xsl')
22
+
23
+ let immediate = null
24
+ const queue = []
25
+
26
+ export function parse (jar, xml, destination = './Music Library') {
27
+ return (
28
+ new Promise((resolve, reject) => {
29
+ exec(`java -jar "${path.resolve(jar)}" -s:"${path.resolve(xml)}" -xsl:"${xsl}" destination="${path.resolve(destination)}"`, { cwd }, (e) => (!e) ? resolve() : reject(e))
30
+ })
31
+ )
32
+ }
33
+
34
+ export async function toM3U (jar, xml, destination) {
35
+ /**
36
+ * Ignore these values if they are duplicates of some in the queue
37
+ */
38
+ if (!queue.some(({ jar: j, xml: x, destination: d }) => (
39
+ j === jar &&
40
+ x === xml &&
41
+ d === destination
42
+ ))) {
43
+ /**
44
+ * These values are unique, so
45
+ */
46
+ if (immediate) {
47
+ /**
48
+ * XML is being parsed. En-queue these values
49
+ */
50
+ queue.push({ jar, xml, destination })
51
+ /**
52
+ * (... if the XML changes while it's being parsed
53
+ * it should be parsed again immediately after,
54
+ * so the second of two identical calls will be
55
+ * put into the queue while the first is executing
56
+ *
57
+ * In other words: the first call isn't put into the
58
+ * queue in order to allow that second call to be
59
+ * put into the queue!)
60
+ */
61
+ } else {
62
+ /**
63
+ * XML is not being parsed
64
+ */
65
+ immediate = setImmediate(async () => {
66
+ try {
67
+ /**
68
+ * Clear the destination directory
69
+ */
70
+ await clear(destination)
71
+ /**
72
+ * Parse these values immediately
73
+ */
74
+ await parse(jar, xml, destination)
75
+
76
+ log(`Succeeded parsing "${xml}"`)
77
+
78
+ immediate = null
79
+
80
+ if (queue.length) {
81
+ /**
82
+ * The queue is not empty. De-queue some values
83
+ */
84
+ const {
85
+ jar: j,
86
+ xml: x,
87
+ destination: d
88
+ } = queue.shift()
89
+
90
+ /**
91
+ * Repeat
92
+ */
93
+ return toM3U(j, x, d)
94
+ }
95
+
96
+ /**
97
+ * Return handle from `setImmediate` or null
98
+ */
99
+ return immediate
100
+ } catch (e) {
101
+ const {
102
+ code = 'No error code defined'
103
+ } = e
104
+
105
+ if (code === 2) {
106
+ error('I/O error in Saxon', e)
107
+ } else {
108
+ const {
109
+ message = 'No error message defined'
110
+ } = e
111
+
112
+ error(code, message, e)
113
+ }
114
+ }
115
+ })
116
+ }
117
+ }
118
+ }
119
+
120
+ export * as tracks from '#music-library-parser/library/tracks'
121
+ export * as playlists from '#music-library-parser/library/playlists'
122
+ export * as transform from '#music-library-parser/library/transform'
@@ -0,0 +1,120 @@
1
+ import {
2
+ exec
3
+ } from 'child_process'
4
+
5
+ import path from 'path'
6
+
7
+ import {
8
+ fileURLToPath
9
+ } from 'url'
10
+
11
+ import debug from 'debug'
12
+
13
+ import {
14
+ clear
15
+ } from '#music-library-parser'
16
+
17
+ const log = debug('@sequencemedia/music-library-parser:to-m3u')
18
+ const error = debug('@sequencemedia/music-library-parser:to-m3u:error')
19
+
20
+ const cwd = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..')
21
+ const xsl = path.resolve(cwd, 'src/xsl/library/playlists.xsl')
22
+
23
+ let immediate = null
24
+ const queue = []
25
+
26
+ export function parse (jar, xml, destination = './Music Library') {
27
+ return (
28
+ new Promise((resolve, reject) => {
29
+ exec(`java -jar "${path.resolve(jar)}" -s:"${path.resolve(xml)}" -xsl:"${xsl}" destination="${path.resolve(destination)}"`, { cwd }, (e) => (!e) ? resolve() : reject(e))
30
+ })
31
+ )
32
+ }
33
+
34
+ export async function toM3U (jar, xml, destination) {
35
+ /**
36
+ * Ignore these values if they are duplicates of some in the queue
37
+ */
38
+ if (!queue.some(({ jar: j, xml: x, destination: d }) => (
39
+ j === jar &&
40
+ x === xml &&
41
+ d === destination
42
+ ))) {
43
+ /**
44
+ * These values are unique, so
45
+ */
46
+ if (immediate) {
47
+ /**
48
+ * XML is being parsed. En-queue these values
49
+ */
50
+ queue.push({ jar, xml, destination })
51
+ /**
52
+ * (... if the XML changes while it's being parsed
53
+ * it should be parsed again immediately after,
54
+ * so the second of two identical calls will be
55
+ * put into the queue while the first is executing
56
+ *
57
+ * In other words: the first call isn't put into the
58
+ * queue in order to allow that second call to be
59
+ * put into the queue!)
60
+ */
61
+ } else {
62
+ /**
63
+ * XML is not being parsed
64
+ */
65
+ immediate = setImmediate(async () => {
66
+ try {
67
+ /**
68
+ * Clear the destination directory
69
+ */
70
+ await clear(destination)
71
+ /**
72
+ * Parse these values immediately
73
+ */
74
+ await parse(jar, xml, destination)
75
+
76
+ log(`Succeeded parsing "${xml}"`)
77
+
78
+ immediate = null
79
+
80
+ if (queue.length) {
81
+ /**
82
+ * The queue is not empty. De-queue some values
83
+ */
84
+ const {
85
+ jar: j,
86
+ xml: x,
87
+ destination: d
88
+ } = queue.shift()
89
+
90
+ /**
91
+ * Repeat
92
+ */
93
+ return toM3U(j, x, d)
94
+ }
95
+
96
+ /**
97
+ * Return handle from `setImmediate` or null
98
+ */
99
+ return immediate
100
+ } catch (e) {
101
+ const {
102
+ code = 'No error code defined'
103
+ } = e
104
+
105
+ if (code === 2) {
106
+ error('I/O error in Saxon', e)
107
+ } else {
108
+ const {
109
+ message = 'No error message defined'
110
+ } = e
111
+
112
+ error(code, message, e)
113
+ }
114
+ }
115
+ })
116
+ }
117
+ }
118
+ }
119
+
120
+ export * as transform from '#music-library-parser/library/playlists/transform'
@@ -0,0 +1,82 @@
1
+ import {
2
+ exec
3
+ } from 'child_process'
4
+
5
+ import path from 'path'
6
+
7
+ import {
8
+ fileURLToPath
9
+ } from 'url'
10
+
11
+ import {
12
+ readFile
13
+ } from 'fs/promises'
14
+
15
+ import debug from 'debug'
16
+
17
+ import {
18
+ transform
19
+ } from '#music-library-parser/transform'
20
+
21
+ const error = debug('@sequencemedia/music-library-parser:to-json:error')
22
+
23
+ const cwd = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../..')
24
+ const xsl = path.resolve(cwd, 'src/xsl/library/tracks/to-json.xsl')
25
+ const DESTINATION = path.resolve(cwd, '.music-library/playlists.json')
26
+
27
+ /**
28
+ * `maxBuffer`
29
+ */
30
+ export function parse (jar, xml, destination = DESTINATION) {
31
+ return (
32
+ new Promise((resolve, reject) => {
33
+ exec(`java -jar "${path.resolve(jar)}" -s:"${path.resolve(xml)}" -xsl:"${xsl}" -o:"${destination}"`, { cwd }, (e) => (!e) ? resolve() : reject(e))
34
+ })
35
+ )
36
+ }
37
+
38
+ async function execute (jar, xml) {
39
+ await parse(jar, xml, DESTINATION)
40
+
41
+ return (
42
+ await readFile(DESTINATION, 'utf8')
43
+ )
44
+ }
45
+
46
+ export async function toJSON (jar, xml) {
47
+ try {
48
+ const fileData = await execute(jar, xml)
49
+
50
+ return fileData.toString('utf8')
51
+ } catch ({ message }) {
52
+ error(message)
53
+
54
+ throw new Error('Failed to process XML to JSON')
55
+ }
56
+ }
57
+
58
+ export async function toJS (jar, xml) {
59
+ try {
60
+ return JSON.parse(await toJSON(jar, xml))
61
+ } catch ({ message }) {
62
+ error(message)
63
+
64
+ throw new Error('Failed to process XML to JS')
65
+ }
66
+ }
67
+
68
+ export async function toES (jar, xml) {
69
+ try {
70
+ return transform(JSON.parse(await toJSON(jar, xml)))
71
+ } catch ({ message }) {
72
+ error(message)
73
+
74
+ throw new Error('Failed to process XML to ES')
75
+ }
76
+ }
77
+
78
+ export default {
79
+ toJSON,
80
+ toJS,
81
+ toES
82
+ }
@@ -0,0 +1,121 @@
1
+ import {
2
+ exec
3
+ } from 'child_process'
4
+
5
+ import path from 'path'
6
+
7
+ import {
8
+ fileURLToPath
9
+ } from 'url'
10
+
11
+ import debug from 'debug'
12
+
13
+ import {
14
+ clear
15
+ } from '#music-library-parser'
16
+
17
+ const log = debug('@sequencemedia/music-library-parser:to-m3u')
18
+ const error = debug('@sequencemedia/music-library-parser:to-m3u:error')
19
+
20
+ const cwd = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..')
21
+ const xsl = path.resolve(cwd, 'src/xsl/library/tracks.xsl')
22
+
23
+ let immediate = null
24
+ const queue = []
25
+
26
+ export function parse (jar, xml, destination = './Music Library') {
27
+ return (
28
+ new Promise((resolve, reject) => {
29
+ exec(`java -jar "${path.resolve(jar)}" -s:"${path.resolve(xml)}" -xsl:"${xsl}" destination="${path.resolve(destination)}"`, { cwd }, (e) => (!e) ? resolve() : reject(e))
30
+ })
31
+ )
32
+ }
33
+
34
+ export async function toM3U (jar, xml, destination) {
35
+ /**
36
+ * Ignore these values if they are duplicates of some in the queue
37
+ */
38
+ if (!queue.some(({ jar: j, xml: x, destination: d }) => (
39
+ j === jar &&
40
+ x === xml &&
41
+ d === destination
42
+ ))) {
43
+ /**
44
+ * These values are unique, so
45
+ */
46
+ if (immediate) {
47
+ /**
48
+ * XML is being parsed. En-queue these values
49
+ */
50
+ queue.push({ jar, xml, destination })
51
+ /**
52
+ * (... if the XML changes while it's being parsed
53
+ * it should be parsed again immediately after,
54
+ * so the second of two identical calls will be
55
+ * put into the queue while the first is executing
56
+ *
57
+ * In other words: the first call isn't put into the
58
+ * queue in order to allow that second call to be
59
+ * put into the queue!)
60
+ */
61
+ } else {
62
+ /**
63
+ * XML is not being parsed
64
+ */
65
+ immediate = setImmediate(async () => {
66
+ try {
67
+ /**
68
+ * Clear the destination directory
69
+ */
70
+ await clear(destination)
71
+ /**
72
+ * Parse these values immediately
73
+ */
74
+ await parse(jar, xml, destination)
75
+
76
+ log(`Succeeded parsing "${xml}"`)
77
+
78
+ immediate = null
79
+
80
+ if (queue.length) {
81
+ /**
82
+ * The queue is not empty. De-queue some values
83
+ */
84
+
85
+ const {
86
+ jar: j,
87
+ xml: x,
88
+ destination: d
89
+ } = queue.shift()
90
+
91
+ /**
92
+ * Repeat
93
+ */
94
+ return toM3U(j, x, d)
95
+ }
96
+
97
+ /**
98
+ * Return handle from `setImmediate` or null
99
+ */
100
+ return immediate
101
+ } catch (e) {
102
+ const {
103
+ code = 'No error code defined'
104
+ } = e
105
+
106
+ if (code === 2) {
107
+ error('I/O error in Saxon', e)
108
+ } else {
109
+ const {
110
+ message = 'No error message defined'
111
+ } = e
112
+
113
+ error(code, message, e)
114
+ }
115
+ }
116
+ })
117
+ }
118
+ }
119
+ }
120
+
121
+ export * as transform from '#music-library-parser/library/tracks/transform'