From 3ac23d3ed26c908a328b42c45d53947584f7cd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tr=C3=A9meur?= Date: Sat, 29 Jun 2024 22:26:26 +0100 Subject: [PATCH] Add script for scrobbling physical albums --- .gitignore | 1 + scrobble.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 scrobble.py diff --git a/.gitignore b/.gitignore index 85d00a6..5abfae0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +albums.py prompts.html prompts.org __pycache__/* \ No newline at end of file diff --git a/scrobble.py b/scrobble.py new file mode 100644 index 0000000..1916b6a --- /dev/null +++ b/scrobble.py @@ -0,0 +1,119 @@ +import pylast,random,re,sys,time +import albums + +network = pylast.LastFMNetwork(api_key=albums.apikey, api_secret=albums.apisecret, username="litrovers", password_hash=pylast.md5(albums.pw)) # https://input.sh/scrobbling-live-sets-into-last-fm-using-python/ + +thealbums = sorted(albums.albums,key=lambda d: d["name"]) +# albums = sorted(albums,key=lambda d: d["artist"]) + +def countdown(t): # https://stackoverflow.com/a/25189629 + while t: + mins, secs = divmod(t, 60) + timeformat = '{:02d}:{:02d}'.format(mins, secs) + print(timeformat, end='\r') + time.sleep(1) + t -= 1 + +def scrobble(thealbum): + for album in thealbums: + if thealbum == album["code"]: + i = 0 + for tracklist in album["tracks"]: + i += 1 + for item in tracklist: + if " :: " in item: + theartist = re.sub(" :: .*","",item) + thetrack = re.sub(".* :: ","",item) + else: + theartist = album["artist"] + thetrack = item + # last_api_call = network.scrobble(theartist,thetrack,int(time.time())) + print("Scrobbled " + item) + # countdown(tracklist[item]) + if i < len(album["tracks"]): + input("Press enter after changing side") + +def query_yes_no(question, default="yes"): # https://code.activestate.com/recipes/577058/ + """Ask a yes/no question via raw_input() and return their answer. + + "question" is a string that is presented to the user. + "default" is the presumed answer if the user just hits . + It must be "yes" (the default), "no" or None (meaning + an answer is required of the user). + + The "answer" return value is one of "yes" or "no". + """ + valid = {"yes":"yes", "y":"yes", "ye":"yes", + "no":"no", "n":"no"} + if default == None: + prompt = " [y/n] " + elif default == "yes": + prompt = " [Y/n] " + elif default == "no": + prompt = " [y/N] " + else: + raise ValueError("invalid default answer: '%s'" % default) + while 1: + sys.stdout.write(question + prompt) + choice = input().lower() + if default is not None and choice == '': + return default + elif choice in valid.keys(): + return valid[choice] + else: + sys.stdout.write("Please respond with 'yes' or 'no' "\ + "(or 'y' or 'n').\n") + +def selectFromDict(options, name): # https://stackoverflow.com/a/64536882 + index = 0 + indexValidList = [] + print('Select an ' + name + ':') + for optionName in options: + index = index + 1 + indexValidList.extend([options[optionName]]) + print(str(index) + ') ' + optionName) + inputValid = False + while not inputValid: + inputRaw = input(name + ': ') + inputNo = int(inputRaw) - 1 + if inputNo > -1 and inputNo < len(indexValidList): + selected = indexValidList[inputNo] + # print('Selected ' + name + ': ' + selected) + inputValid = True + break + else: + print('Please select a valid ' + name + ' number') + return selected + +def choosealbum(): + randomer = query_yes_no("Pick an album for me?","no") + if randomer == "yes": + revision = "yes" + while revision == "yes": + chosen = random.choice(thealbums) + thechosen = chosen["code"] + try: + print(chosen["album artist"] + ": " + chosen["name"]) + except: + print(chosen["artist"] + ": " + chosen["name"]) + revision = query_yes_no("Roll again?","no") + else: + process = False + while process == False: + firstletter = input("Type the first letter of the album name\n") + letteralbums = {} + for album in thealbums: + process = True + if album["name"][0].lower() == str(firstletter): + try: + letteralbums[album["name"] + " (" + album["album artist"] + ")"] = album["code"] + except: + letteralbums[album["name"] + " (" + album["artist"] + ")"] = album["code"] + if len(letteralbums) == 0: + process = False + print("No albums found") + thechosen = selectFromDict(letteralbums, "album") + scrobble(thechosen) + +if __name__ == "__main__": + choosealbum()