-
Notifications
You must be signed in to change notification settings - Fork 19
/
manager.py
executable file
·108 lines (90 loc) · 3.53 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
#coding:utf8
import random
import datetime
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
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)
@manager.command
def tasks(hour):
'''run tasks, keep it running for spider'''
import logging
logging.basicConfig()
sched = Scheduler(standalone=True)
# http://pythonhosted.org/APScheduler/modules/scheduler.html
sched.add_cron_job(douban_spider_task, hour=int(hour))
# sched.add_interval_job(douban_spider_task, seconds=10)
print 'start tasks at', datetime.datetime.now()
sched.start()
if __name__ == "__main__":
manager.run()