def channels(uuid=None): """get one/all channels in db""" if not uuid: channel_list = get_channel() else: channel_list = get_channel(uuid=uuid) print 'key\t\t\t\tuuid\t\t\t\tname\t\t\t\tmusic_num\t\t\t\tplayable' for channel in channel_list: print '%s\t\t%s\t\t%s\t\t%s\t\t%s' % ( channel.key.encode('utf-8'), channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list), channel.playable)
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 auto_update(): """update until stop manually""" assert login(), 'check network or the DOUBAN_USER_NAME, DOUBAN_USER_PASSWORD in config.py' channels = get_channel(playable=True) while True: channel = random.choice(channels) print datetime.datetime.now() print '%s\t\t%s\t\t%s' % (channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list)) music_list = update_music_by_channel(channel, 5) print '%s\t\t%s\t\t%s' % (channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list))
def update_channel_num(uuid, num): """update channel by uuid and num""" num = int(num) print uuid, num channel = get_channel(uuid=uuid)[0] assert login(), 'check network or the DOUBAN_USER_NAME, DOUBAN_USER_PASSWORD in config.py' music_list = update_music_by_channel(channel, num) assert len(music_list) == num print 'update %s %s %s for %d music' % ( channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list), num)
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 update_channel_list(): r = requests.get("http://www.douban.com/j/app/radio/channels") r = json.loads(r.text) channel_list = [] assert 'channels' in r for channel in r['channels']: cid = int(channel['channel_id']) uuid = DOUBAN_CHANNEL_UUID_FORMAT % cid if cid != 0 and not get_channel(uuid=uuid): # not private list and not in db new_channel = add_channel(channel['name'], uuid) channel_list.append(new_channel.name) return channel_list
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 douban_spider_task(): info = u'tasks.spider_task.douban_spider_task: start %s' % (datetime.datetime.now()) spider_task_log.log_info(info) login() info = u'tasks.spider_task.douban_spider_task: login success %s' % (datetime.datetime.now()) spider_task_log.log_info(info) channel_list = update_channel_list() for channel_name in channel_list: info = u'tasks.spider_task.douban_spider_task: updated_channel %s' % channel_name spider_task_log.log_info(info) channel_list = get_channel() for channel in channel_list: music_list = update_music_by_channel(channel, channel.update_num) info = u'tasks.spider_task.douban_spider_task: channel %s, updated_num %d' % (channel, len(music_list)) print info spider_task_log.log_info(info) info = u'tasks.spider_task.douban_spider_task: end %s' % (datetime.datetime.now()) spider_task_log.log_info(info)
def get(self): args = ChannelQueryArgs().args if not args: return marshal(get_channel_status(), channel_status_fields) ret_channels = get_channel(**args) return marshal(ret_channels, channel_fields)
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 PlaylistKey(key): try: assert get_channel(key=key)[0] except: raise ValueError("Invalid Playlist Key") return key
def get(self, key): args = PlaylistQueryArgs().args num = args['num'] channel = get_channel(key=key)[0] return marshal(get_music_by_channel(channel, num), music_fields)
def get(self): ret_channels = get_channel(playable=True) return marshal(ret_channels, playlist_fields)