def test_get(self): self.login_as_admin() # this will add a user name admin! music1 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid1') self.cover.seek(0) self.audio.seek(0) music2 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid2') rv = self.app.get('/api/user/current/favor/?start=0') rv = json.loads(rv.data) assert len(rv) == 0 rv = self.app.post('/api/user/current/history/', data={'op': 'favor', 'key': music1.key}) print rv.status assert rv.status_code == 200 rv = self.app.post('/api/user/current/history/', data={'op': 'favor', 'key': music2.key}) assert rv.status_code == 200 rv = self.app.post('/api/user/current/history/', data={'op': 'favor', 'key': 'unknown key'}) assert rv.status_code == 400 rv = self.app.get('/api/user/current/favor/?start=0&end=10') rv = json.loads(rv.data) # print rv assert len(rv) == 2 rv = self.app.get('/api/user/current/favor/?start=0') rv = json.loads(rv.data) assert len(rv) == 2
def test_add_music(self): title = 'music_title' artist = 'music_artist' album = 'music_album' company = 'music_company' public_time = '2013' kbps = '64' uuid = 'douban-uuid' music = add_music(title, artist, album, company, public_time, kbps, self.cover, self.audio, uuid) try: add_music(title, artist, album, company, public_time, kbps, self.cover, self.audio, uuid) except mongoengine.errors.NotUniqueError: pass assert len(get_music(key=music.key)) == 1 music = get_music()[0] assert music.title == 'music_title' assert music.artist == 'music_artist' assert music.album == 'music_album' assert music.company == 'music_company' assert music.public_time == '2013' assert music.kbps == '64' self.cover.seek(0) self.audio.seek(0) assert music.cover.read() == self.cover.read() assert music.audio.read() == self.audio.read() assert music.uuid == 'douban-uuid'
def test_post(self): self.login_as_admin() # this will add a user name admin! music1 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid1') self.cover.seek(0) self.audio.seek(0) music2 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid2') rv = self.app.get('/api/user/current/history/?start=0') rv = json.loads(rv.data) assert len(rv) == 0 self.app.post('/api/user/current/history/', data={'op': 'favor', 'key': music1.key}) self.app.post('/api/user/current/history/', data={'op': 'dislike', 'key': music2.key}) self.app.post('/api/user/current/history/', data={'op': 'listened', 'key': music2.key}) self.app.post('/api/user/current/history/', data={'op': 'shared', 'key': music2.key}) self.app.post('/api/user/current/history/', data={'op': 'unknown', 'key': music2.key}) rv = self.app.get('/api/user/current/history/?start=0&end=10') rv = json.loads(rv.data) assert len(rv) == 4 rv = self.app.get('/api/user/current/history/?start=0') rv = json.loads(rv.data) assert len(rv) == 4
def test_get_status(self): title = 'music_title' artist = 'music_artist' album = 'music_album' company = 'music_company' public_time = '2013' kbps = '64' uuid = 'douban-uuid' for i in range(10): add_music(title, artist, album, company, public_time, kbps, self.cover, self.audio, uuid + str(i)) assert get_music_status()['count'] == 10
def test_get_user_history(self): # add user user = add_user('name1', 'pw1', 'normal') # add music music1 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid1') self.cover.seek(0) self.audio.seek(0) music2 = add_music('title', 'artist', 'album', 'company', '2013', '64', self.cover, self.audio, 'uuid2') add_user_history(user, 'favor', music1.key) add_user_history(user, 'dislike', music2.key) assert len(get_user_history(user, 0, 10)) == 2
def test_update_music(self): title = 'music_title' artist = 'music_artist' album = 'music_album' company = 'music_company' public_time = '2013' kbps = '64' uuid = 'douban-uuid' music = add_music(title, artist, album, company, public_time, kbps, self.cover, self.audio, uuid) self.cover.seek(0) self.audio.seek(0) music = get_music()[0] # exchange cover & audio update_music(music, title='new_title', artist='new_artist', album='new_album', company='new_company', public_time='2014', kbps='128', cover=self.audio, audio=self.cover, uuid='new_douban-uuid') music = get_music()[0] assert music.title == 'new_title' assert music.artist == 'new_artist' assert music.album == 'new_album' assert music.company == 'new_company' assert music.public_time == '2014' assert music.kbps == '128' self.cover.seek(0) self.audio.seek(0) assert music.cover.read() == self.audio.read() assert music.audio.read() == self.cover.read() assert music.uuid == 'new_douban-uuid'
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
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
def test_delete(self): self.login_as_admin() music = add_music('title', 'artist', 'album', 'company', '2001', '64', self.cover, self.audio, 'uuid') music_key = music.key assert len(get_music(key=music_key)) == 1 self.app.delete('/api/music/%s/' % (music_key)) assert len(get_music(key=music_key)) == 0
def test_delete_music(self): title = 'music_title' artist = 'music_artist' album = 'music_album' company = 'music_company' public_time = '2013' kbps = '64' uuid = 'douban-uuid' music = add_music(title, artist, album, company, public_time, kbps, self.cover, self.audio, uuid) assert len(get_music()) == 1 music = get_music()[0] delete_music(music) assert len(get_music()) == 0
def test_patch(self): self.login_as_admin() music = add_music('title', 'artist', 'album', 'company', '2001', '64', self.cover, self.audio, 'uuid') music_key = music.key rv = self.app.patch('/api/music/%s/' % (music_key), data={'title': 'only_title'}) rv_music = json.loads(rv.data) assert rv_music['title'] == 'only_title' rv = self.app.patch('/api/music/%s/' % (music_key), data={'title': 'new_title', 'artist': 'new_artist', 'album': 'new_album', 'company': 'new_company', 'public_time': '2014', }) rv_music = json.loads(rv.data) assert rv_music['title'] == 'new_title' assert rv_music['artist'] == 'new_artist' assert rv_music['album'] == 'new_album' assert rv_music['company'] == 'new_company' assert rv_music['public_time'] == '2014'
def test_get(self): music1 = add_music('title1', 'artist1', 'album1', 'company1', '2001', '64', self.cover, self.audio, 'uuid1') self.cover.seek(0) self.audio.seek(0) add_music('title2', 'artist2', 'album2', 'company2', '2001', '128', self.cover, self.audio, 'uuid2') self.cover.seek(0) self.audio.seek(0) add_music('title3', 'artist3', 'album3', 'company3', '2004', '64', self.cover, self.audio, 'uuid3') self.cover.seek(0) self.audio.seek(0) add_music('title4', 'artist4', 'album4', 'company4', '2004', '128', self.cover, self.audio, 'uuid4') rv = self.app.get('/api/music/?key=' + music1.key) # print json.loads(rv.data) assert len(json.loads(rv.data)) == 1 rv = self.app.get('/api/music/') assert json.loads(rv.data)['count'] == 4 rv = self.app.get('/api/music/?title=title1') assert len(json.loads(rv.data)) == 1 rv = self.app.get('/api/music/?kbps=64') assert len(json.loads(rv.data)) == 2 rv = self.app.get('/api/music/?title=title1&kbps=64') assert len(json.loads(rv.data)) == 1 rv = self.app.get('/api/music/?start=0&end=2') assert len(json.loads(rv.data)) == 2 rv = self.app.get('/api/music/?start=0') assert len(json.loads(rv.data)) == 4 # get cover and audio rv = self.app.get('/api/music/?start=0&end=1') music = json.loads(rv.data)[0] rv_cover = self.app.get(music['cover']) rv_audio = self.app.get(music['audio']) self.cover.seek(0) self.audio.seek(0) assert rv_cover.data == self.cover.read() assert rv_audio.data == self.audio.read()
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