def index(): def add_user(name, password, level):
    '''add new user, return user obj'''
    user = UserStorage()
    user.name = name
    user.password = _encrypt(name, password)
    assert level in ['disable', 'normal', 'admin']
    user.level = level
    user.regist_date = datetime.datetime.now()
    try:
        user.save()
    except:
        return None
    return user def login():
    payload = {'app_name': DOUBAN_SPIDER_NAME,
               'version': DOUBAN_SPIDER_VERSION,
               'email': DOUBAN_USER_NAME,
               'password': DOUBAN_USER_PASSWORD}
    try:
        r = requests.post("http://www.douban.com/j/app/login", data=payload)
    except requests.exceptions.ConnectionError:
        return False
    except requests.exceptions.Timeout:
        return False
    r = json.loads(r.text)
    if r['r'] != 0:
        print 'spider.douban.login: failed. r=', r
        return False
    global _user_id, _token, _expire
    _user_id = r['user_id']
    _token = r['token']
    _expire = r['expire']
    return 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 len(get_channel(uuid=uuid)) == 0:
            # not private list and not in db
            new_channel = add_channel(channel['name'], uuid)
            channel_list.append(new_channel)
    return channel_list def update_music_by_channel(channel, num):
    '''update the music in channel, music count is num'''
    updated_music = []
    retry = 0
    while num > 0:
        music_list = _update_channel_once(channel, num)
        updated_music.extend(music_list)
        num -= len(music_list)
        if music_list == []:
            retry += 1
            if retry > 5:
                break
    return updated_music def get_channel(**kwargs):
    '''return channel obj'''
    channels = ChannelStorage.get(**kwargs)
    return channels def update_channel(channel, **kwargs):
    '''update channel obj by kwargs'''
    channel.update(**kwargs) def douban_spider_task():
    print 'tasks.spider_task.douban_spider_task: start %s' % (datetime.datetime.now())
    login()
    print 'tasks.spider_task.douban_spider_task: login success %s' % (datetime.datetime.now())
    channels = update_channel_list()
    print 'tasks.spider_task.douban_spider_task: updated_channel %s' % (channels)
    for channel in get_channel():
        music_list = update_music_by_channel(channel, channel.update_num)
        print (u'tasks.spider_task.douban_spider_task: channel %s, updated_num %d' % (channel, len(music_list))).encode('utf8')
    print 'tasks.spider_task.douban_spider_task: end %s' % (datetime.datetime.now()) ADMIN_NAME = 'admin' ADMIN_PASSWORD = 'admin'
import random
import datetime
    import logging
from apscheduler.scheduler import Scheduler
from flask.ext.script import Manager
from fm import app
from model.user import add_user
from spider.douban import login, update_channel_list, update_music_by_channel
from model.channel import get_channel, update_channel
from tasks.spider_task import douban_spider_task
from config import ADMIN_NAME, ADMIN_PASSWORD
#!/usr/bin/env python
#coding:utf8


manager = Manager(app)


@manager.command
def setup():
    '''setup db & update channel & get demo music'''
    print 'setuping...'
    print 'setting admin'
    try:
        assert add_user(ADMIN_NAME, ADMIN_PASSWORD, 'admin'), 'admin already exist!!!this will pass'
    except:
        pass
    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)


@manager.command
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)


@manager.command
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))


@manager.command
def channels(uuid=None):
    '''get one/all channels in db'''
    if not uuid:
        print 'uuid\t\tname\t\tmusic_num\t\tplayable'
        for channel in get_channel():
            print '%s\t\t%s\t\t%s\t\t%s' % (
                channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list), channel.playable)
    else:
        channel = get_channel(uuid=uuid)[0]
        print 'uuid\t\tname'
        print '%s\t\t%s\t\t%s\t\t%s' % (
            channel.uuid.encode('utf8'), channel.name.encode('utf8'), len(channel.music_list), channel.playable)


@manager.command
def enable_channel(uuid):
    '''set channel playable'''
    channel = get_channel(uuid=uuid)[0]
    update_channel(channel, playable=True)


@manager.command
def disable_channel(uuid):
    '''set channel not playable'''
    channel = get_channel(uuid=uuid)[0]

    update_channel(channel, playable=False)