From 23896f7ee691e9494627aac7e57078f8bf1df583 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sat, 13 Dec 2014 12:05:47 +0000 Subject: [PATCH] More bluebird, fetch larger artwork --- app.js | 24 +++-------- lib/services/beats/index.js | 80 +++++++++++++++++++---------------- lib/services/deezer/index.js | 5 ++- lib/services/google/index.js | 10 ++++- lib/services/itunes/index.js | 10 ++++- lib/services/rdio/index.js | 11 ++++- lib/services/spotify/index.js | 10 ++++- lib/services/youtube/index.js | 5 ++- routes/share.js | 5 +-- test/services/google.js | 7 +++ test/services/itunes.js | 4 +- test/services/spotify.js | 7 +++ views/album.ejs | 6 +-- views/header.ejs | 6 +-- views/index.ejs | 2 +- views/track.ejs | 6 +-- 16 files changed, 117 insertions(+), 81 deletions(-) diff --git a/app.js b/app.js index 07212e5..5be9bc5 100644 --- a/app.js +++ b/app.js @@ -35,18 +35,11 @@ app.use(session({ app.use(flash()); app.use(express.static(path.join(__dirname, 'public'))); -var db; -if (process.env.MONGOHQ_URL) { - console.log("Connecting to MongoHQ") - db = pmongo(process.env.MONGOHQ_URL, ['matches']); -} else { - db = pmongo('match-audio', ['matches']); -} - +var dbUrl = process.env.MONGOHQ_URL || "mongodb://localhost/match-audio"; app.use(function(req, res, next) { - req.db = res.db = db; + req.db = res.db = pmongo(dbUrl, ['matches']); next(); -}) +}); app.get('*', function(req,res,next) { @@ -56,7 +49,7 @@ app.get('*', function(req,res,next) { } else if (req.headers['cf-visitor']) { req.userProtocol = "https"; } else { - req.userProtocol = "http" + req.userProtocol = "http"; } // redirect www if (req.headers.host.match(/^www/) !== null ) { @@ -67,15 +60,8 @@ app.get('*', function(req,res,next) { }); app.get('/', function(req, res) { - var samples = [ - {artist: "Aesop Rock", album: "Skelethon", url: '/google/album/B3ppmqcekrmxln4bre33om3qife'}, - {artist: "Hozier", album: "self-titled album", url: '/google/album/Bd3mxcy3otokg4yc45qktq7l35q'}, - {artist: "Daft Punk", album: "Discovery", url: '/google/album/B4t6yqqvhnb2hy4st4uisjrcsrm'} - ]; - - // shitty sort until I add more metadata on cached items req.db.matches.find().sort({$natural:-1}).limit(6).toArray().then(function(docs){ - res.render('index', { page: "home", samples: samples, recent: docs, error: req.flash('search-error') }); + res.render('index', { page: "home", recent: docs, error: req.flash('search-error') }); }); }); diff --git a/lib/services/beats/index.js b/lib/services/beats/index.js index 5972bd5..1df7af1 100644 --- a/lib/services/beats/index.js +++ b/lib/services/beats/index.js @@ -33,27 +33,30 @@ module.exports.parseUrl = function(url) { module.exports.lookupId = function(id, type) { if (type == "album") { - return request.get(apiRoot + "/albums/" + id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).promise().then(function(res) { - var artwork = res.headers.location; - return request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key).promise().then(function(res) { - if (!res.body.data) { - var error = new Error("Not Found"); - error.status = 404; - throw error; - } - var result = res.body.data; - return { - service: "beats", - type: "album", - id: result.id, - name: result.title, - streamUrl: "https://listen.beatsmusic.com/albums/" + result.id, - purchaseUrl: null, - artwork: artwork.replace("http:", "https:"), - artist: { - name: result.artist_display_name + return request.get(apiRoot + "/albums/" + id + "/images/default?size=large&client_id=" + credentials.key).redirects(0).promise().then(function(res) { + var artwork = {large: res.headers.location.replace("http:", "https:")}; + return request.get(apiRoot + "/albums/" + id + "/images/default?client_id=" + credentials.key).redirects(0).promise().then(function(res) { + artwork.small = res.headers.location.replace("http:", "https:"); + return request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key).promise().then(function(res) { + if (!res.body.data) { + var error = new Error("Not Found"); + error.status = 404; + throw error; } - }; + var result = res.body.data; + return { + service: "beats", + type: "album", + id: result.id, + name: result.title, + streamUrl: "https://listen.beatsmusic.com/albums/" + result.id, + purchaseUrl: null, + artwork: artwork, + artist: { + name: result.artist_display_name + } + }; + }); }); }); } else if (type == "track") { @@ -64,23 +67,26 @@ module.exports.lookupId = function(id, type) { throw error; } var result = res.body.data; - return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).promise().then(function(res) { - var artwork = res.headers.location; - return { - service: "beats", - type: "track", - id: result.id, - name: result.title, - streamUrl: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id, - purchaseUrl: null, - artwork: artwork.replace("http:", "https:"), - artist: { - name: result.artist_display_name - }, - album: { - name: result.refs.album.display - } - }; + return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=large&client_id=" + credentials.key).redirects(0).promise().then(function(res) { + var artwork = {large: res.headers.location.replace("http:", "https:")}; + return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?client_id=" + credentials.key).redirects(0).promise().then(function(res) { + artwork.small = res.headers.location.replace("http:", "https:"); + return { + service: "beats", + type: "track", + id: result.id, + name: result.title, + streamUrl: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id, + purchaseUrl: null, + artwork: artwork, + artist: { + name: result.artist_display_name + }, + album: { + name: result.refs.album.display + } + }; + }); }); }); } else { diff --git a/lib/services/deezer/index.js b/lib/services/deezer/index.js index 4969e69..402a17b 100644 --- a/lib/services/deezer/index.js +++ b/lib/services/deezer/index.js @@ -32,7 +32,10 @@ module.exports.lookupId = function(id, type) { } var cover = result.cover || result.album.cover; return request.get(cover).redirects(0).promise().then(function(res) { - var artwork = res.headers.location.replace("120x120", "200x200"); + var artwork = { + small: res.headers.location.replace("120x120", "200x200"), + large: res.headers.location.replace("120x120", "800x800") + }; if (type == "album") { return { service: "deezer", diff --git a/lib/services/google/index.js b/lib/services/google/index.js index b9ba057..d73827e 100644 --- a/lib/services/google/index.js +++ b/lib/services/google/index.js @@ -53,7 +53,10 @@ module.exports.lookupId = function(id, type) { name: album.name, streamUrl: "https://play.google.com/music/listen#/album/" + album.albumId, purchaseUrl: "https://play.google.com/store/music/album?id=" + album.albumId, - artwork: album.albumArtRef.replace("http:", "https:"), + artwork: { + small: album.albumArtRef.replace("http:", "https:"), + large: album.albumArtRef.replace("http:", "https:") + }, artist: { name: album.artist } @@ -70,7 +73,10 @@ module.exports.lookupId = function(id, type) { name: track.title, streamUrl: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId, purchaseUrl: "https://play.google.com/store/music/album?id=" + track.albumId, - artwork: track.albumArtRef[0].url.replace("http:", "https:"), + artwork: { + small: track.albumArtRef[0].url.replace("http:", "https:"), + large: track.albumArtRef[0].url.replace("http:", "https:") + }, album: { name: track.album }, diff --git a/lib/services/itunes/index.js b/lib/services/itunes/index.js index 9605cd4..a127f76 100644 --- a/lib/services/itunes/index.js +++ b/lib/services/itunes/index.js @@ -57,7 +57,10 @@ module.exports.lookupId = function(id, type, cc) { name: result.trackName ? result.trackName : result.collectionName, streamUrl: null, purchaseUrl: result.collectionViewUrl, - artwork: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""), + artwork: { + small: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""), + large: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "600x600").replace("http://", ""), + }, artist: { name: result.artistName } @@ -115,7 +118,10 @@ module.exports.search = function(data) { name: result.trackName ? result.trackName : result.collectionName, streamUrl: null, purchaseUrl: result.collectionViewUrl, - artwork: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""), + artwork: { + small: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""), + large: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "600x600").replace("http://", ""), + }, artist: { name: result.artistName } diff --git a/lib/services/rdio/index.js b/lib/services/rdio/index.js index 9fc4f81..020b653 100644 --- a/lib/services/rdio/index.js +++ b/lib/services/rdio/index.js @@ -29,6 +29,7 @@ module.exports.lookupId = function(id) { var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); var type = result.album ? "track" : "album"; + var item = { service: "rdio", type: type, @@ -36,7 +37,10 @@ module.exports.lookupId = function(id) { name: result.name, streamUrl: result.shortUrl, purchaseUrl: null, - artwork: result.icon.replace("square-200", "square-250").replace("http:", "https:"), + artwork: { + small: result.icon.replace("square-200", "square-250").replace("http:", "https:"), + large: result.icon.replace("square-200", "square-600").replace("http:", "https:") + }, artist: { name: result.artist } @@ -89,7 +93,10 @@ module.exports.parseUrl = function(url) { name: result.name, streamUrl: result.shortUrl, purchaseUrl: null, - artwork: result.icon.replace("square-200", "square-250").replace("http:", "https:"), + artwork: { + small: result.icon.replace("square-200", "square-250").replace("http:", "https:"), + large: result.icon.replace("square-200", "square-600").replace("http:", "https:") + }, artist: { name: result.artist } diff --git a/lib/services/spotify/index.js b/lib/services/spotify/index.js index fa4a0d5..b11fa49 100644 --- a/lib/services/spotify/index.js +++ b/lib/services/spotify/index.js @@ -33,7 +33,10 @@ module.exports.lookupId = function(id, type) { name: data.name, streamUrl: "https://play.spotify.com/" + type + "/" + data.id, purchaseUrl: null, - artwork: data.images ? data.images[1].url.replace("http:", "https:") : data.album.images[1].url.replace("http:", "https:"), + artwork: { + small: data.images[1].url.replace("http:", "https:"), + large: data.images[0].url.replace("http:", "https:"), + }, artist: { name: artist.name } @@ -46,7 +49,10 @@ module.exports.lookupId = function(id, type) { name: data.name, streamUrl: "https://play.spotify.com/" + type + "/" + data.id, purchaseUrl: null, - artwork: data.images ? data.images[1].url.replace("http:", "https:") : data.album.images[1].url.replace("http:", "https:"), + artwork: { + small: data.album.images[1].url.replace("http:", "https:"), + large: data.album.images[0].url.replace("http:", "https:"), + }, artist: { name: artist.name }, diff --git a/lib/services/youtube/index.js b/lib/services/youtube/index.js index e99f256..b664896 100644 --- a/lib/services/youtube/index.js +++ b/lib/services/youtube/index.js @@ -46,7 +46,10 @@ module.exports.search = function(data) { name: result.snippet.title, streamUrl: "https://www.youtube.com/watch?v=" + result.id.videoId, purchaseUrl: null, - artwork: result.snippet.thumbnails.medium.url, + artwork: { + small: result.snippet.thumbnails.medium.url, + large: result.snippet.thumbnails.high.url, + } }; } }); diff --git a/routes/share.js b/routes/share.js index aadad3d..b71b44f 100644 --- a/routes/share.js +++ b/routes/share.js @@ -23,8 +23,7 @@ module.exports = function(req, res, next) { return; } - - req.db.matches.findOne({_id:serviceId + itemId}).then(function(doc) { + req.db.matches.findOne({_id:serviceId + "-" + itemId}).then(function(doc) { if (doc) { res.render(type, { page: type, @@ -58,7 +57,7 @@ module.exports = function(req, res, next) { }); items.unshift(item); - req.db.matches.save({_id:serviceId + itemId, items:items}); + req.db.matches.save({_id:serviceId + "-" + itemId, items:items}); res.render(type, { page: type, title: item.name + " by " + item.artist.name, diff --git a/test/services/google.js b/test/services/google.js index ce44397..b9a1eeb 100644 --- a/test/services/google.js +++ b/test/services/google.js @@ -12,6 +12,13 @@ describe('Google Play Music', function(){ done(); }); }); + + it('should find track by ID', function(done){ + google.lookupId("Tjosptub24g2dft37lforqnudpe", "track").then(function(result) { + result.name.should.equal("Cherub Rock"); + done(); + }); + }); }); describe('search', function(){ diff --git a/test/services/itunes.js b/test/services/itunes.js index de119a5..8144a13 100644 --- a/test/services/itunes.js +++ b/test/services/itunes.js @@ -7,14 +7,14 @@ var itunes = require("../../lib/services/itunes"); describe('iTunes Music', function(){ describe('lookupId', function(){ it('should find album by ID', function(done){ - itunes.lookupId("id215206912").then(function(result) { + itunes.lookupId("id215206912", "album").then(function(result) { result.name.should.equal("Peace Orchestra"); done(); }); }); it('should find track by ID', function(done){ - itunes.lookupId("id215206958").then(function(result) { + itunes.lookupId("id215206958", "track").then(function(result) { result.name.should.equal("Double Drums"); done(); }); diff --git a/test/services/spotify.js b/test/services/spotify.js index 8fa6701..54a06a7 100644 --- a/test/services/spotify.js +++ b/test/services/spotify.js @@ -12,6 +12,13 @@ describe('Spotify', function(){ done(); }); }); + + it('should find track by ID', function(done){ + spotify.lookupId("7dS5EaCoMnN7DzlpT6aRn2", "track").then(function(result) { + result.name.should.equal("Take Me To Church"); + done(); + }); + }); }); describe('search', function(){ diff --git a/views/album.ejs b/views/album.ejs index 54588d6..b40c689 100644 --- a/views/album.ejs +++ b/views/album.ejs @@ -34,17 +34,17 @@ More Youtube matches <% } else if (album.streamUrl) { %> - + <% } else if (album.purchaseUrl) { %> - + <% } else { %> - + diff --git a/views/header.ejs b/views/header.ejs index ddebc86..7729b4c 100644 --- a/views/header.ejs +++ b/views/header.ejs @@ -7,12 +7,12 @@ <% if (locals.items) { var item = items[0] %> - + "> - - + + <% } %> diff --git a/views/index.ejs b/views/index.ejs index c181e2a..6f4c4b7 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -33,7 +33,7 @@
<% for (var i=0;i < recent.length;i++) { var item = recent[i].items[0]; %> <% } %>
diff --git a/views/track.ejs b/views/track.ejs index f57ae71..1cd324d 100644 --- a/views/track.ejs +++ b/views/track.ejs @@ -34,17 +34,17 @@ More Youtube matches <% } else if (track.streamUrl) { %> - + <% } else if (track.purchaseUrl) { %> - + <% } else { %> - +