-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch liked songs and metadata on init
* extracts out the functions for fetching all liked songs and their metadata into a common file, so that it can be used in both `export` and `init` * updates README to remove the note asking users to run an export before `show-genres`. Liked songs and their metadata are fetched on init itself, so `show-genres` can be run after an init without needing an export first.
- Loading branch information
Showing
4 changed files
with
116 additions
and
107 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
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,81 @@ | ||
const { | ||
fetchLikedSongs, | ||
fetchArtists, | ||
fetchAudioFeatures, | ||
} = require("../libs/api"); | ||
const { | ||
saveTrack, | ||
getFetchedArtists, | ||
batchSaveGenres, | ||
getFetchedTracks, | ||
batchSaveAudioFeatures, | ||
} = require("../libs/db"); | ||
const pc = require("picocolors"); | ||
|
||
exports.fetchAllLikedSongs = async () => { | ||
const header = "Fetching Liked songs..."; | ||
process.stdout.write(header); | ||
|
||
let count = 0; | ||
for await (let [savedTrackObj, total] of fetchLikedSongs()) { | ||
saveTrack(savedTrackObj); | ||
|
||
count++; | ||
|
||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
process.stdout.write(pc.dim(`[${count}/${total}]`)); | ||
} | ||
|
||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
console.log(pc.green("Done.")); | ||
}; | ||
|
||
exports.fetchGenres = async () => { | ||
const header = "Fetching genre data..."; | ||
process.stdout.write(header); | ||
|
||
let count = 0; | ||
for await (let artistIDs of getFetchedArtists()) { | ||
const artists = await fetchArtists(artistIDs); | ||
const genreData = artists.map((artist) => { | ||
return { artistID: artist.id, genres: artist.genres }; | ||
}); | ||
|
||
batchSaveGenres(genreData); | ||
|
||
count += artistIDs.length; | ||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
process.stdout.write(pc.dim(`[${count}] artists`)); | ||
} | ||
|
||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
process.stdout.write(pc.green(`Done.\n`)); | ||
}; | ||
|
||
exports.fetchAllAudioFeatures = async () => { | ||
const header = "Fetching audio features..."; | ||
process.stdout.write(header); | ||
|
||
let count = 0; | ||
for await (let trackIDs of getFetchedTracks({}, 100)) { | ||
const audioFeatures = await fetchAudioFeatures(trackIDs); | ||
const trackFeatures = audioFeatures.map((featuresObj) => { | ||
return { trackID: featuresObj.id, audioFeatures: featuresObj }; | ||
}); | ||
|
||
batchSaveAudioFeatures(trackFeatures); | ||
|
||
count += trackIDs.length; | ||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
process.stdout.write(pc.dim(`[${count}] songs`)); | ||
} | ||
|
||
process.stdout.cursorTo(header.length); | ||
process.stdout.clearLine(+1); | ||
process.stdout.write(pc.green(`Done.\n`)); | ||
}; |
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