-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split fetchers into classes, used inheritance to provide cache
- Loading branch information
1 parent
b9b4025
commit 5c4755d
Showing
5 changed files
with
153 additions
and
117 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
|
||
function createKey(metadata) { | ||
// Create key that can be reasonably expected to be same for all songs in an album | ||
return JSON.stringify({ | ||
artist: metadata.ALBUMARTIST || metadata.artist, | ||
album: metadata.album, | ||
date: metadata.date, | ||
publisher: metadata.publisher, | ||
}); | ||
} | ||
|
||
class ACachingFetcher { | ||
|
||
/** @type {!Object} #knownResults [metadataJSON][service] = {{fetchedFrom: string, artworkUrl: string, joinUrl: string}} */ | ||
#knownResults = {}; | ||
|
||
fetch(metadata) { | ||
let metadataJSON = createKey(metadata); | ||
// Use cached results if possible | ||
if (metadataJSON in this.#knownResults){ | ||
return this.#knownResults[metadataJSON]; | ||
} else { | ||
return (this.#knownResults[metadataJSON] = this.fetchUncached(metadata)); | ||
} | ||
} | ||
} | ||
|
||
module.exports = ACachingFetcher; |
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,37 @@ | ||
|
||
const ACachingFetcher = require('./aCachingFetcher.js'); | ||
const axios = require('axios'); | ||
|
||
/** | ||
* Caching fetcher for covers from iTunes | ||
*/ | ||
class AppleFetcher extends ACachingFetcher { | ||
/** | ||
* Fetch artwork and join URLs if not cached, otherwise return cached | ||
* @param {Object} metadata VLC metadata | ||
* @returns {?{artworkFrom: ?string, artworkUrl: ?string, joinFrom: ?string, joinUrl: ?string}} | ||
*/ | ||
async fetchUncached(metadata) { | ||
if ((metadata.ALBUMARTIST || metadata.artist) && metadata.title) { | ||
const result = await axios.get( | ||
"https://itunes.apple.com/search", | ||
{ | ||
params: { | ||
media: "music", | ||
term: `${metadata.ALBUMARTIST || metadata.artist} ${metadata.title}`, | ||
}, | ||
headers: {"Accept-Encoding": "gzip,deflate,compress" } | ||
}); | ||
if (result.data.resultCount > 0 && result.data.results[0] !== undefined) { | ||
return { | ||
artworkFrom: "Apple Music", | ||
artworkUrl: result.data.results[0].artworkUrl100, | ||
joinFrom: "Apple Music", | ||
joinUrl: result.data.results[0].trackViewUrl | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = AppleFetcher; |
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,40 @@ | ||
|
||
const ACachingFetcher = require('./aCachingFetcher.js'); | ||
const axios = require('axios'); | ||
|
||
/** | ||
* Caching fetcher for covers from iTunes | ||
*/ | ||
class BandcampFetcher extends ACachingFetcher { | ||
/** | ||
* Fetch artwork and join URLs if not cached, otherwise return cached | ||
* @param {Object} metadata VLC metadata | ||
* @returns {?{artworkFrom: ?string, artworkUrl: ?string, joinFrom: ?string, joinUrl: ?string}} | ||
*/ | ||
async fetchUncached(metadata) { | ||
if ((metadata.ALBUMARTIST || metadata.artist) && (metadata.album || metadata.title)) { | ||
const result = await axios.post( | ||
"https://bandcamp.com/api/bcsearch_public_api/1/autocomplete_elastic", | ||
{ | ||
fan_id: null, | ||
full_page: false, | ||
search_filter: "", | ||
search_text: `${metadata.ALBUMARTIST || metadata.artist} ${metadata.album || metadata.title}`, | ||
},{ | ||
headers: {"Accept-Encoding": "gzip,deflate,compress" } | ||
}); | ||
if (result.data.auto.results.length > 0) { | ||
let resultItem = result.data.auto.results[0]; | ||
//console.log(resultTrack); | ||
return { | ||
artworkFrom: "Bandcamp", | ||
artworkUrl: resultItem.img.replace("/img/", "/img/a"), | ||
joinFrom: "Bandcamp", | ||
joinUrl: resultItem.item_url_path | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = BandcampFetcher; |
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,34 @@ | ||
|
||
const ACachingFetcher = require('./aCachingFetcher.js'); | ||
const axios = require('axios'); | ||
|
||
/** | ||
* Caching fetcher for covers from CoverArtArchive | ||
*/ | ||
class CoverArtArchiveFetcher extends ACachingFetcher { | ||
/** | ||
* Fetch artwork and join URLs if not cached, otherwise return cached | ||
* @param {Object} metadata VLC metadata | ||
* @returns {?{artworkFrom: ?string, artworkUrl: ?string, joinFrom: ?string, joinUrl: ?string}} | ||
*/ | ||
async fetchUncached(metadata) { | ||
if ((metadata.MUSICBRAINZ_ALBUMID) && metadata.title) { | ||
const result = await axios.get( | ||
"https://coverartarchive.org/release/" + metadata.MUSICBRAINZ_ALBUMID, | ||
{ | ||
headers: {"Accept-Encoding": "gzip,deflate,compress" } | ||
}); | ||
if (result.data.images[0] !== undefined){ | ||
//console.warn(result.data.images[0].thumbnails.small); | ||
return { | ||
artworkFrom: "Cover Art Archive", | ||
artworkUrl: result.data.images[0].thumbnails.small, | ||
joinFrom: "Cover Art Archive", | ||
joinUrl: result.data.release | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = CoverArtArchiveFetcher; |
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