Created
December 24, 2024 11:59
-
-
Save Shiroizu/541d7141888a9b22fe53d93cf356d3ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import time | |
import requests | |
def get_rated_songs(user_id: int, extra_params): | |
page = 1 | |
params = { | |
"getTotalCount": "true", | |
"maxResults": "50", | |
"groupByRating": "false", | |
"sort": "RatingDate", | |
} | |
for param in extra_params: | |
params[param] = extra_params[param] | |
rated_songs = [] | |
while True: | |
r = requests.get(f"https://vocadb.net/api/users/{user_id}/ratedSongs", params) | |
songs_in_total = r.json()["totalCount"] | |
r.raise_for_status() | |
pages_total = (songs_in_total // 50) + 1 | |
songs = r.json()["items"] | |
if not songs: | |
break | |
rated_songs.extend(songs) | |
print(f"Page {page}/{pages_total}") | |
page += 1 | |
params["start"] = str(50 * (page - 1)) | |
time.sleep(1) | |
return rated_songs | |
if __name__ == "__main__": | |
USER_ID = | |
COLUMN_DELIMITER = "£" | |
LIST_DELIMITER = "," | |
params = { | |
"fields": "Albums, Artists, PVs, ReleaseEvent, Tags, WebLinks, CultureCodes" | |
} | |
# Skipped: AdditionalNames,Bpm,Lyrics,MainPicture,Names,PVs,ThumbUrl | |
rated_songs = get_rated_songs(USER_ID, params) | |
save_file = f"rated songs (user {USER_ID}).csv" | |
simple_columns = { | |
"Song id": "id", | |
"Name": "defaultName", | |
"Total score": "ratingScore", | |
"Times favorited": "favoritedTimes", | |
"Entry created": "createDate", | |
"Publish date": "publishDate", | |
"PV services": "pvServices", | |
"Song type": "songType", | |
"Entry status": "status", | |
} | |
headers = list(simple_columns.keys()) | |
headers.extend( | |
[ | |
"Rating date", | |
"Own score", | |
"Tag ids", | |
"Album ids", | |
"Producer ids", | |
"Vocalist ids", | |
"Other artist ids", | |
"Number of external links", | |
"Languages", | |
] | |
) | |
with open(save_file, "w", encoding="utf8") as f: | |
print(headers) | |
f.write(COLUMN_DELIMITER.join(headers) + "\n") | |
def list_to_string_or_zero(data: list[str]) -> str: | |
return LIST_DELIMITER.join(data) if data else "0" | |
for song in rated_songs: | |
output_data: list[str] = [] | |
entry = song["song"] | |
for key, value in simple_columns.items(): | |
try: | |
output_data.append(str(entry[value]).replace(COLUMN_DELIMITER, " ")) | |
except KeyError: | |
print(f"{value} not found for {entry['id']}") | |
output_data.append("?") | |
output_data.append(song["date"]) # rating date | |
own_score = 3 if (song["rating"] == "Favorite") else 2 | |
output_data.append(str(own_score)) | |
tag_ids = [] | |
if "tags" in entry: | |
tag_ids = [str(tag["tag"]["id"]) for tag in entry["tags"]] | |
output_data.append(list_to_string_or_zero(tag_ids)) | |
album_ids = [str(album["id"]) for album in entry["albums"]] | |
output_data.append(list_to_string_or_zero(album_ids)) | |
producer_ids = [] | |
vocalist_ids = [] | |
other_artist_ids = [] | |
if "artists" in entry: | |
for artist in entry["artists"]: | |
if "categories" in artist: | |
if artist["categories"] == "Producer": | |
producer_ids.append(str(artist["id"])) | |
elif artist["categories"] == "Vocalist": | |
vocalist_ids.append(str(artist["id"])) | |
else: | |
other_artist_ids.append(str(artist["id"])) | |
output_data.append(list_to_string_or_zero(producer_ids)) | |
output_data.append(list_to_string_or_zero(vocalist_ids)) | |
output_data.append(list_to_string_or_zero(other_artist_ids)) | |
if "webLinks" in entry: | |
output_data.append(str(len(entry["webLinks"]))) | |
else: | |
output_data.append("0") | |
output_data.append(list_to_string_or_zero(entry["cultureCodes"])) | |
assert len(output_data) == len(headers) | |
f.write(COLUMN_DELIMITER.join(output_data) + "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Default output columns = Song id, Name, Total score, Times favorited, Entry created, Publish date, PV services, Song type, Entry status, Rating date, Own score, Tag ids, Album ids, Producer ids, Vocalist ids, Other artist ids, Number of external links, Languages
For data visualization and exploration, one easy solution is using spreadsheet programs such as Libre calc.