Skip to main content

A Python package for interacting with the Last.fm API

Project description

                                                       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.

Installation

You can install pyscrobble via pip:

pip install pyscrobble

Usage

Here is how to use the pyscrobble library to interact with the Last.fm API:

Importing the Library

from pyscrobble import Handler

Initializing the Handler

api_key = 'your_lastfm_api_key'
handler = Handler(api_key)

Fetching Artist Information

artist = handler.get_artist('Radiohead')
print(artist)

Fetching Album Information

album = handler.get_album('OK Computer', 'Radiohead')
print(album)

Fetching Track Information

track = handler.get_track('Creep', 'Radiohead')
print(track)

Fetching User Information

user = handler.get_user('someusername')
print(user)

Fetching Top Artists for a User

top_artists = handler.get_top_artists('someusername')
for artist in top_artists:
    print(artist)

Example with a Discord Bot

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')

Methods

get_artist(artist_name)

Fetches information about an artist.

get_album(album_name, artist_name)

Fetches information about an album.

get_track(track_name, artist_name)

Fetches information about a track.

get_user(user_name)

Fetches information about a user.

get_top_artists(user_name, period='overall')

Fetches the top artists for a user for a specified period.

get_top_albums(user_name, period='overall')

Fetches the top albums for a user for a specified period.

get_top_tracks(user_name, period='overall')

Fetches the top tracks for a user for a specified period.

get_friends(user_name)

Fetches a list of friends for a user.

get_neighbours(user_name)

Fetches a list of neighbours for a user.

get_now_playing(user_name)

Fetches the currently playing track for a user.

get_recent_tracks(user_name)

Fetches recent tracks listened to by a user.

get_loved_tracks(user_name)

Fetches tracks that a user has loved.

get_attended_events(user_name)

Fetches events attended by a user.

get_shouts(user_name)

Fetches shouts (user comments) for a user.

License

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyscrobble-0.0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyscrobble-0.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pyscrobble-0.0.2.tar.gz.

File metadata

  • Download URL: pyscrobble-0.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for pyscrobble-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e21e2b779d66308e14149f31263a523784804453bc7175b7f3295492a1b4f5d3
MD5 f4d4793a03226267e0307e01f4c46980
BLAKE2b-256 3d88790903490299aab6d52230b0a38cfe4034fa85f53d69753ce9adf97b5f0f

See more details on using hashes here.

File details

Details for the file pyscrobble-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyscrobble-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for pyscrobble-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ac5dec61f31c16b29a0ed86a850354fd25bb49d96c20e6059f889a396ff1f380
MD5 c59d7087de70036954ea02c2f2a1dfac
BLAKE2b-256 9c3229e74311a2a8be2830e3cfaae4867a96ffe19110b7f129c895093cd6ade7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page