Esempio n. 1
0
class LyricServer(object):
    def __init__(self):
        self.source_builder = BuilderOfSources()
        self.artist = None
        self.song = None
        self.lyrics = None
        self._socket = socket.socket()
        self._n_requests = None

    def try_get_lyrics(self):
        """
            Tries to get the lyrics of the current object's artist and song
        """
        source_names = self.source_builder.get_names()
        for source_name in source_names:
            try:
                source = self.source_builder.build_from_source(source_name, self.artist, self.song)
                source.build_url()
                self.lyrics = source.get_lyrics()
                break
            except:
                pass

    def define_socket(self, port, n_requests=5):
        """
            Used to define the socket object from inputs
        """
        host = socket.gethostname()
        print "Lyric Thief Server: Hostname is %s" % str(host)
        self._socket.bind((host, port))
        self._n_requests = n_requests

    def handle_lyric_requests(self):
        self._socket.listen(self._n_requests)

        while True:
            client, address = self._socket.accept()
            print "Lyric Thief Server: Connected to %s" % str(address)

            self.lyrics = ''
            data = json.loads(client.recv(1024))
            try:
                self.artist, self.song = data['artist'], data['song']
            except KeyError:
                client.send("Invalid JSON passed")
                print "Lyric Thief Server: Invalid JSON Passed"
                client.close()
                return
                
            self.try_get_lyrics()

            # Try sending the lyrics line-by line because of the text cut-off

            for line in self.lyrics.split('\n'):
                client.send(line+'\n')

            print "Lyric Thief Server: Sent Lyrics, Closing Connection"
            client.close()
Esempio n. 2
0
 def __init__(self):
     self.source_builder = BuilderOfSources()
     self.artist = None
     self.song = None
     self.lyrics = None
     self._socket = socket.socket()
     self._n_requests = None
Esempio n. 3
0
    def __init__(self):
        GladeWindow.__init__(self, './GladeFiles/LyricThiefWindow.glade')
        
        self.source_builder = BuilderOfSources()        
        self.current_source = None

        self.window = self.connect_widget_by_name('wdwMain', 'destroy', lambda x: gtk.main_quit())
        self.btn_get = self.connect_widget_by_name('btnGet', 'clicked', self.get_clicked)
        self.btn_exit = self.connect_widget_by_name('btnExit', 'clicked', lambda x: gtk.main_quit())
        self.ent_artist = self.w_tree.get_widget('entArtist')
        self.ent_song = self.w_tree.get_widget('entSong')
        self.cmb_source = self.connect_widget_by_name('cmbSource', 'changed', self.change_current_source)
        self.result_buffer = gtk.TextBuffer()

        self.window.set_icon_from_file('Icons/LyricThiefIcon.png')
        self.cmb_source.set_active(0)
        self.w_tree.get_widget('txtResults').set_buffer(self.result_buffer)
        self.result_buffer.set_text('')

        for source in self.source_builder.get_names():
            self.cmb_source.append_text(source)
Esempio n. 4
0
class LyricThiefWindow(GladeWindow):
    def __init__(self):
        GladeWindow.__init__(self, './GladeFiles/LyricThiefWindow.glade')
        
        self.source_builder = BuilderOfSources()        
        self.current_source = None

        self.window = self.connect_widget_by_name('wdwMain', 'destroy', lambda x: gtk.main_quit())
        self.btn_get = self.connect_widget_by_name('btnGet', 'clicked', self.get_clicked)
        self.btn_exit = self.connect_widget_by_name('btnExit', 'clicked', lambda x: gtk.main_quit())
        self.ent_artist = self.w_tree.get_widget('entArtist')
        self.ent_song = self.w_tree.get_widget('entSong')
        self.cmb_source = self.connect_widget_by_name('cmbSource', 'changed', self.change_current_source)
        self.result_buffer = gtk.TextBuffer()

        self.window.set_icon_from_file('Icons/LyricThiefIcon.png')
        self.cmb_source.set_active(0)
        self.w_tree.get_widget('txtResults').set_buffer(self.result_buffer)
        self.result_buffer.set_text('')

        for source in self.source_builder.get_names():
            self.cmb_source.append_text(source)

    def change_current_source(self, sender):
        if sender.get_active() != 0:
            self.current_source = sender.get_active_text()
        else:
            self.current_source = None

    def append_to_result(self, text):
        self.result_buffer.insert(self.result_buffer.get_end_iter(), text)

    def get_clicked(self, sender):
        if not self.fields_empty():
            self.result_buffer.set_text('')
            artist, song = self.ent_artist.get_text(), self.ent_song.get_text()
            source_found = None
            lyrics = ''

            source_names = self.source_builder.get_names() if self.current_source==None else [self.current_source] 
            for source_name in source_names:
                try:
                    source = self.source_builder.build_from_source(source_name, artist, song)
                    source.build_url()
                    lyrics = source.get_lyrics()
                    source_found = source_name
                    break
                except:
                    self.append_to_result('\n' + source_name + " failed")

            if source_found:
                self.run_lyric_window(lyrics, artist, song)
                self.append_to_result('\n\n' + source_found + ' found it!' + '\n-----------------------')
            else:
                self.append_to_result('\n\nFailed to find song\n-----------------------')

    def run_lyric_window(self, lyrics, artist, song):
        text_buffer = gtk.TextBuffer()
        lyric_window = LyricWindow(TitleHelper.capitalize_music(artist +' - ' + song))
        text_buffer.set_text(lyrics)
        lyric_window.tvi_lyrics.set_buffer(text_buffer)
        self.window.hide()
        lyric_window.window.show()
        gtk.main()
        self.window.show()
        
    def fields_empty(self):
        return self.ent_artist.get_text() == '' and self.ent_song.get_text() == ''