def music(uuid=None): if not uuid: music_list = get_music() else: music_list = get_music(uuid=uuid) print 'key\t\t\t\t\t\tuuid\t\t\t\t\t\ttitle\t\t\t\t\t\tartist' for music in music_list: print '%s\t\t%s\t\t%s\t\t\t\t%s' % ( music.key.encode('utf-8'), music.uuid.encode('utf8'), music.title.encode('utf8'), music.artist.encode('utf-8'))
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 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
def get_user_history(user, start, end): """ return user operation history return as: date, op, music_key, music_title, music_cover """ if start < 0 or start > end: start = 0 end = -1 history_list = user.history[start:end] new_history_list = list() for history in history_list: music = get_music(key=history[2])[0] new_history_list.append({'date': history[0], 'op': history[1], 'key': history[2], 'title': music.title, 'audio': music.audio, 'cover': music.cover}) return new_history_list
def get_user_music_list(user, kind, start, end): if start < 0 or start > end: start = 0 end = -1 if kind == 'listened': music_key_list = user.listened[start:end] elif kind == 'dislike': music_key_list = user.dislike[start:end] elif kind == 'favor': music_key_list = user.favor[start:end] elif kind == 'share': music_key_list = user.share[start:end] else: return [] music_list = [] #at here we just return music object for key in music_key_list: music = get_music(key=key)[0] music_list.append(music) return music_list
def get(self): args = MusicQueryArgs().args if not args: return marshal(get_music_status(), music_status_fields) return marshal(get_music(**args), music_fields)
def MusicKey(key): try: assert get_music(key=key)[0] except: raise ValueError("Invalid Music Key") return key
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
def get_music_by_channel(channel, num): num = min(len(channel.music_list), num) music_list = random.sample(channel.music_list, num) return [get_music(key=each)[0] for each in music_list]