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)
def setup(): """setup db & update channel & get demo music""" print 'setuping...' 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)
def test_channel(): channel_name = u"测试频道" channel_uuid = u"test_channel" # 测试添加 channel = channel_model.add_channel(channel_name, channel_uuid) assert channel.name == channel_name and channel_uuid == channel.uuid # 测试获取 channel = channel_model.get_channel(name=channel_name)[0] assert channel.name == channel_name and channel.uuid == channel.uuid # 测试更新 update_channel_name = u"更新测试频道" channel_model.update_channel(channel, name=update_channel_name) channel = channel_model.get_channel(uuid=channel_uuid)[0] assert channel.name == update_channel_name # 测试删除 channel_model.delete_channel(channel) channel = channel_model.get_channel(uuid=channel_uuid) assert not channel
def disable_channel(uuid): """set channel not playable""" channel = get_channel(uuid=uuid)[0] update_channel(channel, playable=False)
def enable_channel(uuid): """set channel playable""" channel = get_channel(uuid=uuid)[0] update_channel(channel, playable=True)
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