44# Inspired by: https://github.com/Alexays/Waybar/blob/
55# master/resources/custom_modules/mediaplayer.py
66import argparse
7+ import io
78import json
89import logging
910import math
@@ -13,6 +14,7 @@ import subprocess
1314import sys
1415import time
1516import urllib
17+ from collections import namedtuple
1618from functools import partial
1719from pathlib import Path
1820
@@ -286,23 +288,36 @@ def update_metadata(player, metadata, manager):
286288 logger .debug ("Returning" )
287289
288290
289- def get_new_lyrics (artist , title ):
290- def finalize ():
291- global lyrics
292- lyrics ["max_len" ] = max ([len (x ) for x in lyrics ["text" ]])
293- global lyrics
294- lyrics = {"secs" : [0 ], "text" : ["[INTRO]" ], "last_ix" : - 1 }
291+ def download_lyrics (artist , title ):
292+ syncedlyrics .logger .setLevel (logging .INFO )
293+ stream = io .StringIO ()
294+ syncedlyrics .logger .addHandler (logging .StreamHandler (stream ))
295295 lt = syncedlyrics .search (
296296 f"{ artist } { title } " ,
297297 allow_plain_format = False ,
298298 providers = config ["lyrics_providers" ],
299299 )
300- if lt is None :
300+ lt = lt .split ("\n " )
301+ provider = stream .getvalue ().split ("on " )[1 ].strip ()
302+ stream .close ()
303+ SyncedLyrics = namedtuple ("SyncedLyrics" , ["lt" , "provider" ])
304+ return SyncedLyrics (lt , provider )
305+
306+
307+ def get_new_lyrics (artist , title ):
308+ global lyrics
309+ lyrics = {
310+ "secs" : [0 ],
311+ "text" : ["[INTRO]" ],
312+ "last_ix" : - 1 ,
313+ "max_len" : 7 ,
314+ "provider" : None ,
315+ }
316+ lyr = download_lyrics (artist , title )
317+ if lyr .lt is None :
301318 logger .debug ("No lyrics, returning" )
302- finalize ()
303319 return
304- lt = lt .split ("\n " )
305- for i , line in enumerate (lt ):
320+ for i , line in enumerate (lyr .lt ):
306321 m = re .match (r"\[(.*)\](.*)" , line )
307322 timing = m .group (1 )
308323 timing2 = timing .split (":" )
@@ -317,7 +332,8 @@ def get_new_lyrics(artist, title):
317332 logger .debug (i , line , "->" , "[" + str (secs ) + "] " , text )
318333 lyrics ["text" ].append ("[OUTRO]" )
319334 lyrics ["secs" ].append (lyrics ["secs" ][- 1 ] + 1 )
320- finalize ()
335+ lyrics ["max_len" ] = max ([len (x ) for x in lyrics ["text" ]])
336+ lyrics ["provider" ] = lyr .provider
321337
322338
323339def left_text (s ):
@@ -421,22 +437,26 @@ def update_progressbar(manager, player):
421437 and metadata ["length" ] == last_metadata ["length" ]
422438 and lyrics_same
423439 )
424- if same_song and not pbar_needs_update and not rot_needs_update :
440+ # CHeck if we need to update
441+ if same_song and \
442+ not pbar_needs_update and \
443+ not rot_needs_update :
425444 logger .debug (
426445 "Song is the same and neither the text "
427446 "nor the progressbar nor the lyrics needs an update. Returning"
428447 )
429448 return False
430449 # Write output
431- # Widget text
450+ # - Widget text
432451 widget_text = gen_widget_text (player )
433- # Tooltip text
452+ # - Tooltip text
434453 s = [metadata ["title" ], metadata ["artist" ], metadata ["album" ]]
435- # lyrics
454+ # -- Lyrics
436455 curr_line = None
437456 if lyrics :
438457 ll = lyrics .get ("max_len" , 3 )
439458 s .append ("-" * ll )
459+ s .append ("[" + lyrics .get ("provider" , "" )+ "]" )
440460
441461 # DEBUG
442462 # print(f"lyrics['secs']={lyrics['secs']}")
@@ -459,7 +479,7 @@ def update_progressbar(manager, player):
459479 s .append (lyrics ["text" ][t ])
460480 if t == lyrics_ix :
461481 curr_line = len (s ) - 1
462- # tooltip
482+ # -- Tooltip
463483 center_text (s , lyrics .get ("max_len" , None ))
464484 if curr_line :
465485 s [curr_line ] = "<b>" + s [curr_line ] + "</b>"
0 commit comments