Skip to content

Commit

Permalink
Fix examples and update to ECMA version 8.
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Jan 2, 2021
1 parent 75af3da commit 39e8f77
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 79 deletions.
56 changes: 26 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,19 @@ Parses the specified file (`filePath`) and returns a promise with the metadata r
parseFile(filePath: string, opts: IOptions = {}): Promise<IAudioMetadata>`
```

Javascript example:
Example:
```js
const mm = require('music-metadata');
const util = require('util');

mm.parseFile('../test/samples/MusicBrainz-multiartist [id3v2.4].V2.mp3')
.then( metadata => {
(async () => {
try {
const metadata = await mm.parseFile('../music-metadata/test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
})
.catch( err => {
console.error(err.message);
});
```

Typescript example:
```js
import * as mm from 'music-metadata';
import * as util from 'util';

mm.parseFile('../test/samples/MusicBrainz-multiartist [id3v2.4].V2.mp3')
.then( metadata => {
console.log(util.inspect(metadata, {showHidden: false, depth: null}));
})
.catch((err) => {
console.error(err.message);
});
} catch (error) {
console.err(err.message);
}
})();
```

#### parseStream function
Expand All @@ -179,11 +166,16 @@ parseStream(stream: Stream.Readable, fileInfo?: IFileInfo | string, opts?: IOpti

Example:
```js
mm.parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838})
.then( metadata => {
console.log(util.inspect(metadata, {showHidden: false, depth: null}));
someReadStream.destroy();
});
const mm = require('music-metadata');
(async () => {
try {
const metadata = await mm.parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838});
console.log(metadata);
} catch (error) {
console.err(err.message);
}
})();
```

#### parseBuffer function
Expand All @@ -196,10 +188,14 @@ parseBuffer(buffer: Buffer, fileInfo?: IFileInfo | string, opts?: IOptions = {})

Example:
```js
mm.parseBuffer(someBuffer, 'audio/mpeg')
.then( metadata => {
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
});
(async () => {
try {
const metadata = mm.parseBuffer(someBuffer, 'audio/mpeg');
console.log(metadata);
} catch (error) {
console.err(err.message);
}
})();
```

#### parseFromTokenizer function
Expand Down
2 changes: 1 addition & 1 deletion example/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 8,
"sourceType": "module"
},
"rules": {
Expand Down
11 changes: 6 additions & 5 deletions example/javascript/javascript.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const mm = require('../../lib'); // music-metadata
const util = require('util');

mm.parseFile('../test/samples/MusicBrainz-multiartist [id3v2.4].V2.mp3')
.then(function (metadata) {
(async () => {
try {
const metadata = await mm.parseFile('../../test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
})
.catch(function (err) {
} catch (error) {
console.err(err.message);
});
}
})();
28 changes: 12 additions & 16 deletions example/javascript/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const mm = require('../../lib'); // music-metadata
const audioUrl = 'https://github.com/Borewit/music-metadata/raw/master/test/samples/MusicBrainz%20-%20Beth%20Hart%20-%20Sinner\'s%20Prayer%20%5Bid3v2.3%5D.V2.mp3';

function httpGet (url) {
return new Promise(function (resolve, reject) {
https.get(url, function (res) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
switch (res.statusCode) {
case 200:
resolve(res);
Expand All @@ -21,17 +21,13 @@ function httpGet (url) {
});
}

// Stream MP3 sample file from GitHub via HTTP
httpGet(audioUrl).then(metadata => {
// Parse the MP3 audio stream
const mimeType = metadata.headers['content-type'];
console.log('Parsing: ' + mimeType);
return mm.parseStream(metadata, mimeType, {native: true})
.then(metadata => {
// Print the metadata result
console.log(util.inspect(metadata, {showHidden: false, depth: null}));
});
}).catch(function (err) {
// Oops, something went wrong
console.error(err.message);
});
(async () => {
try {
// Stream MP3 sample file from GitHub via HTTP
const metadata = await httpGet(audioUrl);
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
} catch(err) {
// Oops, something went wrong
console.error(err.message);
}
})();
57 changes: 30 additions & 27 deletions example/javascript/walk.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
const walk = require('walk');
const Walk = require('@root/walk');
const path = require('path');
const util = require('util');
const mm = require('../../lib'); // music-metadata

const walker = walk.walk('M:\\');
async function walkFunc(err, pathname, dirent) {
// err is failure to lstat a file or directory
// pathname is relative path, including the file or folder name
// dirent = { name, isDirectory(), isFile(), isSymbolicLink(), ... }

let filesParsed = 0;

walker.on('file', function (root, fileStats, next) {

switch (path.extname(fileStats.name)) {
case '.mp3':
case '.m4a':
case '.wav':
case '.ogg':
case '.flac':
// Queue (asynchronous call) parsing of metadata
const fn = path.join(root, fileStats.name);
mm.parseFile(fn).then(metadata => {
// console.log(util.inspect(metadata, { showHidden: false, depth: null }));
console.log('Parsed %s files, last file: %s', ++filesParsed, fn);
next(); // asynchronous call completed, 'release' next
}).catch(function (err) {
console.error('Error parsing: %s', fn);
console.error(err.message);
next();
});
break;
if (dirent.isFile()) {
console.log(`File: ${pathname}`);
switch (path.extname(pathname)) {
case '.mp3':
case '.m4a':
case '.wav':
case '.ogg':
case '.flac':
// Queue (asynchronous call) parsing of metadata
const metadata = await mm.parseFile(pathname);
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
break;
}
}
}

default:
next(); // Go to next file, no asynchronous call 'queued'
(async () => {
try {
await Walk.walk('M:\\_Classic', walkFunc);
console.log('Done.');
} catch(err) {
// Oops, something went wrong
console.error(err.message);
}
});
})();

0 comments on commit 39e8f77

Please sign in to comment.