Skip to content

Instantly share code, notes, and snippets.

@pingiun
Created October 28, 2014 17:29
Show Gist options
  • Save pingiun/90b6a0d8ca0529d0dcd3 to your computer and use it in GitHub Desktop.
Save pingiun/90b6a0d8ca0529d0dcd3 to your computer and use it in GitHub Desktop.
Promise = require('bluebird')
Promise.longStackTraces();
fs = require('fs')
Promise.promisifyAll(fs);
youtube = require('./youtube')
ffmpeg = require('fluent-ffmpeg')
ffmetadata = require('ffmetadata')
Promise.promisifyAll(ffmetadata)
mb = require('musicbrainz')
Promise.promisifyAll(mb)
ds = module.exports
ds.downloadRelease = (songinfo, location) ->
filename = songinfo.artist + ' - ' + songinfo.title
filename = filename.replace /[*/?\\":|<>]/g, ''
youtube.download(songinfo.title, songinfo.artist, songinfo.album)
.then (result) ->
new Promise (yay, nay) ->
result.pipe fs.createWriteStream songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.m4a'
result.on 'end', ->
if not fs.existsSync songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.m4a'
nay "File download failed"
else
do yay
result.on 'error', (err) ->
nay 'File download failed'
.then ->
new Promise (yay, nay) ->
command = new ffmpeg songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.m4a'
.format 'mp3'
.on 'error', (err) ->
nay 'File convertion failed'
.on 'end', ->
console.log "Downloaded " + songinfo.artist + " - " + songinfo.title
do yay
.save songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.mp3'
.then ->
fs.unlinkAsync songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.m4a'
.then ->
ffmetadata.writeAsync songinfo.artist + '\\' + songinfo.album + '\\' + filename + '.mp3', songinfo
ds.downloadAlbum = (albuminfo, location) ->
mb.searchReleasesAsync albuminfo.album, {artist: albuminfo.artist}
.then (releases) ->
mb.lookupReleaseAsync releases[0].id, ['recordings', 'artists']
.then (release) ->
#console.log do release.artistCreditsString
console.log track.recording.title + ' - ' + do release.artistCreditsString for track in release.mediums[0].tracks
tracks = ({title: track.recording.title, artist: do release.artistCreditsString, album: albuminfo.album, track: i+1} for track, i in release.mediums[0].tracks)
Promise.map(tracks, ds.downloadRelease, {concurrency: 2})
ds.downloadArtist = (artistinfo, location) ->
mb.searchArtistsAsync artistinfo.artist, {}
.then (artists) ->
new Promise (yay, nay) ->
#mb.lookupArtist artists[0].id, [], (err, artist) ->
# console.log artist
console.log artists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment