Example #1
0
    def test_get(self):
        self.login_as_admin()
        channel = add_channel('name1', 'uuid1')
        update_channel(channel, playable=True)

        title = 'music_title'
        artist = 'music_artist'
        album = 'music_album'
        company = 'music_company'
        public_time = '2013'
        kbps = '64'
        uuid = 'douban-uuid'
        new_music_list = []
        for i in range(20):
            self.cover.seek(0)
            self.audio.seek(0)
            music = add_music(title, artist, album, company, public_time,
                              kbps, self.cover, self.audio, uuid + str(i))
            new_music_list.append(music.key)
        update_channel(channel, music_list=new_music_list)
        rv = self.app.get('/api/playlist/%s/?num=10' % (channel.key))
        rv = json.loads(rv.data)
        assert len(rv) == 10

        rv = self.app.get('/api/playlist/%s/?num=50' % (channel.key))
        rv = json.loads(rv.data)
        assert len(rv) == 20
Example #2
0
    def test_get_music_by_channel(self):
        name = 'channel_name'
        uuid = 'douban-cid'
        channel = add_channel(name, uuid)
        assert len(get_music_by_channel(channel, 20)) == 0

        title = 'music_title'
        artist = 'music_artist'
        album = 'music_album'
        company = 'music_company'
        public_time = '2013'
        kbps = '64'
        uuid = 'douban-uuid'
        new_music_list = []
        for i in range(20):
            self.cover.seek(0)
            self.audio.seek(0)
            music = add_music(title, artist, album, company, public_time, kbps,
                              self.cover, self.audio, uuid + str(i))
            new_music_list.append(music.key)
        update_channel(channel, music_list=new_music_list)
        channel = get_channel(key=channel.key)[0]
        assert len(get_music_by_channel(channel, 30)) == 20
        assert len(get_music_by_channel(channel, 20)) == 20
        assert len(get_music_by_channel(channel, 10)) == 10
Example #3
0
    def test_get_music_by_channel(self):
        name = 'channel_name'
        uuid = 'douban-cid'
        channel = add_channel(name, uuid)
        assert len(get_music_by_channel(channel, 20)) == 0

        title = 'music_title'
        artist = 'music_artist'
        album = 'music_album'
        company = 'music_company'
        public_time = '2013'
        kbps = '64'
        uuid = 'douban-uuid'
        new_music_list = []
        for i in range(20):
            self.cover.seek(0)
            self.audio.seek(0)
            music = add_music(title, artist, album, company, public_time,
                              kbps, self.cover, self.audio, uuid + str(i))
            new_music_list.append(music.key)
        update_channel(channel, music_list=new_music_list)
        channel = get_channel(key=channel.key)[0]
        assert len(get_music_by_channel(channel, 30)) == 20
        assert len(get_music_by_channel(channel, 20)) == 20
        assert len(get_music_by_channel(channel, 10)) == 10
Example #4
0
 def test_get(self):
     channel = add_channel('name1', 'uuid1')
     update_channel(channel, playable=True)
     channel = add_channel('name2', 'uuid2')
     update_channel(channel, playable=True)
     channel = add_channel('name3', 'uuid3')
     channel = add_channel('name4', 'uuid4')
     rv = self.app.get('/api/playlist/')
     assert len(json.loads(rv.data)) == 2
Example #5
0
 def test_update_channel(self):
     name = 'channel_name'
     uuid = 'douban-cid'
     channel = add_channel(name, uuid)
     assert len(get_channel()) == 1
     update_channel(channel, music_list=['1' * 24, '2' * 24])
     assert len(get_channel()) == 1
     channel = get_channel()[0]
     assert channel.music_list == ['1' * 24, '2' * 24]
Example #6
0
def setup():
    '''setup db & update channel & get demo music'''
    print 'setuping...'
    print 'setting admin'
    try:
        assert add_user(ADMIN_NAME, ADMIN_PASSWORD, 'admin'), 'admin already exist!!!this will pass'
    except:
        pass
    print 'login douban...'
    assert login(), 'check network or the DOUBAN_USER_NAME, DOUBAN_USER_PASSWORD in config.py'
    print 'update channel list'
    update_channel_list()
    print 'update demo music'
    channel = get_channel()[0]
    music_list = update_music_by_channel(channel, 1)
    assert len(music_list) == 1
    print 'add demo channel to playlist'
    update_channel(channel, playable=True)
Example #7
0
def setup():
    '''setup db & update channel & get demo music'''
    print 'setuping...'
    print 'setting admin'
    try:
        assert add_user(ADMIN_NAME, ADMIN_PASSWORD,
                        'admin'), 'admin already exist!!!this will pass'
    except:
        pass
    print 'login douban...'
    assert login(
    ), 'check network or the DOUBAN_USER_NAME, DOUBAN_USER_PASSWORD in config.py'
    print 'update channel list'
    update_channel_list()
    print 'update demo music'
    channel = get_channel()[0]
    music_list = update_music_by_channel(channel, 1)
    assert len(music_list) == 1
    print 'add demo channel to playlist'
    update_channel(channel, playable=True)
