-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfm
More file actions
executable file
·194 lines (173 loc) · 5.9 KB
/
fm
File metadata and controls
executable file
·194 lines (173 loc) · 5.9 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import os
import os.path
import sys
import socket
import logging
import doubanfm.util
from doubanfm.util import cachedir, socketfile, stdout, stderr, \
initParent, readReplyLine, readUtilEOFLine
parser = argparse.ArgumentParser(description="命令行的 豆瓣FM ,\
可以使用 Firecookie(firefox 的插件) 导出的 cookie 文件,\
cookie 文件的默认路径为 %s/cookies.txt ,可以在配置文件 \
~/.config/python-doubanfm/doubanfm.conf 中进行配置" % cachedir)
parser.add_argument('-s', '--server', action='store_true', help="启动服务")
parser.add_argument('-d',
'--debug',
action='store_true',
help="debug 模式,和 --server 一起使用,在前台运行 server")
parser.add_argument('-p',
'--play',
action='store_const',
const='play',
dest="cmd",
help="播放")
parser.add_argument('-P',
'--pause',
action='store_const',
const='pause',
dest="cmd",
help="暂停")
parser.add_argument('-G',
'--toggle-pause',
action='store_const',
const='togglePause',
dest="cmd",
help="播放/暂停")
parser.add_argument('-n',
'--next',
action='store',
nargs='?',
const=0,
type=int,
help="下一首歌曲,可以指定播放第 n 首歌曲,\
如果 n 为 0 ,则会获取新的歌曲列表,\
这是 豆瓣FM 默认的行为,如果不指定 n 默认为 0")
parser.add_argument('-C',
'--cycling',
action='store_const',
const='cycling',
dest="cmd",
help="单曲循环")
parser.add_argument('-f',
'--favourite',
action='store_const',
const='favourite',
dest="cmd",
help="喜欢")
parser.add_argument('-u',
'--unfavourite',
action='store_const',
const='unFavourite',
dest="cmd",
help="不喜欢")
parser.add_argument('-i',
'--info',
action='store_const',
const='info',
dest='cmd',
help="当前歌曲信息")
parser.add_argument('-l',
'--list',
action='store_const',
const='list',
dest='cmd',
help="歌曲列表")
parser.add_argument('-x',
'--exit',
action='store_const',
const='exit',
dest="cmd",
help="关闭服务")
parser.add_argument('-U',
'--update',
action='store_const',
const='update',
dest='cmd',
help='更新 RSS')
LIST_CHANNEL = 'listChannel'
parser.add_argument('-c',
'--channel',
nargs='?',
action='store',
const=LIST_CHANNEL,
dest='channel',
help='选择某个频道,如果没有指定频道,则会列出所有可选的频道')
parser.add_argument('-r',
'--reply',
action='store_true',
help='等待应答信息,-i -l 本来就会等待应答')
args = parser.parse_args()
def serverHasStart():
return os.path.exists(socketfile)
def getsocket():
s = socket.socket(socket.AF_UNIX)
s.connect(socketfile)
return s
def sendcmd(cmd, waitReply, *args):
s = getsocket()
f = s.makefile('rw')
f.write(cmd)
f.write(' ')
f.write(' '.join(map(str, args)))
f.write('\n')
f.flush()
if waitReply:
result, message = readReplyLine(f)
if 'OK' == result:
if message:
print message
else:
print 'OK'
elif 'FAIL' == result or 'ERROR' == result:
print result, message
elif 'VALUE' == result:
value = readUtilEOFLine(f, message)
print value.rstrip()
else:
print 'UNKNOW REPLY %s %s' % (result, message)
s.close()
if args.server:
if serverHasStart():
print '服务已经启动,如果服务并未启动请执行 rm %s' % socketfile
sys.exit()
if args.debug:
initParent(stdout)
initParent(stderr)
#sys.stdout = open(stdout, 'w')
#sys.stderr = open(stderr, 'w')
import doubanfm.listening
doubanfm.listening.start(args.debug)
else:
if not os.fork():
initParent(stdout)
initParent(stderr)
sys.stdout = open(stdout, 'w')
sys.stderr = open(stderr, 'w')
import doubanfm.listening
doubanfm.listening.start()
elif args.next is not None:
if serverHasStart():
index = int(args.next)
index = max(0, index)
sendcmd('next', args.reply, index)
else:
print >> sys.stderr, '服务尚未启动'
elif args.cmd:
if serverHasStart():
if args.cmd in ['info', 'list']:
sendcmd(args.cmd, True)
else:
sendcmd(args.cmd, args.reply)
else:
print >> sys.stderr, '服务尚未启动'
elif args.channel:
if serverHasStart():
if args.channel == LIST_CHANNEL:
sendcmd('channel', True)
else:
sendcmd('channel', args.reply, args.channel)
else:
parser.print_help()