Skip to content

Commit

Permalink
fix start error when mpd not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugran committed Apr 23, 2022
1 parent 354cde2 commit 42cc8d3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lyrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

CONFIG_PATH = Path.home().joinpath('.config', 'lyrics-in-terminal','lyrics.cfg')

__version__ = '1.4.3'
__version__ = '1.4.4-dev'

if not CONFIG_PATH.exists():
from shutil import copy
Expand Down
2 changes: 1 addition & 1 deletion lyrics/lyrics_in_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def wrapper(*args, **kwargs):
except curses.error as err:
print('Please increase terminal window size!')
except:
print('Unexpected exception occurred.', sys.exc_info()[0])
print('Unexpected exception occurred.', sys.exc_info())

return wrapper

Expand Down
82 changes: 52 additions & 30 deletions lyrics/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@

import dbus
import re
from mpd import MPDClient as mpd

MPD_ENABLED = False

try:
# making mpd an optional dependency
from mpd import MPDClient as mpd
MPD_ENABLED = True
except ImportError:
pass


class Player:
def __init__(self, name, source, autoswitch, mpd_connect, **kwargs):
Expand All @@ -20,14 +29,17 @@ def __init__(self, name, source, autoswitch, mpd_connect, **kwargs):
self.mpd_host = mpd_connect[0] or '127.0.0.1'
self.mpd_port = mpd_connect[1] or 6600
self.mpd_pass = mpd_connect[2] or ''

self.mpd_enabled = MPD_ENABLED
self.update()

def check_playing(self):
''' checks playing status of current player
'''

if self.player_interface:
status = self.player_interface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
status = self.player_interface.Get(
'org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
self.running = (status == 'Playing')

# def get_players(self):
Expand All @@ -48,44 +60,53 @@ def get_active_player(self):
session_bus = dbus.SessionBus()
for service in session_bus.list_names():
if re.findall(r'org.mpris.MediaPlayer2|plasma-browser-integration', service, re.IGNORECASE):
obj = session_bus.get_object(service, '/org/mpris/MediaPlayer2')
interface = dbus.Interface(obj, 'org.freedesktop.DBus.Properties')
status = interface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
obj = session_bus.get_object(
service, '/org/mpris/MediaPlayer2')
interface = dbus.Interface(
obj, 'org.freedesktop.DBus.Properties')
status = interface.Get(
'org.mpris.MediaPlayer2.Player', 'PlaybackStatus')

if status == 'Playing':
self.player_name = service.split('MediaPlayer2.')[-1]
self.player_interface = interface
self.running = True
return


def mpd_active(self):
""" Check if mpd is active and get metadata """
client = mpd()
try: client.connect(self.mpd_host,self.mpd_port)
try:
client.connect(self.mpd_host, self.mpd_port)
except Exception as e:
print(e)
return False

if self.mpd_pass != '':
client.password(self.mpd_pass)

if client.status()['state'] == 'play':
self.player_name = "mpd"
self.running2 = True
self.running = True
currentsong = client.currentsong()
if 'album' in currentsong: album = currentsong['album']
else: album = ''

if 'album' in currentsong:
album = currentsong['album']
else:
album = ''

title = currentsong['title']
title2 = currentsong['title']
artist = currentsong['artist']
album = album
trackid = currentsong['id']

if self.track.title != title:
self.track.update(artist, title, album, trackid)
self.refresh()
return True
else:
self.running2 = False
self.running = False
return False

def get_bus(self):
''' gets dbus session bus and player interface
'''
Expand All @@ -95,8 +116,10 @@ def get_bus(self):
self.get_active_player()
else:
session_bus = dbus.SessionBus()
player_bus = session_bus.get_object(f'org.mpris.MediaPlayer2.{self.player_name}', '/org/mpris/MediaPlayer2')
self.player_interface = dbus.Interface(player_bus, 'org.freedesktop.DBus.Properties')
player_bus = session_bus.get_object(
f'org.mpris.MediaPlayer2.{self.player_name}', '/org/mpris/MediaPlayer2')
self.player_interface = dbus.Interface(
player_bus, 'org.freedesktop.DBus.Properties')
self.running = True

except dbus.exceptions.DBusException:
Expand All @@ -110,25 +133,25 @@ def update(self):
try:
if self.autoswitch:
self.check_playing()

if not self.running:
self.get_bus()

metadata = self.player_interface.Get("org.mpris.MediaPlayer2.Player", "Metadata")
metadata = self.player_interface.Get(
"org.mpris.MediaPlayer2.Player", "Metadata")
self.running = True
except Exception as e:
self.running = False
self.player_interface = None



if self.running:
try:
title = metadata['xesam:title']
if title.strip() == '':
# if Title is empty, don't update
return False
artist=''

artist = ''
if 'xesam:artist' in metadata:
artist = metadata['xesam:artist']
artist = artist[0] if isinstance(artist, list) else artist
Expand All @@ -139,7 +162,7 @@ def update(self):

title = title.strip()
artist = artist.strip()

album = metadata.get('xesam:album')
album = '' if album is None else album
# arturl = metadata['mpris:artUrl']
Expand All @@ -148,27 +171,26 @@ def update(self):
except (IndexError, KeyError) as e:
self.running = False
return False

if trackid.find('spotify:ad') != -1:
self.running = False
elif self.track.trackid != trackid or self.track.title != title:
# update track
self.track.update(artist, title, album, trackid)
self.refresh()
return True

if not self.running:
if self.mpd_active():
return True


elif MPD_ENABLED:
return self.mpd_active()

return False

def refresh(self, source=None, cache=True):
''' Re-fetches lyrics from procided source
source -> source name ('google' or 'azlyrics')
cache -> bool | wether to store cache file
'''

if source is None:
source = self.default_source
self.track.get_lyrics(source, cache=cache)
16 changes: 2 additions & 14 deletions lyrics/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,9 @@ def set_up(self):
self.stdscr.refresh()
self.scroll_pad.refresh(self.current_pos, 0, 4,
self.pad_offset, self.height - 2, self.width - 1)
elif self.player.running2:
self.update_track()
self.set_titlebar()
self.stdscr.refresh()
self.scroll_pad.refresh(self.current_pos, 0, 4,
self.pad_offset, self.height - 2, self.width - 1)
else:
self.stdscr.addstr(0, 1, f'{self.player.player_name} is not running!')
self.stdscr.refresh()
self.stdscr.addstr(0, 1, f'{self.player.player_name} is not running!')
self.stdscr.refresh()

def set_titlebar(self):
track_info = self.player.track.track_info(self.width - 1)
Expand Down Expand Up @@ -235,12 +229,6 @@ def main(self):
self.stdscr.refresh()
self.scroll_pad.refresh(self.current_pos, 0, 4,
self.pad_offset, self.height - 2, self.width - 1)
elif self.player.running2:
self.keys.input(self, key)
self.set_titlebar()
self.stdscr.refresh()
self.scroll_pad.refresh(self.current_pos, 0, 4,
self.pad_offset, self.height - 2, self.width - 1)
else:
self.stdscr.clear()
self.stdscr.addstr(0, 1, f'{self.player.player_name} player is not running.')
Expand Down

0 comments on commit 42cc8d3

Please sign in to comment.