Example #8
0
def disable_channel(uuid):
    '''set channel not playable'''
    channel = get_channel(uuid=uuid)[0]
    update_channel(channel, playable=False)
Example #9
0
def enable_channel(uuid):
    '''set channel playable'''
    channel = get_channel(uuid=uuid)[0]
    update_channel(channel, playable=True)
Example #10
0
 def patch(self, key):
     args = ChannelPatchArgs().args
     channel = get_channel(key=key)[0]
     update_channel(channel, **args)
     channel = get_channel(key=key)[0]
     return channel
Example #11
0
 def patch(self, key):
     args = ChannelPatchArgs().args
     channel = get_channel(key=key)[0]
     update_channel(channel, **args)
     channel = get_channel(key=key)[0]
     return channel
Example #12
0
def disable_channel(uuid):
    '''set channel not playable'''
    channel = get_channel(uuid=uuid)[0]
    update_channel(channel, playable=False)
Example #13
0
def enable_channel(uuid):
    '''set channel playable'''
    channel = get_channel(uuid=uuid)[0]
    update_channel(channel, playable=True)
Example #14
0
def _update_channel_once(channel, max_num=10):
    '''update music in channel. max is the max number it will update
    return updated music
    please login before this function'''
    global _user_id, _token, _expire
    # TODO
    # maybe need a better method to assert and get cid
    assert channel.uuid.startswith(DOUBAN_CHANNEL_UUID_FORMAT.split('-')[0])
    cid = int(channel.uuid.split('-')[1])
    if channel.music_list == []:
        payload = {'app_name': DOUBAN_SPIDER_NAME,
                   'version': DOUBAN_SPIDER_VERSION,
                   'user_id': _user_id,
                   'expire': _expire,
                   'token': _token,
                   'channel': cid,
                   'type': 'n'}
    else:
        uuid = get_music(key=random.choice(channel.music_list))[0].uuid
        sid = uuid.split('-')[2]
        payload = {'app_name': DOUBAN_SPIDER_NAME,
                   'version': DOUBAN_SPIDER_VERSION,
                   'user_id': _user_id,
                   'expire': _expire,
                   'token': _token,
                   'channel': cid,
                   'type': 'p',
                   'sid': sid}

        # # mark as listened
        # mark_payload = {'app_name': DOUBAN_SPIDER_NAME,
        #                 'version': DOUBAN_SPIDER_VERSION,
        #                 'user_id': _user_id,
        #                 'expire': _expire,
        #                 'token': _token,
        #                 'channel': cid,
        #                 'type': 'e',
        #                 'sid': sid}
        # try:
        #     requests.get("http://www.douban.com/j/app/radio/people", params=mark_payload, timeout=5)
        # except:
        #     pass

        # # don't play again
        # mark_payload = {'app_name': DOUBAN_SPIDER_NAME,
        #                 'version': DOUBAN_SPIDER_VERSION,
        #                 'user_id': _user_id,
        #                 'expire': _expire,
        #                 'token': _token,
        #                 'channel': cid,
        #                 'type': 'b',
        #                 'sid': sid}
        # try:
        #     requests.get("http://www.douban.com/j/app/radio/people", params=mark_payload, timeout=5)
        # except:
        #     pass
    try:
        print 'getting list'
        r = requests.get("http://www.douban.com/j/app/radio/people", params=payload, timeout=5)
    except requests.exceptions.ConnectionError:
        traceback.print_exc()
        return []
    except requests.exceptions.Timeout:
        traceback.print_exc()
        return []
    r = json.loads(r.text)
    assert r['r'] == 0
    update_music = []
    #channel_music_list = channel.music_list
    for song in r['song']:
        try:
            uuid = DOUBAN_MUSIC_UUID_FORMAT % (int(song['aid']), int(song['sid']))
        except:
            # ads
            continue
        print uuid
        music = None
        if len(get_music(uuid=uuid)) == 0:
            try:
                print 'getting song'
                cover_fd = requests.get(song['picture'], stream=True, timeout=5).raw
                audio_fd = requests.get(song['url'], stream=True, timeout=5).raw
            except requests.exceptions.ConnectionError:
                traceback.print_exc()
                continue
            except requests.exceptions.Timeout:
                traceback.print_exc()
                continue
            music = add_music(song['title'], song['artist'], song['albumtitle'],
                              song['company'], song['public_time'], song['kbps'],
                              cover_fd, audio_fd, uuid)
        else:
            music = get_music(uuid=uuid)[0]
        if music and music.key not in channel.music_list:
            channel_music_list = channel.music_list
            channel_music_list.append(music.key)
            update_channel(channel, music_list=channel_music_list)
            update_music.append(music)
            if len(update_music) >= max_num:
                break
    return update_music