Skip to content

Instantly share code, notes, and snippets.

@alairock
Last active December 20, 2023 04:07
Show Gist options
  • Save alairock/3487aa2479d1771041b79ad34bf1e32e to your computer and use it in GitHub Desktop.
Save alairock/3487aa2479d1771041b79ad34bf1e32e to your computer and use it in GitHub Desktop.
Clean out comedians from lastfm
import pylast
import secretstorage
import requests
from pycookiecheat import chrome_cookies
##########
# CONFIG #
##########
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from https://www.last.fm/api/account/create for Last.fm
API_KEY = "arst" # this is a sample key
API_SECRET = "arst"
# In order to perform a write operation you need to authenticate yourself
username = "user"
password_hash = "pass"
# your lastfm user
lastfm_user = "alairock"
# get the csrf tokenn from any random lastfm request in chrome dev tools, required
CSRF_TOKEN = "INSERT_CSRF_TOKEN_HERE"
##########
# CODE #
##########
password_hash = pylast.md5(password_hash)
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET,
username=username, password_hash=password_hash)
library = network.get_user(lastfm_user).get_library()
def nuke_artist_from_lib(artist_name):
headers = {
'X-Requested-With': 'XMLHttpRequest',
'Accept-Language': 'en-GB,en;q=0.7,en-US;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Accept': '*/*',
'Referer': f'https://www.last.fm/user/{lastfm_user}/library/music/{artist_name.replace(" ", "+")}',
}
data = {
'csrfmiddlewaretoken': CSRF_TOKEN,
'confirm': 'on',
'ajax': 1
}
# Comment out this password stuff if you are not on ubuntu
# Modify it if you are using something else that has a different password than chrome_cookies defaults
bus = secretstorage.dbus_init()
collection = secretstorage.get_default_collection(bus)
ubuntu_system_pass = None
for item in collection.get_all_items():
if item.get_label() == 'Chrome Safe Storage':
ubuntu_system_pass = item.get_secret()
break
cookies = chrome_cookies('https://www.last.fm', password=ubuntu_system_pass)
return requests.post(url=f'https://www.last.fm/user/{lastfm_user}/library/music/{artist_name.replace(" ", "+")}/+delete?is_modal=1', data=data, headers=headers, cookies=cookies)
for artist in library.get_artists(limit=None):
if artist.playcount < 30:
break
if any(tag in ['comedy', 'stand-up', 'stand-up comedy']
for tag in [x.item.name for x in artist.item.get_top_tags(10)]):
r = nuke_artist_from_lib(artist.item.name)
if r.status_code != 200:
print(f'Something went wrong when trying to delete {artist.item.name}')
@alairock
Copy link
Author

alairock commented Sep 5, 2019

This removes "artists" tagged comedy, stand-up or stand-up comedy.

I love comedians, but a lot of the times, they have tracks that are super short, and often I listen to whole albums. I feel this messes with the purpose of tracking music, and I don't honestly care to have them in my scrobbles.

There might be other reasons as to why you want to nuke entire artists' scrobbles from your lastfm and so I am making this gist public, for those who want it. Modify at your own risk, use at your own risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment