mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-25 19:19:19 +02:00
first dump I could find from my laptop
This commit is contained in:
parent
8d949a52b0
commit
034419b9a5
5 changed files with 310 additions and 0 deletions
54
lastfm-scrobble-fetch.py
Normal file
54
lastfm-scrobble-fetch.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import requests
|
||||
from datetime import datetime
|
||||
import json
|
||||
import urllib.parse
|
||||
import time
|
||||
|
||||
starttime = time.time()
|
||||
|
||||
API_KEY = ""
|
||||
USERNAME = ""
|
||||
|
||||
# Get the total number of scrobbles for the user
|
||||
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user={USERNAME}&api_key={API_KEY}&format=json"
|
||||
response = requests.get(url)
|
||||
data = response.json()
|
||||
total_scrobbles = int(data['user']['playcount'])
|
||||
|
||||
# Calculate the number of pages required for pagination
|
||||
page_count = (total_scrobbles + 200 - 1) // 200
|
||||
|
||||
# Fetch all scrobbles by paginating through the API responses
|
||||
all_scrobbles = []
|
||||
for page in range(1, page_count + 1):
|
||||
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={USERNAME}&api_key={API_KEY}&format=json&page={page}"
|
||||
response = requests.get(url)
|
||||
data = response.json()
|
||||
tracks = data['recenttracks']['track']
|
||||
|
||||
for track in tracks:
|
||||
artist = urllib.parse.quote(track['artist']['#text'])
|
||||
song = urllib.parse.quote(track['name'])
|
||||
|
||||
track_url = f"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={API_KEY}&artist={artist}&track={song}&format=json"
|
||||
#print(track_url)
|
||||
track_response = requests.get(track_url)
|
||||
track_data = track_response.json()
|
||||
duration = int(track_data['track']['duration'])
|
||||
track['duration'] = duration
|
||||
all_scrobbles.append(track)
|
||||
|
||||
# Save the song list to a JSON file
|
||||
output_file = "song_list.json"
|
||||
with open(output_file, "w") as file:
|
||||
json.dump(all_scrobbles, file)
|
||||
|
||||
total_duration = sum(track['duration'] for track in all_scrobbles)
|
||||
|
||||
print(total_duration)
|
||||
# Convert the total listening time to a human-readable format
|
||||
total_time = datetime.utcfromtimestamp(total_duration).strftime('%H:%M:%S')
|
||||
|
||||
print(f"Overall Listening Time: {total_time}")
|
||||
|
||||
print(f"Took: {time.time() - starttime}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue