first dump I could find from my laptop

This commit is contained in:
Jonas_Jones 2023-08-31 12:58:45 +02:00
parent 8d949a52b0
commit 034419b9a5
5 changed files with 310 additions and 0 deletions

54
lastfm-scrobble-fetch.py Normal file
View 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}")