**pyscrobble**
pyscrobble is a Python library for interacting with the Last.fm API. It allows you to fetch detailed information about artists, albums, tracks, and users. The library includes rate limiting to avoid exceeding API usage limits and supports various Last.fm endpoints.
You can install pyscrobble
via pip:
pip install pyscrobble
Here’s how to use the pyscrobble
library to interact with the Last.fm API:
from pyscrobble import Handler
api_key = 'your_lastfm_api_key'
handler = Handler(api_key)
artist = handler.get_artist('Radiohead')
print(artist)
album = handler.get_album('OK Computer', 'Radiohead')
print(album)
track = handler.get_track('Creep', 'Radiohead')
print(track)
user = handler.get_user('someusername')
print(user)
top_artists = handler.get_top_artists('someusername')
for artist in top_artists:
print(artist)
Here’s a simple example of how to use pyscrobble
in a Discord bot using discord.py
:
import discord
from discord.ext import commands
from pyscrobble import Handler
# Initialize the bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Initialize the Handler with your Last.fm API key
api_key = 'your_lastfm_api_key'
handler = Handler(api_key)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command(name='artist')
async def fetch_artist(ctx, artist_name: str):
try:
artist = handler.get_artist(artist_name)
await ctx.send(f"Artist: {artist.name}\nBio: {artist.bio}")
except Exception as e:
await ctx.send(f"Error: {str(e)}")
@bot.command(name='album')
async def fetch_album(ctx, album_name: str, artist_name: str):
try:
album = handler.get_album(album_name, artist_name)
await ctx.send(f"Album: {album.title}\nArtist: {album.artist.name}\nRelease Date: {album.release_date}\nURL: {album.url}")
except Exception as e:
await ctx.send(f"Error: {str(e)}")
@bot.command(name='track')
async def fetch_track(ctx, track_name: str, artist_name: str):
try:
track = handler.get_track(track_name, artist_name)
await ctx.send(f"Track: {track.title}\nArtist: {track.artist.name}\nAlbum: {track.album.title}\nDuration: {track.duration} seconds\nURL: {track.url}")
except Exception as e:
await ctx.send(f"Error: {str(e)}")
# Run the bot with your Discord token
bot.run('your_discord_bot_token')
Fetches information about an artist.
Fetches information about an album.
Fetches information about a track.
Fetches information about a user.
Fetches the top artists for a user for a specified period.
Fetches the top albums for a user for a specified period.
Fetches the top tracks for a user for a specified period.
Fetches a list of friends for a user.
Fetches a list of neighbours for a user.
Fetches the currently playing track for a user.
Fetches recent tracks listened to by a user.
Fetches tracks that a user has loved.
Fetches events attended by a user.
Fetches shouts (user comments) for a user.
This project is licensed under the MIT License - see the LICENSE file for details.
Replace placeholders like your_package_name
, your_lastfm_api_key
, and your_discord_bot_token
with your actual package name and credentials.