Refactor media info services

This commit is contained in:
An Phan 2016-06-05 19:29:49 +08:00
parent 080b0aac64
commit fc325e5262
10 changed files with 139 additions and 122 deletions

View file

@ -31,6 +31,7 @@
import albumInfo from './album-info.vue';
import preferences from '../../../stores/preference';
import songStore from '../../../stores/song';
import songInfoService from '../../../services/info/song';
export default {
components: { lyrics, artistInfo, albumInfo },
@ -93,7 +94,7 @@
},
'song:played': function (song) {
songStore.getInfo(this.song = song);
songInfoService.fetch(this.song = song);
},
'koel:teardown': function () {

View file

@ -67,6 +67,7 @@
import artistStore from '../../../stores/artist';
import sharedStore from '../../../stores/shared';
import playback from '../../../services/playback';
import albumInfoService from '../../../services/info/album';
import download from '../../../services/download';
import hasSongList from '../../../mixins/has-song-list';
import artistAlbumDetails from '../../../mixins/artist-album-details';
@ -146,7 +147,7 @@
this.info.showing = true;
if (!this.album.info) {
this.info.loading = true;
albumStore.fetchInfo(this.album, () => {
albumInfoService.fetch(this.album, () => {
this.info.loading = false;
});
} else {

View file

@ -62,9 +62,10 @@
<script>
import isMobile from 'ismobilejs';
import artistStore from '../../../stores/artist';
import sharedStore from '../../../stores/shared';
import artistStore from '../../../stores/artist';
import playback from '../../../services/playback';
import artistInfoService from '../../../services/info/artist';
import download from '../../../services/download';
import hasSongList from '../../../mixins/has-song-list';
import artistInfo from '../extra/artist-info.vue';
@ -136,7 +137,7 @@
this.info.showing = true;
if (!this.artist.info) {
this.info.loading = true;
artistStore.fetchInfo(this.artist, () => {
artistInfoService.fetch(this.artist, () => {
this.info.loading = false;
});
} else {

View file

@ -79,6 +79,7 @@
import { every, filter } from 'lodash';
import { br2nl } from '../../services/utils';
import songInfoService from '../../services/info/song';
import artistStore from '../../stores/artist';
import albumStore from '../../stores/album';
import songStore from '../../stores/song';
@ -232,7 +233,7 @@
if (!this.songs[0].infoRetrieved) {
this.loading = true;
songStore.getInfo(this.songs[0], () => {
songInfoService.fetch(this.songs[0], () => {
this.loading = false;
this.formData.lyrics = br2nl(this.songs[0].lyrics);
this.formData.track = this.songs[0].track;

View file

@ -0,0 +1,51 @@
import { each } from 'lodash';
import { secondsToHis } from '../utils';
import http from '../http';
export default {
/**
* Get extra album info (from Last.fm).
*
* @param {Object} album
* @param {?Function} cb
*/
fetch(album, cb = null) {
if (album.info) {
cb && cb();
return;
}
http.get(`album/${album.id}/info`, response => {
if (response.data) {
this.merge(album, response.data);
}
cb && cb();
});
},
/**
* Merge the (fetched) info into an album.
*
* @param {Object} album
* @param {Object} info
*/
merge(album, info) {
// Convert the duration into i:s
info.tracks && each(info.tracks, track => track.fmtLength = secondsToHis(track.length));
// If the album cover is not in a nice form, discard.
if (typeof info.image !== 'string') {
info.image = null;
}
// Set the album cover on the client side to the retrieved image from server.
if (info.cover) {
album.cover = info.cover;
}
album.info = info;
},
};

View file

@ -0,0 +1,45 @@
import http from '../http';
export default {
/**
* Get extra artist info (from Last.fm).
*
* @param {Object} artist
* @param {?Function} cb
*/
fetch(artist, cb = null) {
if (artist.info) {
cb && cb();
return;
}
http.get(`artist/${artist.id}/info`, response => {
if (response.data) {
this.merge(artist, response.data);
}
cb && cb();
});
},
/**
* Merge the (fetched) info into an artist.
*
* @param {Object} artist
* @param {Object} info
*/
merge(artist, info) {
// If the artist image is not in a nice form, discard.
if (typeof info.image !== 'string') {
info.image = null;
}
// Set the artist image on the client side to the retrieved image from server.
if (info.image) {
artist.image = info.image;
}
artist.info = info;
},
};

View file

@ -0,0 +1,34 @@
import http from '../http';
import albumInfo from './album';
import artistInfo from './artist';
export default {
/**
* Get extra song information (lyrics, artist info, album info).
*
* @param {Object} song
* @param {?Function} cb
*/
fetch(song, cb = null) {
// Check if the song's info has been retrieved before.
if (song.infoRetrieved) {
cb && cb();
return;
}
http.get(`${song.id}/info`, response => {
const data = response.data;
song.lyrics = data.lyrics;
data.artist_info && artistInfo.merge(song.album.artist, data.artist_info);
data.album_info && albumInfo.merge(song.album, data.album_info);
song.infoRetrieved = true;
cb && cb();
});
},
};

View file

@ -11,7 +11,6 @@ import {
} from 'lodash';
import { secondsToHis } from '../services/utils';
import http from '../services/http';
import stub from '../stubs/album';
import songStore from './song';
import artistStore from './artist';
@ -163,51 +162,6 @@ export default {
});
},
/**
* Get extra album info (from Last.fm).
*
* @param {Object} album
* @param {?Function} cb
*/
fetchInfo(album, cb = null) {
if (album.info) {
cb && cb();
return;
}
http.get(`album/${album.id}/info`, response => {
if (response.data) {
this.mergeAlbumInfo(album, response.data);
}
cb && cb();
});
},
/**
* Merge the (fetched) info into an album.
*
* @param {Object} album
* @param {Object} info
*/
mergeAlbumInfo(album, info) {
// Convert the duration into i:s
info.tracks && each(info.tracks, track => track.fmtLength = secondsToHis(track.length));
// If the album cover is not in a nice form, discard.
if (typeof info.image !== 'string') {
info.image = null;
}
// Set the album cover on the client side to the retrieved image from server.
if (info.cover) {
album.cover = info.cover;
}
album.info = info;
},
/**
* Get top n most-played albums.
*

View file

@ -10,7 +10,6 @@ import {
orderBy,
} from 'lodash';
import http from '../services/http';
import config from '../config';
import stub from '../stubs/artist';
import albumStore from './album';
@ -118,48 +117,6 @@ export default {
this.all = difference(this.all, [].concat(artists));
},
/**
* Get extra artist info (from Last.fm).
*
* @param {Object} artist
* @param {?Function} cb
*/
fetchInfo(artist, cb = null) {
if (artist.info) {
cb && cb();
return;
}
http.get(`artist/${artist.id}/info`, response => {
if (response.data) {
this.mergeArtistInfo(artist, response.data);
}
cb && cb();
});
},
/**
* Merge the (fetched) info into an artist.
*
* @param {Object} artist
* @param {Object} info
*/
mergeArtistInfo(artist, info) {
// If the artist image is not in a nice form, discard.
if (typeof info.image !== 'string') {
info.image = null;
}
// Set the artist image on the client side to the retrieved image from server.
if (info.image) {
artist.image = info.image;
}
artist.info = info;
},
/**
* Add album(s) into an artist.
*

View file

@ -184,34 +184,6 @@ export default {
this.state.recent.unshift(song);
},
/**
* Get extra song information (lyrics, artist info, album info).
*
* @param {Object} song
* @param {?Function} cb
*/
getInfo(song, cb = null) {
// Check if the song's info has been retrieved before.
if (song.infoRetrieved) {
cb && cb();
return;
}
http.get(`${song.id}/info`, response => {
const data = response.data;
song.lyrics = data.lyrics;
data.artist_info && artistStore.mergeArtistInfo(song.album.artist, data.artist_info);
data.album_info && albumStore.mergeAlbumInfo(song.album, data.album_info);
song.infoRetrieved = true;
cb && cb();
});
},
/**
* Scrobble a song (using Last.fm).
*