Esempio n. 1
0
def test_playlist():

        #添加测试频道
        channel_name = u"test_channel_name"
        channel_uuid = u"mk_test_douban-cid"
        channel = channel_model.add_channel(channel_name, channel_uuid)
        assert len(playlist.get_music_by_channel(channel, 20)) == 0

        #添加测试音乐
        music_information = get_test_music()
        new_music_list = []

        for i in range(20):
            music_information[u"cover"].seek(0)
            music_information[u"audio"].seek(0)
            music_information[u"uuid"] += unicode(i)
            music = music_model.add_music(music_information[u"title"], music_information[u"artist"], music_information[u"album"]
                                          , music_information[u"company"], music_information[u"public_time"], music_information[u"kbps"], music_information[u"cover"], music_information[u"audio"], music_information[u"uuid"])
            new_music_list.append(music.key)

        #往测试频道中添加测试音乐信息
        channel_model.update_channel(channel, music_list=new_music_list)

        channel = channel_model.get_channel(key=channel.key)[0]
        assert len(playlist.get_music_by_channel(channel, 30)) == 20
        assert len(playlist.get_music_by_channel(channel, 20)) == 20
        assert len(playlist.get_music_by_channel(channel, 10)) == 10

        #删除
        channel_model.delete_channel(channel)
        music_list = music_model.get_music(title=music_information[u"title"])
        for music in music_list:
            music_model.delete_music(music)
Esempio n. 2
0
def test_music():
    music_information = get_test_music()
    #测试添加音乐
    music = music_model.add_music(music_information[u"title"], music_information[u"artist"], music_information[u"album"]
                                  , music_information[u"company"], music_information[u"public_time"], music_information[u"kbps"], music_information[u"cover"], music_information[u"audio"], music_information[u"uuid"])

    equal_check(music, music_information)

    #测试获取音乐
    music = music_model.get_music(uuid=music_information[u"uuid"])[0]
    equal_check(music, music_information)

    #测试更新音乐
    music_information[u"title"] = u"update_test_music_title"
    music_model.update_music(music, title=music_information[u"title"])
    music = music_model.get_music(uuid=music_information[u"uuid"])[0]
    equal_check(music, music_information)

    #测试删除音乐
    music_model.delete_music(music)
    music = music_model.get_music(uuid=music_information[u"uuid"])
    assert not music
Esempio n. 3
0
def _update_channel_once(channel, max_num=20):
    """"update music in channel. max is the max number it will update
    return updated music
    please login before this function"""
    global g_user_id, g_token, g_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 not channel.music_list:
        payload = {'app_name': DOUBAN_SPIDER_NAME,
                   'version': DOUBAN_SPIDER_VERSION,
                   'user_id': g_user_id,
                   'expire': g_expire,
                   'token': g_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': g_user_id,
                   'expire': g_expire,
                   'token': g_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:
        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 = []
    for song in r['song']:
        try:
            uuid = DOUBAN_MUSIC_UUID_FORMAT % (int(song['aid']), int(song['sid']))
        except Exception:
            # ads
            continue
        if not get_music(uuid=uuid):
            try:
                import pdb; pdb.set_trace()
                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
            try:
                print song['rating_avg']
                music = add_music(song['title'], song['artist'], song['albumtitle'],
                                  song['company'], song['public_time'], song['kbps'],
                                  cover_fd, audio_fd, uuid)
            except Exception:
                traceback.print_exc()
                continue

            spider_log.log_info("add music:"+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