This commit is contained in:
mugman 2025-11-02 02:17:32 -05:00
parent eeaa90724d
commit 5c6596b36d
2 changed files with 29 additions and 8 deletions

View file

@ -12,4 +12,5 @@ pyobjc-framework-applescriptobjc==11.1
pyobjc-framework-cocoa==11.1 pyobjc-framework-cocoa==11.1
python-dotenv==1.1.1 python-dotenv==1.1.1
sniffio==1.3.1 sniffio==1.3.1
tqdm==4.67.1
typing-extensions==4.15.0 typing-extensions==4.15.0

View file

@ -7,6 +7,7 @@ import applescript # py-applescript
import time import time
import pylast import pylast
from dotenv import dotenv_values from dotenv import dotenv_values
from tqdm import tqdm
config = dotenv_values(".env") config = dotenv_values(".env")
@ -17,7 +18,7 @@ lfm = pylast.LastFMNetwork(
password_hash=pylast.md5(config["PASSWORD"]), password_hash=pylast.md5(config["PASSWORD"]),
) )
tell_iTunes = applescript.AppleScript(''' tell_iTunes = applescript.AppleScript("""
on is_running(appName) on is_running(appName)
tell application "System Events" to (name of processes) contains appName tell application "System Events" to (name of processes) contains appName
end is_running end is_running
@ -38,9 +39,17 @@ on Playing()
end tell end tell
end if end if
end Playing end Playing
''') """)
old_id = tracked_id = -1 old_id = tracked_id = -1
runtime = 0 runtime = 0
progress = tqdm()
progress.set_postfix(scrobbled=False)
def print(*args):
progress.write(" ".join(map(str, args)))
while True: while True:
time.sleep(1) time.sleep(1)
try: try:
@ -53,11 +62,14 @@ while True:
id, state, artist, song, album, duration, album_artist = out id, state, artist, song, album, duration, album_artist = out
if hasattr(duration, "code"): if hasattr(duration, "code"):
continue continue
print(f"{runtime}/{round(duration)}", end="\r")
if old_id != id: if old_id != id:
print("new song", old_id, id, song) progress.set_description(song)
progress.set_postfix(scrobbled=False)
runtime = 0 runtime = 0
old_id = id old_id = id
progress.n = runtime
progress.total = round(duration)
if tracked_id == id: if tracked_id == id:
continue continue
if state.code != b"kPSP": if state.code != b"kPSP":
@ -68,6 +80,14 @@ while True:
continue continue
runtime += 1 runtime += 1
if (runtime >= duration / 2 or runtime > 4 * 60) and tracked_id != id: if (runtime >= duration / 2 or runtime > 4 * 60) and tracked_id != id:
print("scrobbling") progress.set_postfix(scrobbled=True)
tracked_id = id 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()