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

@ -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()