-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: New metaprovider for iTunes music #1009
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import iTunesMusicMetaProvider from './itunesmusic'; | ||
import * as iTunesMocks from './metaMocks/iTunesMusicMocks'; | ||
import Track from '../../structs/Track'; | ||
|
||
describe('iTunes music metaprovider tests', () => { | ||
it('search for artists', async () => { | ||
iTunesMocks.mockArtistResult(); | ||
const itunesMeta = new iTunesMusicMetaProvider(); | ||
const response = await itunesMeta.searchForArtists('Queen'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(response).toEqual([ | ||
{ | ||
'coverImage': '', | ||
'id': 3296287, | ||
'name': 'Queen', | ||
'resourceUrl': 'https://music.apple.com/us/artist/queen/3296287?uo=4', | ||
'source': 'iTunesMusic', | ||
'thumb': '' | ||
} | ||
]); | ||
}); | ||
|
||
it('search for releases', async () => { | ||
iTunesMocks.mockAlbumResult(); | ||
const itunesMeta = new iTunesMusicMetaProvider(); | ||
const response = await itunesMeta.searchForReleases('The Platinum Collection (Greatest Hits I, II & III)'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(response).toEqual([ | ||
{ | ||
'id': 1440650428, | ||
'coverImage': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/250x250bb.jpg', | ||
'thumb': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/1600x1600bb.jpg', | ||
'title': 'The Platinum Collection (Greatest Hits I, II & III)', | ||
'artist': 'Queen', | ||
'resourceUrl': 'https://music.apple.com/us/artist/queen/3296287?uo=4', | ||
'source': 'iTunesMusic' | ||
} | ||
]); | ||
}); | ||
|
||
it('search for tracks', async () => { | ||
iTunesMocks.mockTrackResult(); | ||
const itunesMeta = new iTunesMusicMetaProvider(); | ||
const response = await itunesMeta.searchForTracks('The Platinum Collection (Greatest Hits I, II & III)'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(response).toEqual([ | ||
{ | ||
'id': 1440651216, | ||
'title': 'We Will Rock You', | ||
'artist': 'Queen', | ||
'source': 'iTunesMusic' | ||
} | ||
]); | ||
}); | ||
|
||
it('fetch artist albums', async () => { | ||
iTunesMocks.mockArtistAlbumsResult(); | ||
const itunesMeta = new iTunesMusicMetaProvider(); | ||
const response = await itunesMeta.fetchArtistAlbums('3296287'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(response).toEqual([ | ||
{ | ||
'id': 1440650428, | ||
'coverImage': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/250x250bb.jpg', | ||
'thumb': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/1600x1600bb.jpg', | ||
'title': 'The Platinum Collection (Greatest Hits I, II & III)', | ||
'artist': 'Queen', | ||
'resourceUrl': 'https://music.apple.com/us/artist/queen/3296287?uo=4', | ||
'source': 'iTunesMusic' | ||
} | ||
]); | ||
}); | ||
|
||
it('fetch albums details', async () => { | ||
iTunesMocks.mockAlbumSongsSearch(); | ||
const itunesMeta = new iTunesMusicMetaProvider(); | ||
const response = await itunesMeta.fetchAlbumDetails('1440650428', 'master'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
const track = new Track ({ | ||
'artist': 'The Platinum Collection (Greatest Hits I, II & III)', | ||
'duration': 356, | ||
'position': 1, | ||
'thumbnail': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/60x60bb.jpg', | ||
'title': 'Bohemian Rhapsody' | ||
}); | ||
track.uuid = ''; | ||
response.tracklist[0].uuid = ''; | ||
expect(response).toEqual({ | ||
'artist': 'Queen', | ||
'coverImage': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/250x250bb.jpg', | ||
'id': 1440650428, | ||
'thumb': 'https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/83/23/e4/8323e48b-3467-448b-1ce0-8981d8a97437/source/1600x1600bb.jpg', | ||
'title': 'The Platinum Collection (Greatest Hits I, II & III)', | ||
'tracklist': [track], | ||
'type': 'master', | ||
'year': '2014-01-01T08:00:00Z' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import _ from 'lodash'; | ||
|
||
import MetaProvider from '../metaProvider'; | ||
import * as iTunes from '../../rest/iTunes'; | ||
import { | ||
SearchResultsArtist, | ||
SearchResultsAlbum, | ||
SearchResultsTrack, | ||
ArtistDetails, | ||
AlbumDetails, | ||
SearchResultsSource, | ||
AlbumType | ||
} from '../plugins.types'; | ||
import LastFmApi from '../../rest/Lastfm'; | ||
import Track from '../../structs/Track'; | ||
import { LastFmArtistInfo, LastfmTopTracks } from '../../rest/Lastfm.types'; | ||
|
||
class iTunesMusicMetaProvider extends MetaProvider { | ||
lastfm: LastFmApi; | ||
constructor() { | ||
super(); | ||
this.name = 'iTunesMusic Meta Provider'; | ||
this.sourceName = 'iTunesMusic Meta Provider'; | ||
this.description = 'Metadata provider that uses iTunes as a source.'; | ||
this.searchName = 'iTunesMusic'; | ||
this.image = null; | ||
this.lastfm = new LastFmApi(process.env.LAST_FM_API_KEY, process.env.LASTFM_API_SECRET); | ||
} | ||
|
||
async searchForArtists(query: string): Promise<SearchResultsArtist[]> { | ||
const artistInfo = (await ((await iTunes.artistSearch(query, '50')).json())); | ||
return artistInfo.results | ||
.filter(artist => artist.artistType === 'Artist') | ||
.map(artist => ({ | ||
id: artist.artistId, | ||
coverImage: '', | ||
thumb: '', | ||
name: artist.artistName, | ||
resourceUrl: artist.artistLinkUrl, | ||
source: SearchResultsSource.iTunesMusic | ||
})); | ||
} | ||
|
||
async searchForReleases(query: string): Promise<SearchResultsAlbum[]> { | ||
const albumInfo = (await ((await iTunes.albumSearch(query, '50')).json())); | ||
return albumInfo.results.map(album => ({ | ||
id: album.collectionId, | ||
coverImage: album.artworkUrl100.replace('100x100bb.jpg', '250x250bb.jpg'), | ||
thumb: album.artworkUrl100.replace('100x100bb.jpg', '1600x1600bb.jpg'), | ||
title: album.collectionName, | ||
artist: album.artistName, | ||
resourceUrl: album.artistViewUrl, | ||
source: SearchResultsSource.iTunesMusic | ||
})); | ||
} | ||
|
||
async searchForTracks(query: string): Promise<SearchResultsTrack[]> { | ||
const musicInfo = (await ((await iTunes.musicSearch(query, '50')).json())); | ||
return musicInfo.results.map(music => ({ | ||
id: music.trackId, | ||
title: music.trackName, | ||
artist: music.artistName, | ||
source: SearchResultsSource.iTunesMusic | ||
})); | ||
} | ||
|
||
async searchAll(query): Promise<{ | ||
artists: Array<SearchResultsArtist>; | ||
releases: Array<SearchResultsAlbum>; | ||
tracks: Array<SearchResultsTrack>; | ||
}> { | ||
const artists = await this.searchForArtists(query); | ||
const releases = await this.searchForReleases(query); | ||
const tracks = await this.searchForTracks(query); | ||
return Promise.resolve({ artists, releases, tracks }); | ||
} | ||
|
||
async fetchArtistDetails(artistId: string): Promise<ArtistDetails> { | ||
const artistDetails = (await ((await iTunes.artistDetailsSearch(artistId, '1')).json())); | ||
const lastFmInfo: LastFmArtistInfo = | ||
(await (await this.lastfm.getArtistInfo(artistDetails.results[0].artistName)).json()).artist; | ||
const lastFmTopTracks: LastfmTopTracks = | ||
(await (await this.lastfm.getArtistTopTracks(artistDetails.results[0].artistName)).json()).toptracks; | ||
|
||
return ({ | ||
id: artistId, | ||
name: artistDetails.results[0].artistName, | ||
coverImage: artistDetails.results[1].artworkUrl100.replace('100x100bb.jpg', '1600x1600bb.jpg'), | ||
similar: _.map(lastFmInfo.similar.artist, artist => ({ | ||
name: artist.name, | ||
thumbnail: _.get(_.find(artist.image, { size: 'large' }), '#text') | ||
})), | ||
topTracks: _.map(lastFmTopTracks.track, (track) => ({ | ||
name: track.name, | ||
title: track.name, | ||
thumb: artistDetails.results[1].artworkUrl100.replace('100x100bb.jpg', '250x250bb.jpg'), | ||
playcount: track.playcount, | ||
listeners: track.listeners, | ||
artist: track.artist | ||
})), | ||
source: SearchResultsSource.iTunesMusic | ||
}); | ||
} | ||
|
||
async fetchArtistDetailsByName(artistName: string): Promise<ArtistDetails> { | ||
const artists = await this.searchForArtists(artistName); | ||
return this.fetchArtistDetails(artists[0]?.id); | ||
} | ||
|
||
async fetchArtistAlbums(artistId: string): Promise<SearchResultsAlbum[]> { | ||
const artistAlbums = (await ((await iTunes.artistAlbumsSearch(artistId)).json())); | ||
return artistAlbums.results.slice(1).map(album => ({ | ||
id: album.collectionId, | ||
coverImage: album.artworkUrl100.replace('100x100bb.jpg', '250x250bb.jpg'), | ||
thumb: album.artworkUrl100.replace('100x100bb.jpg', '1600x1600bb.jpg'), | ||
title: album.collectionName, | ||
artist: album.artistName, | ||
resourceUrl: album.artistViewUrl, | ||
source: SearchResultsSource.iTunesMusic | ||
})); | ||
} | ||
|
||
async fetchAlbumDetails( | ||
albumId: string, | ||
albumType: 'master' | 'release' | ||
): Promise<AlbumDetails> { | ||
const albumInfo = (await ((await iTunes.albumSongsSearch(albumId, '50')).json())).results; | ||
return Promise.resolve({ | ||
id: albumInfo[0].collectionId, | ||
artist: albumInfo[0].artistName, | ||
title: albumInfo[0].collectionName, | ||
thumb: albumInfo[0].artworkUrl100.replace('100x100bb.jpg', '1600x1600bb.jpg'), | ||
coverImage: albumInfo[0].artworkUrl100.replace('100x100bb.jpg', '250x250bb.jpg'), | ||
year: albumInfo[0].releaseDate, | ||
type: albumType as AlbumType, | ||
tracklist: _.map(albumInfo.slice(1), (episode, index) => new Track ({ | ||
artist: episode.collectionName, | ||
title: episode.trackName, | ||
duration: Math.ceil(episode.trackTimeMillis/1000), | ||
thumbnail: episode.artworkUrl60, | ||
position: index + 1 | ||
})) | ||
}); | ||
} | ||
|
||
async fetchAlbumDetailsByName(albumName: string): Promise<AlbumDetails> { | ||
const albums = await this.searchForReleases(albumName); | ||
return this.fetchAlbumDetails(albums[0]?.id, 'master'); | ||
} | ||
|
||
} | ||
export default iTunesMusicMetaProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import iTunesPodcastMetaProvider from './itunespodcast'; | ||
import * as iTunesMocks from './metaMocks/iTunesPodcastMocks'; | ||
import { Track } from '../..'; | ||
|
||
describe('iTunes podcast metaprovider tests', () => { | ||
it('search for podcasts', async () => { | ||
iTunesMocks.mockPodcastResult(); | ||
const itunesMeta = new iTunesPodcastMetaProvider(); | ||
const response = await itunesMeta.searchForPodcast('Programming Throwdown'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(response).toEqual([ | ||
{ | ||
'id': 427166321, | ||
'coverImage': 'https://is3-ssl.mzstatic.com/image/thumb/Podcasts125/v4/83/e8/a9/83e8a9d5-df87-b19d-7050-55e4ce4df89d/mza_13511678666604160959.jpg/600x600bb.jpg', | ||
'thumb': 'https://is3-ssl.mzstatic.com/image/thumb/Podcasts125/v4/83/e8/a9/83e8a9d5-df87-b19d-7050-55e4ce4df89d/mza_13511678666604160959.jpg/600x600bb.jpg', | ||
'title': 'Programming Throwdown', | ||
'author': 'Patrick Wheeler and Jason Gauci', | ||
'type': 'podcast', | ||
'source': 'iTunesPodcast' | ||
} | ||
]); | ||
}); | ||
|
||
it('search for podcasts details', async () => { | ||
iTunesMocks.mockPodcastEpisodesResult(); | ||
const itunesMeta = new iTunesPodcastMetaProvider(); | ||
const response = await itunesMeta.fetchAlbumDetails('Programming Throwdown'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
const track = new Track ({ | ||
'artist': 'Programming Throwdown', | ||
'duration': 4554, | ||
'position': 1, | ||
'thumbnail': 'https://is3-ssl.mzstatic.com/image/thumb/Podcasts125/v4/83/e8/a9/83e8a9d5-df87-b19d-7050-55e4ce4df89d/mza_13511678666604160959.jpg/60x60bb.jpg', | ||
'title': 'Route Planning with Parker Woodward' | ||
}); | ||
track.uuid = ''; | ||
response.tracklist[0].uuid = ''; | ||
expect(response).toEqual({ | ||
'artist': 'Programming Throwdown', | ||
'coverImage': 'https://is3-ssl.mzstatic.com/image/thumb/Podcasts125/v4/83/e8/a9/83e8a9d5-df87-b19d-7050-55e4ce4df89d/mza_13511678666604160959.jpg/600x600bb.jpg', | ||
'id': 427166321, | ||
'thumb': 'https://is3-ssl.mzstatic.com/image/thumb/Podcasts125/v4/83/e8/a9/83e8a9d5-df87-b19d-7050-55e4ce4df89d/mza_13511678666604160959.jpg/600x600bb.jpg', | ||
'title': 'Programming Throwdown', | ||
'tracklist': [track], | ||
'type': 'master', | ||
'year': '2021-07-07T17:08:00Z' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too bad they don't have images for artists 😔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I tried but it would require a lot of API requests :/