Skip to content

Instantly share code, notes, and snippets.

@henryjfry
Created July 10, 2024 09:39
Show Gist options
  • Save henryjfry/236864004f23047715ea048291ed381d to your computer and use it in GitHub Desktop.
Save henryjfry/236864004f23047715ea048291ed381d to your computer and use it in GitHub Desktop.
LASTFM Python Scrobble_WEB_AUTH
import requests
import hashlib
import time
import webbrowser
# Replace with your Last.fm API credentials
API_KEY = 'API_KEY'
API_SECRET = 'API_SECRET'
PASSWORD = 'PASSWORD'
USERNAME = 'USERNAME'
# List of dummy songs to scrobble
songs = [
#{'artist': 'Dummy Artist 2', 'track': 'Dummy Track 2', 'album': 'Dummy Album 2'},
#{'artist': 'Dummy Artist 3', 'track': 'Dummy Track 3', 'album': 'Dummy Album 3'},
#{'artist': 'Dummy Artist 4', 'track': 'Dummy Track 4', 'album': 'Dummy Album 4'},
#{'artist': 'Dummy Artist 5', 'track': 'Dummy Track 5', 'album': 'Dummy Album 5'},
]
def generate_signature(params):
sorted_params = ''.join([f"{key}{params[key]}" for key in sorted(params)])
signature = hashlib.md5((sorted_params + API_SECRET).encode('utf-8')).hexdigest()
return signature
def get_request_token():
params = {
'method': 'auth.gettoken',
'api_key': API_KEY
}
params['api_sig'] = generate_signature(params)
params['format'] = 'json'
response = requests.get('http://ws.audioscrobbler.com/2.0/', params=params)
return response.json()['token']
def get_session_key(token):
params = {
'method': 'auth.getsession',
'api_key': API_KEY,
'token': token
}
params['api_sig'] = generate_signature(params)
params['format'] = 'json'
response = requests.get('http://ws.audioscrobbler.com/2.0/', params=params)
return response.json()['session']['key']
def get_latest_scrobble():
url = f'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={USERNAME}&api_key={API_KEY}&format=json&limit=1'
response = requests.get(url)
data = response.json()
latest_scrobble = data['recenttracks']['track'][0]
latest_timestamp = int(latest_scrobble['date']['uts'])
return latest_timestamp
def scrobble_song(artist, track, album, timestamp, session_key):
params = {
'method': 'track.scrobble',
'artist': artist,
'track': track,
'album': album,
'timestamp': timestamp,
'api_key': API_KEY,
'sk': session_key
}
params['api_sig'] = generate_signature(params)
params['format'] = 'json'
response = requests.post('http://ws.audioscrobbler.com/2.0/', data=params)
return response.json()
# Step 1: Get the request token
token = get_request_token()
# Step 2: Direct the user to authorize the token
auth_url = f"http://www.last.fm/api/auth/?api_key={API_KEY}&token={token}"
print(f"Please authorize this application by visiting this URL: {auth_url}")
webbrowser.open(auth_url)
# Wait for user to authorize the token
input("Press Enter after authorizing the application...")
# Step 3: Get the session key using the authorized token
session_key = get_session_key(token)
print(f"Session Key: {session_key}")
# Main script
latest_timestamp = get_latest_scrobble()
next_timestamp = latest_timestamp + 60 # Start 1 minute after the latest scrobble
for song in songs:
response = scrobble_song(song['artist'], song['track'], song['album'], next_timestamp, session_key)
print(f"Scrobbled {song['track']} by {song['artist']} at {next_timestamp}: {response}")
next_timestamp += 60 # Increment by 1 minute for each subsequent scrobble
time.sleep(1) # Short sleep to avoid rate limiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment