From 3aa99add6910347a830c5df1fcc61bbda9db6519 Mon Sep 17 00:00:00 2001 From: Bhrigu Date: Wed, 29 Mar 2017 10:44:40 +0530 Subject: [PATCH] code separated --- InstantLyrics.py | 192 +------------------------------------ src/__init__.py | 0 src/appIndicator.py | 61 ++++++++++++ Lyrics.py => src/lyrics.py | 0 src/utils.py | 32 +++++++ src/windows.py | 154 +++++++++++++++++++++++++++++ 6 files changed, 248 insertions(+), 191 deletions(-) create mode 100644 src/__init__.py create mode 100644 src/appIndicator.py rename Lyrics.py => src/lyrics.py (100%) create mode 100644 src/utils.py create mode 100644 src/windows.py diff --git a/InstantLyrics.py b/InstantLyrics.py index 33ee94c..653c693 100644 --- a/InstantLyrics.py +++ b/InstantLyrics.py @@ -1,194 +1,4 @@ -import gi -gi.require_version('Gtk', '3.0') -gi.require_version('AppIndicator3', '0.1') -from gi.repository import Gtk, Gdk -from gi.repository import AppIndicator3 as appindicator - -import os -import signal -import threading -import time -import dbus -from Lyrics import get_lyrics - -APPINDICATOR_ID = 'lyricsappindicator' - - -def get_icon_path(rel_path): - dir_of_py_file = os.path.dirname(__file__) - rel_path_to_resource = os.path.join(dir_of_py_file, rel_path) - abs_path_to_resource = os.path.abspath(rel_path_to_resource) - return abs_path_to_resource - - -class LyricsWindow(Gtk.Window): - - def __init__(self, type): - Gtk.Window.__init__(self, title="Lyrics") - self.set_icon_from_file(get_icon_path('icons/instant-lyrics.svg')) - self.set_border_width(20) - self.set_default_size(350, 650) - self.set_position(Gtk.WindowPosition.CENTER) - - self.main_box = Gtk.Box( - orientation=Gtk.Orientation.VERTICAL, spacing=6) - self.main_box.set_size_request(350, 700) - - if(type == "get"): - entry_hbox = self.create_input_box() - self.main_box.pack_start(entry_hbox, False, False, 10) - - lyrics_vbox = self.create_lyrics_box() - self.main_box.pack_start(lyrics_vbox, True, True, 0) - - scrolled = Gtk.ScrolledWindow() - scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) - scrolled.add(self.main_box) - - self.add(scrolled) - self.show_all() - - def on_key_release(self, widget, ev, data=None): - if ev.keyval == Gdk.KEY_Return: - self.fetch_lyrics() - - def create_input_box(self): - entry_hbox = Gtk.Box( - orientation=Gtk.Orientation.HORIZONTAL, spacing=50) - entry_hbox.set_property("margin", 10) - - self.input = Gtk.Entry() - self.input.set_text("song/artist") - self.input.connect("key-release-event", self.on_key_release) - entry_hbox.pack_start(self.input, True, True, 0) - - submit = Gtk.Button.new_with_label("Get Lyrics") - submit.connect("clicked", self.fetch_lyrics) - entry_hbox.pack_start(submit, True, True, 0) - - return entry_hbox - - def create_lyrics_box(self): - lyrics_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) - - self.title = Gtk.Label() - self.title.set_justify(Gtk.Justification.CENTER) - - self.lyrics = Gtk.Label() - self.lyrics.set_justify(Gtk.Justification.CENTER) - self.lyrics.set_property("margin_left", 40) - self.lyrics.set_property("margin_right", 40) - self.lyrics.set_line_wrap(True) - - self.spinner = Gtk.Spinner() - - lyrics_vbox.pack_start(self.title, False, False, 5) - lyrics_vbox.pack_start(self.spinner, False, False, 5) - lyrics_vbox.pack_start(self.lyrics, False, False, 5) - lyrics_vbox.set_size_request(350, 700) - - return lyrics_vbox - - def put_lyrics(self, song): - self.spinner.start() - - self.lyrics.set_text("") - lyrics = get_lyrics(song) - self.lyrics.set_text(lyrics) - - self.spinner.stop() - - def fetch_lyrics(self, source=None): - input = self.input.get_text() - text = "" + input + "" - self.title.set_markup(text) - - thread = threading.Thread( - target=self.put_lyrics, kwargs={'song': input}) - thread.daemon = True - thread.start() - - def get_spotify_song_data(self): - session_bus = dbus.SessionBus() - - spotify_bus = session_bus.get_object( - "org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2") - spotify_properties = dbus.Interface( - spotify_bus, "org.freedesktop.DBus.Properties") - metadata = spotify_properties.Get( - "org.mpris.MediaPlayer2.Player", "Metadata") - - title = metadata['xesam:title'].encode( - 'utf-8').decode('utf-8').replace("&", "&") - artist = metadata['xesam:artist'][0].encode( - 'utf-8').decode('utf-8').replace("&", "&") - return {'title': title, 'artist': artist} - - def get_spotify(self): - - try: - song_data = self.get_spotify_song_data() - song = song_data['title'] - artist = song_data['artist'] - except: - self.title.set_markup("Error") - message = ("Could not get current spotify song\n" - "Either spotify is not running or\n" - "no song is playing on spotify.\n\n" - "Else, report an issue here") - - self.lyrics.set_markup(message) - return - - title = "" + song + "\n" + artist + "" - self.title.set_markup(title) - - self.put_lyrics(song + " " + artist) - - -class AppIndicator(): - - def __init__(self): - signal.signal(signal.SIGINT, signal.SIG_DFL) - - indicator = appindicator.Indicator.new(APPINDICATOR_ID, get_icon_path( - 'icons/instant-lyrics-24.png'), appindicator.IndicatorCategory.SYSTEM_SERVICES) - indicator.set_status(appindicator.IndicatorStatus.ACTIVE) - indicator.set_menu(self.build_menu()) - Gtk.main() - - def build_menu(self): - menu = Gtk.Menu() - - get_lyrics = Gtk.MenuItem('Get Lyrics') - get_lyrics.connect('activate', self.fetch_lyrics) - - spotify_lyrics = Gtk.MenuItem('Spotify Lyrics') - spotify_lyrics.connect('activate', self.spotify_lyrics) - - item_quit = Gtk.MenuItem('Quit') - item_quit.connect('activate', self.quit) - - menu.append(get_lyrics) - menu.append(spotify_lyrics) - menu.append(item_quit) - menu.show_all() - return menu - - def fetch_lyrics(self, source): - win = LyricsWindow("get") - - def spotify_lyrics(self, source): - win = LyricsWindow("spotify") - thread = threading.Thread(target=win.get_spotify) - thread.daemon = True - thread.start() - - def quit(self, source): - Gtk.main_quit() - +from src.appIndicator import AppIndicator if __name__ == "__main__": app = AppIndicator() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/appIndicator.py b/src/appIndicator.py new file mode 100644 index 0000000..d85cc4f --- /dev/null +++ b/src/appIndicator.py @@ -0,0 +1,61 @@ +import gi +gi.require_version('Gtk', '3.0') +gi.require_version('AppIndicator3', '0.1') +from gi.repository import AppIndicator3 as appindicator +from gi.repository import Gtk + +import signal +import threading + +from src.windows import LyricsWindow, PreferenceWindow +from src.utils import get_icon_path + +APPINDICATOR_ID = 'lyricsappindicator' + +class AppIndicator(): + + def __init__(self): + signal.signal(signal.SIGINT, signal.SIG_DFL) + + indicator = appindicator.Indicator.new(APPINDICATOR_ID, get_icon_path( + '../icons/instant-lyrics-24.png'), appindicator.IndicatorCategory.SYSTEM_SERVICES) + indicator.set_status(appindicator.IndicatorStatus.ACTIVE) + indicator.set_menu(self.build_menu()) + Gtk.main() + + def build_menu(self): + menu = Gtk.Menu() + + get_lyrics = Gtk.MenuItem('Get Lyrics') + get_lyrics.connect('activate', self.fetch_lyrics) + + spotify_lyrics = Gtk.MenuItem('Spotify Lyrics') + spotify_lyrics.connect('activate', self.spotify_lyrics) + + preferences = Gtk.MenuItem('Preferences') + preferences.connect('activate', self.preferences) + + item_quit = Gtk.MenuItem('Quit') + item_quit.connect('activate', self.quit) + + menu.append(get_lyrics) + menu.append(spotify_lyrics) + menu.append(preferences) + menu.append(item_quit) + menu.show_all() + return menu + + def fetch_lyrics(self, source): + win = LyricsWindow("get") + + def spotify_lyrics(self, source): + win = LyricsWindow("spotify") + thread = threading.Thread(target=win.get_spotify) + thread.daemon = True + thread.start() + + def preferences(self, source): + win = PreferenceWindow() + + def quit(self, source): + Gtk.main_quit() \ No newline at end of file diff --git a/Lyrics.py b/src/lyrics.py similarity index 100% rename from Lyrics.py rename to src/lyrics.py diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..da5cfcd --- /dev/null +++ b/src/utils.py @@ -0,0 +1,32 @@ +import os +try: + import configparser +except ImportError: + import ConfigParser as configparser + +CONFIG_PATH = os.getenv("HOME") + "/.confg/instant-lyrics/config" + + +def get_icon_path(rel_path): + dir_of_py_file = os.path.dirname(__file__) + rel_path_to_resource = os.path.join(dir_of_py_file, rel_path) + abs_path_to_resource = os.path.abspath(rel_path_to_resource) + return abs_path_to_resource + +def ConfigSectionMap(Config, section): + dict1 = {} + options = Config.options(section) + for option in options: + try: + dict1[option] = Config.get(section, option) + if dict1[option] == -1: + DebugPrint("skip: %s" % option) + except: + print("exception on %s!" % option) + dict1[option] = None + return dict1 + +def read_config(config_file): + Config = ConfigParser.ConfigParser() + Config.read(config_file) + return Config \ No newline at end of file diff --git a/src/windows.py b/src/windows.py new file mode 100644 index 0000000..fe04a2e --- /dev/null +++ b/src/windows.py @@ -0,0 +1,154 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, Gdk + +import dbus +import threading + +from src.utils import get_icon_path +from src.lyrics import get_lyrics + +class LyricsWindow(Gtk.Window): + + def __init__(self, type): + Gtk.Window.__init__(self, title="Lyrics") + self.set_icon_from_file(get_icon_path('../icons/instant-lyrics.svg')) + self.set_border_width(20) + self.set_default_size(350, 650) + self.set_position(Gtk.WindowPosition.CENTER) + + self.main_box = Gtk.Box( + orientation=Gtk.Orientation.VERTICAL, spacing=6) + self.main_box.set_size_request(350, 700) + + if(type == "get"): + entry_hbox = self.create_input_box() + self.main_box.pack_start(entry_hbox, False, False, 10) + + lyrics_vbox = self.create_lyrics_box() + self.main_box.pack_start(lyrics_vbox, True, True, 0) + + scrolled = Gtk.ScrolledWindow() + scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) + scrolled.add(self.main_box) + + self.add(scrolled) + self.show_all() + + def on_key_release(self, widget, ev, data=None): + if ev.keyval == Gdk.KEY_Return: + self.fetch_lyrics() + + def create_input_box(self): + entry_hbox = Gtk.Box( + orientation=Gtk.Orientation.HORIZONTAL, spacing=50) + entry_hbox.set_property("margin", 10) + + self.input = Gtk.Entry() + self.input.set_text("song/artist") + self.input.connect("key-release-event", self.on_key_release) + entry_hbox.pack_start(self.input, True, True, 0) + + submit = Gtk.Button.new_with_label("Get Lyrics") + submit.connect("clicked", self.fetch_lyrics) + entry_hbox.pack_start(submit, True, True, 0) + + return entry_hbox + + def create_lyrics_box(self): + lyrics_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) + + self.title = Gtk.Label() + self.title.set_justify(Gtk.Justification.CENTER) + + self.lyrics = Gtk.Label() + self.lyrics.set_justify(Gtk.Justification.CENTER) + self.lyrics.set_property("margin_left", 40) + self.lyrics.set_property("margin_right", 40) + self.lyrics.set_line_wrap(True) + + self.spinner = Gtk.Spinner() + + lyrics_vbox.pack_start(self.title, False, False, 5) + lyrics_vbox.pack_start(self.spinner, False, False, 5) + lyrics_vbox.pack_start(self.lyrics, False, False, 5) + lyrics_vbox.set_size_request(350, 700) + + return lyrics_vbox + + def put_lyrics(self, song): + self.spinner.start() + + self.lyrics.set_text("") + lyrics = get_lyrics(song) + self.lyrics.set_text(lyrics) + + self.spinner.stop() + + def fetch_lyrics(self, source=None): + input = self.input.get_text() + text = "" + input + "" + self.title.set_markup(text) + + thread = threading.Thread( + target=self.put_lyrics, kwargs={'song': input}) + thread.daemon = True + thread.start() + + def get_spotify_song_data(self): + session_bus = dbus.SessionBus() + + spotify_bus = session_bus.get_object( + "org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2") + spotify_properties = dbus.Interface( + spotify_bus, "org.freedesktop.DBus.Properties") + metadata = spotify_properties.Get( + "org.mpris.MediaPlayer2.Player", "Metadata") + + title = metadata['xesam:title'].encode( + 'utf-8').decode('utf-8').replace("&", "&") + artist = metadata['xesam:artist'][0].encode( + 'utf-8').decode('utf-8').replace("&", "&") + return {'title': title, 'artist': artist} + + def get_spotify(self): + + try: + song_data = self.get_spotify_song_data() + song = song_data['title'] + artist = song_data['artist'] + except: + self.title.set_markup("Error") + message = ("Could not get current spotify song\n" + "Either spotify is not running or\n" + "no song is playing on spotify.\n\n" + "Else, report an issue here") + + self.lyrics.set_markup(message) + return + + title = "" + song + "\n" + artist + "" + self.title.set_markup(title) + + self.put_lyrics(song + " " + artist) + +class PreferenceWindow(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self, title="Instant-Lyrics Prefenreces") + self.set_icon_from_file(get_icon_path('../icons/instant-lyrics.svg')) + self.set_border_width(20) + self.set_default_size(350, 550) + self.set_position(Gtk.WindowPosition.CENTER) + + self.main_box = Gtk.Box( + orientation=Gtk.Orientation.VERTICAL, spacing=6) + self.main_box.set_size_request(350, 550) + + pref_box = self.create_pref_box() + self.main_box.pack_start(lyrics_vbox, True, True, 0) + + self.add(main_box) + self.show_all() \ No newline at end of file