/bin/spotify-lyrics.py

https://github.com/metalelf0/dot-files · Python · 58 lines · 35 code · 10 blank · 13 comment · 2 complexity · 97b5b1f51a7efedb867d70dd901c7491 MD5 · raw file

  1. #!/usr/bin/env/python3
  2. """ A Python script that displays the lyrics to the currently playing song on
  3. Spotify in your terminal.
  4. File name: spotify-lyrics.py
  5. Author: Caleb Hamilton
  6. Website: https://github.com/cjlh/spotify-lyrics
  7. License: MIT
  8. Python version: 3
  9. Usage:
  10. $ python spotify-lyrics.py
  11. """
  12. import os
  13. import time
  14. import dbus
  15. import requests
  16. old_song_info = None
  17. def get_song_info():
  18. session_bus = dbus.SessionBus()
  19. spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
  20. "/org/mpris/MediaPlayer2")
  21. spotify_metadata = dbus.Interface(spotify_bus,
  22. "org.freedesktop.DBus.Properties").Get(
  23. "org.mpris.MediaPlayer2.Player",
  24. "Metadata"
  25. )
  26. return [
  27. str(spotify_metadata['xesam:artist'][0].title()),
  28. str(spotify_metadata['xesam:title'])
  29. ]
  30. def center_string(s):
  31. terminal_cols = os.popen('tput cols').read()
  32. return str("{:^" + str(int(terminal_cols) + 10) + "}").format(s)
  33. def print_lyrics(artist, title):
  34. print(center_string("\n---\n\n"))
  35. print(center_string("\033[4m" + artist + ": " + title + "\033[0m\n"))
  36. pageurl = "https://makeitpersonal.co/lyrics?artist=" + artist + \
  37. "&title=" + title
  38. lyrics = requests.get(pageurl).text.strip()
  39. print(lyrics)
  40. while True:
  41. song_info = get_song_info()
  42. if song_info != old_song_info:
  43. old_song_info = song_info
  44. os.system('clear')
  45. print_lyrics(song_info[0], song_info[1])
  46. time.sleep(1)