A Python interface to Last.fm and Libre.fm for music data, scrobbling, and social features.
—
Network initialization, authentication methods, session management, and configuration options for PyLast. This covers the core infrastructure for accessing Last.fm and Libre.fm APIs with proper authentication and advanced configuration.
Core network objects that serve as entry points to Last.fm and Libre.fm APIs, managing authentication, API communication, and configuration.
class LastFMNetwork:
"""
Main entry point for Last.fm API access.
Args:
api_key (str): Last.fm API key
api_secret (str): Last.fm API secret
session_key (str, optional): Pre-generated session key
username (str, optional): Username for authentication
password_hash (str, optional): MD5 hash of password
token (str, optional): Authentication token to retrieve session
"""
def __init__(self, api_key="", api_secret="", session_key="", username="", password_hash="", token=""): ...
def get_artist(self, artist_name: str) -> Artist: ...
def get_track(self, artist: str, title: str) -> Track: ...
def get_album(self, artist: str, title: str) -> Album: ...
def get_user(self, username: str) -> User: ...
def get_authenticated_user(self) -> AuthenticatedUser: ...
def get_country(self, country_name: str) -> Country: ...
def get_tag(self, name: str) -> Tag: ...
class LibreFMNetwork:
"""
Preconfigured network for Libre.fm API access.
Args:
api_key (str): Libre.fm API key
api_secret (str): Libre.fm API secret
session_key (str, optional): Pre-generated session key
username (str, optional): Username for authentication
password_hash (str, optional): MD5 hash of password
"""
def __init__(self, api_key="", api_secret="", session_key="", username="", password_hash=""): ...Authentication and session key generation for secure API access, supporting multiple authentication flows.
class SessionKeyGenerator:
"""
Handles session key generation for authentication.
Args:
network: Network instance (LastFMNetwork or LibreFMNetwork)
"""
def __init__(self, network): ...
def get_web_auth_url(self) -> str:
"""Generate web authentication URL for user authorization."""
def get_web_auth_session_key(self, url: str, token: str = None) -> str:
"""
Get session key from web authentication.
Args:
url (str): Authentication URL
token (str, optional): Authentication token
Returns:
str: Session key for API access
"""
def get_web_auth_session_key_username(self, url: str = None, token: str = None) -> tuple[str, str]:
"""
Get session key and username from web authentication.
Args:
url (str, optional): Authentication URL
token (str, optional): Authentication token
Returns:
tuple[str, str]: (session_key, username)
"""
def get_session_key(self, username: str, password_hash: str) -> str:
"""
Get session key from username and password hash.
Args:
username (str): User's username
password_hash (str): MD5 hash of user's password
Returns:
str: Session key for API access
"""Advanced configuration options for proxy support, caching, and rate limiting.
# Network configuration methods (available on LastFMNetwork and LibreFMNetwork)
def enable_proxy(self, proxy: str | dict) -> None:
"""
Enable proxy support for API requests.
Args:
proxy (str | dict): Proxy server URL string or dict of proxy settings.
Multiple proxies can be passed as a dict for different protocols.
"""
def disable_proxy(self) -> None:
"""Disable proxy support."""
def is_proxy_enabled(self) -> bool:
"""Returns True if web proxy is enabled."""
def enable_caching(self, file_path: str = None) -> None:
"""
Enable caching of web service calls.
Args:
file_path (str, optional): Path to cache file. If None, uses temporary file.
"""
def disable_caching(self) -> None:
"""Disable caching of web service calls."""
def is_caching_enabled(self) -> bool:
"""Returns True if caching is enabled."""
def enable_rate_limit(self) -> None:
"""Enable rate limiting to comply with Last.fm API terms (0.2s between calls)."""
def disable_rate_limit(self) -> None:
"""Disable rate limiting for faster API calls."""
def is_rate_limited(self) -> bool:
"""Return True if web service calls are rate limited."""Lookup music objects using MusicBrainz IDs for precise identification.
def get_artist_by_mbid(self, mbid: str) -> Artist:
"""
Get artist by MusicBrainz ID.
Args:
mbid (str): MusicBrainz artist ID
Returns:
Artist: Artist object
"""
def get_album_by_mbid(self, mbid: str) -> Album:
"""
Get album by MusicBrainz ID.
Args:
mbid (str): MusicBrainz album ID
Returns:
Album: Album object
"""
def get_track_by_mbid(self, mbid: str) -> Track:
"""
Get track by MusicBrainz ID.
Args:
mbid (str): MusicBrainz track ID
Returns:
Track: Track object
"""import pylast
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
# Simple setup without authentication (read-only access)
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
# Setup with username/password authentication
username = "your_username"
password_hash = pylast.md5("your_password")
network = pylast.LastFMNetwork(
api_key=API_KEY,
api_secret=API_SECRET,
username=username,
password_hash=password_hash
)import pylast
import webbrowser
import time
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
# Generate authentication URL
skg = pylast.SessionKeyGenerator(network)
auth_url = skg.get_web_auth_url()
print(f"Please authorize this application: {auth_url}")
webbrowser.open(auth_url)
# Wait for user authorization and get session key
input("Press Enter after authorizing the application...")
try:
session_key = skg.get_web_auth_session_key(auth_url)
network.session_key = session_key
print("Authentication successful!")
except pylast.WSError as e:
print(f"Authentication failed: {e}")# Enable caching for better performance
network.enable_caching("/path/to/cache/file")
# Enable proxy support
network.enable_proxy("proxy.example.com", 8080)
# Enable rate limiting (recommended for production)
network.enable_rate_limit()
# Configure for Libre.fm instead
libre_network = pylast.LibreFMNetwork(
api_key=LIBRE_API_KEY,
api_secret=LIBRE_API_SECRET,
username=username,
password_hash=password_hash
)PyLast supports multiple authentication methods:
Choose the appropriate method based on your application's security requirements and user experience needs.
Install with Tessl CLI
npx tessl i tessl/pypi-pylast