Created
September 20, 2022 08:52
-
-
Save blacklight/da0c9635929fb71a61821b22a838fea8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/platypush/scripts/music/sync.py | |
from logging import getLogger | |
from platypush.context import get_plugin | |
from platypush.event.hook import hook | |
from platypush.message.event.music import NewPlayingTrackEvent | |
logger = getLogger('music_sync') | |
# SQLAlchemy connection string that points to your database | |
music_db_engine = 'postgresql+pg8000://dbuser:dbpass@dbhost/dbname' | |
# Hook that react to NewPlayingTrackEvent events | |
@hook(NewPlayingTrackEvent) | |
def on_new_track_playing(event, **_): | |
track = event.track | |
# Skip if the track has no artist/title specified | |
if not (track.get('artist') and track.get('title')): | |
return | |
logger.info( | |
'Inserting track: %s - %s', | |
track['artist'], track['title'] | |
) | |
db = get_plugin('db') | |
db.insert( | |
engine=music_db_engine, | |
table='tmp_music', | |
records=[ | |
{ | |
'artist': track['artist'], | |
'title': track['title'], | |
'album': track.get('album'), | |
} | |
for track in tracks | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment