From 5c6596b36ddd2e8a204b56fc4a971112cd7a5788 Mon Sep 17 00:00:00 2001 From: mugman Date: Sun, 2 Nov 2025 02:17:32 -0500 Subject: [PATCH] tqdm --- requirements.txt | 1 + scrobbler.py | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index f5cff3c..e1310cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ pyobjc-framework-applescriptobjc==11.1 pyobjc-framework-cocoa==11.1 python-dotenv==1.1.1 sniffio==1.3.1 +tqdm==4.67.1 typing-extensions==4.15.0 diff --git a/scrobbler.py b/scrobbler.py index 7546c7d..0b4660d 100644 --- a/scrobbler.py +++ b/scrobbler.py @@ -3,10 +3,11 @@ scrobbler https://gist.github.com/mvrpl/53f1e377315e6db26c47 """ -import applescript # py-applescript +import applescript # py-applescript import time import pylast from dotenv import dotenv_values +from tqdm import tqdm config = dotenv_values(".env") @@ -17,7 +18,7 @@ lfm = pylast.LastFMNetwork( password_hash=pylast.md5(config["PASSWORD"]), ) -tell_iTunes = applescript.AppleScript(''' +tell_iTunes = applescript.AppleScript(""" on is_running(appName) tell application "System Events" to (name of processes) contains appName end is_running @@ -38,9 +39,17 @@ on Playing() end tell end if end Playing -''') +""") old_id = tracked_id = -1 runtime = 0 +progress = tqdm() +progress.set_postfix(scrobbled=False) + + +def print(*args): + progress.write(" ".join(map(str, args))) + + while True: time.sleep(1) try: @@ -53,11 +62,14 @@ while True: id, state, artist, song, album, duration, album_artist = out if hasattr(duration, "code"): continue - print(f"{runtime}/{round(duration)}", end="\r") if old_id != id: - print("new song", old_id, id, song) + progress.set_description(song) + progress.set_postfix(scrobbled=False) runtime = 0 old_id = id + progress.n = runtime + progress.total = round(duration) + if tracked_id == id: continue if state.code != b"kPSP": @@ -67,7 +79,15 @@ while True: old_id = tracked_id = id continue runtime += 1 - if (runtime >= duration/2 or runtime > 4*60) and tracked_id != id: - print("scrobbling") + if (runtime >= duration / 2 or runtime > 4 * 60) and tracked_id != id: + progress.set_postfix(scrobbled=True) tracked_id = id - lfm.scrobble(artist=artist, title=song, timestamp=int(time.time() - runtime), album=album, album_artist=album_artist, duration=duration) + lfm.scrobble( + artist=artist, + title=song, + timestamp=int(time.time() - runtime), + album=album, + album_artist=album_artist, + duration=duration, + ) + progress.display()