コード例 #1
0
ファイル: service.py プロジェクト: r0stig/spotwinos
class MyStreamer(TwythonStreamer):

    def set_device(self, soco_device):
        self.soco_device = soco_device
        self.spotify_plugin = Spotify(soco_device)

    def on_success(self, data):
        if 'text' in data and 'user' in data and 'screen_name' in data['user']:
            text = data['text'].encode('utf-8')
            text = self.strip_receiver(text)
            screen_name = data['user']['screen_name'].encode('utf-8')
            if (text in commands):
                self.do_command(text, screen_name)
            elif self.validate_format(text):
                print "Adding {0} to playlist".format(text)
                self.play_track(text)
            else:
                print "Unknown command or spotify uri format, text: {0}".format(text)

    def on_error(self, status_code, data):
        print status_code

        # Want to stop trying to get data because of the error?
        # Uncomment the next line!
        # self.disconnect()

    def validate_format(self, spotify_uri):
        """ Example format: spotify:track:20DfkHC5grnKNJCzZQB6KC """
        return re.match(r"spotify\:track\:[A-z0-9]", spotify_uri)

    def strip_receiver(self, text):
        """ When sending tweet to a user the text becomes: @user text, this is to strip on the @user """
        return re.sub(r"\@[A-z0-9]+", "", text).strip()

    def play_track(self, uri):
        print self.spotify_plugin.add_track_to_queue(SpotifyTrack(uri))

    def do_command(self, cmd, screen_name):
        # Dessa borde endast få köras av vissa användare..
        print "Doing command " + cmd + " from " + screen_name
        if cmd == 'pause':
            self.soco_device.pause()
        elif cmd == 'play':
            self.soco_device.play()
        elif cmd == 'next':
            self.soco_device.next()
        elif cmd == 'prev':
            self.soco_device.prev()
コード例 #2
0
ファイル: service.py プロジェクト: r0stig/spotwinos
 def set_device(self, soco_device):
     self.soco_device = soco_device
     self.spotify_plugin = Spotify(soco_device)