Created
July 10, 2024 09:38
-
-
Save henryjfry/33730830a565719f838610ce1b63f19a to your computer and use it in GitHub Desktop.
LASTFM Python Scrobble_USERNAME_PASSWORD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import hashlib | |
import time | |
# Replace with your Last.fm API credentials | |
API_KEY = 'API_KEY' | |
API_SECRET = 'API_SECRET' | |
USERNAME = 'username' | |
PASSWORD = 'password' | |
# List of dummy songs to scrobble | |
songs = [ | |
{'artist': "ARTIST", 'track': "TRACK_NAME", 'album': 'ALBUM'}, | |
{'artist': "ARTIST", 'track': "TRACK_NAME", 'album': 'ALBUM'} | |
] | |
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_mobile_session(username, password): | |
params = { | |
'method': 'auth.getMobileSession', | |
'username': username, | |
'password': password, | |
'api_key': API_KEY | |
} | |
params['api_sig'] = generate_signature(params) | |
params['format'] = 'json' | |
response = requests.post('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() | |
# Get the session key using the mobile session method | |
session_key = get_mobile_session(USERNAME, PASSWORD) | |
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