add all the things

This commit is contained in:
mugman 2025-10-20 10:44:48 -04:00
commit c51a74d5dd
4 changed files with 87 additions and 0 deletions

69
scrobbler.py Normal file
View file

@ -0,0 +1,69 @@
"""
scrobbler
https://gist.github.com/mvrpl/53f1e377315e6db26c47
"""
import applescript # py-applescript
import time
import pylast
from dotenv import dotenv_values
config = dotenv_values(".env")
lfm = pylast.LastFMNetwork(
api_key=config["LFM_KEY"],
api_secret=config["LFM_SECRET"],
username=config["USERNAME"],
password_hash=pylast.md5(config["PASSWORD"]),
)
tell_iTunes = applescript.AppleScript('''
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
on Playing()
set iTunesRunning to is_running("Music")
if iTunesRunning then
tell application "Music"
set songId to the database ID of the current track
set songState to the player state
set songTitle to the name of the current track
set songArtist to the artist of the current track
set songAlbum to the album of the current track
set songAlbumArtist to the album artist of the current track
set songDuration to the duration of the current track
set result to songId & songState & songArtist & songTitle & songAlbum & songDuration & songAlbumArtist
return result
end tell
end if
end Playing
''')
old_id = tracked_id = -1
runtime = 0
while True:
time.sleep(1)
try:
out = tell_iTunes.call("Playing")
except Exception as e:
print(e)
continue
id, state, artist, song, album, duration, album_artist = out
print(f"{runtime}/{round(duration)}", end="\r")
if old_id != id:
print("new song", old_id, id, song)
runtime = 0
old_id = id
if tracked_id == id:
continue
if state.code != b"kPSP":
continue
if duration < 30:
print("not tracking", duration)
old_id = tracked_id = id
continue
runtime += 1
if (runtime >= duration/2 or runtime > 4*60) and tracked_id != id:
print("scrobbling")
tracked_id = id
lfm.scrobble(artist, song, int(time.time() - runtime), album, album_artist, duration)