combine.fm/lib/share.js
Jonathan Cremin 7bb0497ff4 Migrate all the things
* Migrates from Mongo to Postgres.
* Migrates from JSPM to Webpack.
* Migrates from React to Vuejs.
* Migrates from Bootstrap to Bulma.

Also:
* Fixes rendering of meta data in the document head tag.
2016-10-23 21:36:23 +01:00

91 lines
2.3 KiB
JavaScript

import co from 'co';
import debuglog from 'debug';
import models from '../models';
import services from '../lib/services';
const debug = debuglog('match.audio:share');
export function find(music) {
return models[music.type].findOne({
where: {
externalId: music.id,
},
include: [
{ model: models.artist },
{ model: models.match },
],
});
}
export function create(music) {
return models[music.type].create({
externalId: music.id,
service: music.service,
name: music.name,
albumName: music.type === 'track' ? music.album.name : null,
artist: {
name: music.artist.name,
artworkSmall: null,
artworkLarge: null,
},
matches: [
{
externalId: music.id,
service: music.service,
name: music.name,
streamUrl: music.streamUrl,
purchaseUrl: music.purchaseUrl,
artworkSmall: music.artwork.small,
artworkLarge: music.artwork.large,
},
],
}, {
include: [
{ model: models.artist },
{ model: models.match },
],
});
}
export function findMatchesAsync(share) {
process.nextTick(() => {
for (const service of services) {
if (service.id === share.service) {
continue; // eslint-disable-line no-continue
}
co(function* gen() { // eslint-disable-line no-loop-func
const match = yield service.search(share);
console.log(service.id)
console.log(match)
if (match.id) {
models.match.create({
trackId: share.albumName ? share.id : null,
albumId: share.albumName ? null : share.id,
externalId: match.id,
service: match.service,
name: match.name,
streamUrl: match.streamUrl,
purchaseUrl: match.purchaseUrl,
artworkSmall: match.artwork.small,
artworkLarge: match.artwork.large,
});
} else {
models.match.create({
trackId: share.trackId ? share.id : null,
albumId: share.albumId ? share.id : null,
externalId: null,
service: match.service,
name: null,
streamUrl: null,
purchaseUrl: null,
artworkSmall: null,
artworkLarge: null,
});
}
}).catch((err) => {
debug(err);
});
}
});
}