Skip to content

Instantly share code, notes, and snippets.

@droserasprout
Created June 13, 2017 14:30
Show Gist options
  • Select an option

  • Save droserasprout/36236424c3a592bb3c0d15acfe33f8d2 to your computer and use it in GitHub Desktop.

Select an option

Save droserasprout/36236424c3a592bb3c0d15acfe33f8d2 to your computer and use it in GitHub Desktop.
Batch Last.fm scrobbler for quick start
#!/usr/bin/python -u
import sys, os, time
try:
import pylast, taglib
except:
print("Please install python-pylast and python-pytaglib first.", file=sys.stderr)
API_KEY = "b01fb9eac7dc07d6712dee8986af73ce"
API_SECRET = "887363c94049af9f82c1dd9ea76e0c12"
def main():
if len(sys.argv) != 4 or (not os.path.isdir(sys.argv[1])):
print("usage: folderscrobbler.py <path> <login> <password>", file=sys.stderr)
else:
#parsing arguments
path = sys.argv[1]
username = sys.argv[2]
password_hash = pylast.md5(sys.argv[3])
timestamp = int(time.time())#-298080
#authentificating
try:
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash)
except:
print("Invalid username/password", file=sys.stderr)
sys.exit(2)
#generating list of audio files
filepaths = []
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(("mp3", "flac", "ogg", "aac")):
filepaths.append(os.path.join(root, name))
#scrobbling
if len(filepaths) == 0:
print("Nothing to do.", file=stderr)
else:
for i, curr_file_path in enumerate(filepaths):
perc = round(1.0 * i / len(filepaths) * 100, 2)
print("\rScrobbling... ", perc, "%", sep='', end='')
try:
curr_file = taglib.File(curr_file_path)
network.scrobble(artist=curr_file.tags["ARTIST"][0], title=curr_file.tags["TITLE"][0], album=curr_file.tags["ALBUM"][0], track_number=curr_file.tags["TRACKNUMBER"][0].split("/")[0].split("-")[0], duration=curr_file.length, timestamp=timestamp)
except:
print("\nCan't scrobble", curr_file_path)
timestamp -= 30
print("\nDone!")
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment