Skip to content

Instantly share code, notes, and snippets.

@blacklight
Created September 20, 2022 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacklight/da0c9635929fb71a61821b22a838fea8 to your computer and use it in GitHub Desktop.
Save blacklight/da0c9635929fb71a61821b22a838fea8 to your computer and use it in GitHub Desktop.
# ~/.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