From 2693afb2bd614b6e37609c60afe2061597c86a1c Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Fri, 26 May 2017 19:47:17 +0100 Subject: [PATCH] Minor bugfixes for groove and deezer --- app.js | 2 +- lib/error-handler.js | 3 ++- lib/services/xbox/index.js | 32 ++++++++++++++++---------------- lib/services/xbox/url.js | 2 +- lib/share.js | 8 ++++---- models/index.js | 1 + routes/index.js | 2 ++ 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app.js b/app.js index cfd9940..1d29968 100644 --- a/app.js +++ b/app.js @@ -31,7 +31,7 @@ app.on('error', (err) => { raven.captureException(err); }); -app.use(errorHandler()); +app.use(errorHandler(raven)); app.use(bodyparser()); app.use(cors()); diff --git a/lib/error-handler.js b/lib/error-handler.js index d6c8020..5dcd75e 100644 --- a/lib/error-handler.js +++ b/lib/error-handler.js @@ -2,7 +2,7 @@ import debuglog from 'debug'; const debug = debuglog('match.audio'); -export default function () { +export default function (raven) { return function* error(next) { this.set('Server', 'Nintendo 64'); try { @@ -15,6 +15,7 @@ export default function () { this.body = err.error; } else { debug('Error: %o', err); + raven.captureException(err); throw err; } } diff --git a/lib/services/xbox/index.js b/lib/services/xbox/index.js index 5d6c8c6..d1a8649 100644 --- a/lib/services/xbox/index.js +++ b/lib/services/xbox/index.js @@ -68,12 +68,12 @@ export function* parseUrl(url) { const parsed = parse(url); const parts = parsed.path.split('/'); const type = parts[1]; - const idMatches = parts[4].match(/[\w\-]+/); - const id = idMatches[0]; + const idMatches = parts[4].match(/bz.[\w\-]+/); + const id = idMatches[0].replace('bz.', 'music.'); if (!id) { return false; } - return yield lookupId('music.' + id, type); + return yield lookupId(id, type); } export function* lookupId(id, type) { @@ -106,22 +106,23 @@ export function* search(data) { } const name = data.name; + const path = '/music/search?q=' + encodeURIComponent(query.trim()) + '&filters=' + type + 's'; + try { + const result = yield apiCall(path); - const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's'; - const result = yield apiCall(path); + const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's'; - const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's'; + let match = exactMatch(name, data.artist.name, result.body[apiType].Items, type); + if (!match) { + match = looseMatch(name, data.artist.name, result.body[apiType].Items, type); + } - let match = exactMatch(name, data.artist.name, result.body[apiType].Items, type); - if (!match) { - match = looseMatch(name, data.artist.name, result.body[apiType].Items, type); + if (match) { + return formatResponse(match); + } + } catch (err) { + return {service: 'xbox'}; } - - if (match) { - return formatResponse(match); - } - - return {service: 'xbox'}; }; function exactMatch(item, artist, haystack, type) { @@ -136,7 +137,6 @@ function exactMatch(item, artist, haystack, type) { function looseMatch(item, artist, haystack, type) { // try to find exact match return haystack.find(function(entry) { - console.log(entry.Name, entry.Artists[0].Artist.Name) if (entry.Name.indexOf(item) >= 0 && entry.Artists[0].Artist.Name.indexOf(artist) >= 0) { return entry; } diff --git a/lib/services/xbox/url.js b/lib/services/xbox/url.js index fb54a36..a545dd1 100644 --- a/lib/services/xbox/url.js +++ b/lib/services/xbox/url.js @@ -2,7 +2,7 @@ import { parse } from 'url'; export function* match(url, type) { const parsed = parse(url); - if (!parsed.host.match(/music.xbox.com$/)) { + if (!parsed.host.match(/music.microsoft.com$/)) { return false; } diff --git a/lib/share.js b/lib/share.js index 5103392..efcbdbd 100644 --- a/lib/share.js +++ b/lib/share.js @@ -9,7 +9,7 @@ const debug = debuglog('match.audio:share'); export function find(music) { return models[music.type].findOne({ where: { - externalId: music.id, + externalId: music.id.toString(), }, include: [ { model: models.artist }, @@ -20,7 +20,7 @@ export function find(music) { export function create(music) { return models[music.type].create({ - externalId: music.id, + externalId: music.id.toString(), service: music.service, name: music.name, albumName: music.type === 'track' ? music.album.name : null, @@ -31,7 +31,7 @@ export function create(music) { }, matches: [ { - externalId: music.id, + externalId: music.id.toString(), service: music.service, name: music.name, streamUrl: music.streamUrl, @@ -62,7 +62,7 @@ export function findMatchesAsync(share) { models.match.create({ trackId: share.$modelOptions.name.singular == 'track' ? share.id : null, albumId: share.$modelOptions.name.singular == 'album' ? share.id : null, - externalId: match.id, + externalId: match.id.toString(), service: match.service, name: match.name, streamUrl: match.streamUrl, diff --git a/models/index.js b/models/index.js index 7f39ae5..c774f88 100644 --- a/models/index.js +++ b/models/index.js @@ -9,6 +9,7 @@ const debug = debugname('match.audio:models'); const config = { dialect: 'postgres', protocol: 'postgres', + quoteIdentifiers: true, logging: debug, }; diff --git a/routes/index.js b/routes/index.js index beeb4d7..3056c41 100644 --- a/routes/index.js +++ b/routes/index.js @@ -39,6 +39,8 @@ export default function* () { share: false, }; + this.set('Cache-Control', 'no-cache'); + yield this.render('index', { initialState, head,