Beispiel #1
0
 def get_songlist(self):
     if self.user:
         self.songlist = self.user.playlist()
     elif self.private:
         self.get_user_name_pass()
         self.user = PrivateFM(self.channel)
         self.songlist = self.user.playlist()
     else:
         self.songlist = json.loads(urllib.urlopen(self.ch).read())['song']
Beispiel #2
0
class DoubanFM_CLI:
    def __init__(self, channel):
        self.user = None
        self.username = ''
        self.channel = channel
        if channel == '0' or channel == '-3':
            self.private = True
        else:
            self.private = False
        self.player = gst.element_factory_make("playbin", "player")
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.connect("message", self.on_message)
        self.ch = 'http://douban.fm/j/mine/playlist?type=p&sid=&channel='+channel

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
            self.playmode = False
        elif t == gst.MESSAGE_ERROR:
            self.player.set_state(gst.STATE_NULL)
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.playmode = False

    def get_songlist(self):
        if self.user:
            self.songlist = self.user.playlist()
        elif self.private:
            self.get_user_name_pass()
            self.user = PrivateFM(self.channel)
            self.songlist = self.user.playlist()
        else:
            self.songlist = json.loads(urllib.urlopen(self.ch).read())['song']

    def get_user_name_pass(self):
        # todo we should del this method
        self.username = ''
        self.password = ''

    def control(self,r):
        rlist, _, _ = select([sys.stdin], [], [], 1)
        if rlist:
            s = sys.stdin.readline()
            if s[0] == 'n':
                return 'next'
            elif s[0] == 'f' and self.private:
                self.user.fav_song(r['sid'], r['aid'])
                print "加心成功:)"
                return 'fav'
            elif s[0] == 'd' and self.private:
                self.songlist = self.user.del_song(r['sid'], r['aid'])
                print "删歌成功:)"
                return 'del'

    def start(self):
        self.get_songlist()
        for r in self.songlist:
            song_uri = r['url']
            self.playmode = True
            print u'正在播放: '+r['title']+u'     歌手: '+r['artist']+'    ',
            if r['like']:
                print u'♥'
            else:
                print u'x'
            self.player.set_property("uri", song_uri)
            self.player.set_state(gst.STATE_PLAYING)
            while self.playmode:
                c = self.control(r)
                if c == 'next' or c == 'del':
                    self.player.set_state(gst.STATE_NULL)
                    self.playmode = False
                    break 
        loop.quit()