Files
music-file-sync-2/last_fm.py
2025-08-21 00:25:48 +10:00

30 lines
893 B
Python

import json
from typing import List
class LastFm:
def __init__(self, credentials_file):
with open(credentials_file, 'r') as f:
credentials = json.loads(f.read())
self.lastfm = pylast.LastFMNetwork(
api_key=credentials['api_kay'],
api_secret=credentials['api_secret']
)
def get_recent_plays(self, username):
self.lastfm.get_user(username).get_recent_tracks(limit=200)
def get_top_artists(self, username: str, min_scrobbles: int) -> List[pylast.TopItem]:
artists = []
page = 0
while True:
artists_page = self.lastfm.get_user(username).get_top_artists()
for artist in artists_page:
if (artist.weight > min_scrobbles):
artists.append(artist)
else:
break