-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfy_mog.py
183 lines (157 loc) · 6.28 KB
/
fy_mog.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
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
# gearman_client.py
# schedule the task of movie, series, useries downloading
import sys
import oursql
from optparse import OptionParser
import mogilefs
import gearman_client
from ctypes import *
from ffmpeg import *
class Action(object):
def __init__(self, options):
self.options = options
self.client = mogilefs.client.Client(domain=self.options.domain, hosts=self.options.trackers.split(','))
def _setup_db(self):
self.db = oursql.connect(
host = self.options.host,
port = self.options.port,
user = self.options.user,
passwd = self.options.pwd,
db = self.options.db)
class ListAction(Action):
def doAction(self):
try:
keylist = self.client.list_keys(self.options.source_id)
for k in keylist:
print k
except Exception as e:
print e
class DeleteAction(Action):
def doAction(self):
print "Delete %s" % self.options.source_id
#delete form database
self._setup_db()
print "Delete %s from database." % self.options.source_id
params = (self.options.source_id, )
sql = "delete from fy_video where source_id=?"
with self.db.cursor() as cursor:
cursor.execute(sql, params)
sql = "delete from fy_fav where source_id=?"
with self.db.cursor() as cursor:
cursor.execute(sql, params)
sql = "delete from fy_share where source_id=?"
with self.db.cursor() as cursor:
cursor.execute(sql, params)
#delete files of this source_id, include mp4, jpg, m3u8, ts
try:
keylist = self.client.list_keys(self.options.source_id)
for k in keylist:
print "Delete %s from MogileFS." % k
self.client.delete(k)
except Exception as e:
print e
class CheckAction(Action):
def doAction(self):
self._setup_db()
sql = "select source_id, status, created_time from fy_video where channel>2 and status<>100 order by created_time limit 100"
with self.db.cursor() as cursor:
cursor.execute(sql)
result_list = cursor.fetchall()
for r in result_list:
self.options.source_id = r[0]
d = DeleteAction(self.options)
d.doAction()
class Checkm3u8Action(Action):
def doAction(self):
self._setup_db()
sql = """select source_id, status, created_time from fy_video
where channel>2 order by created_time"""
with self.db.cursor() as cursor:
cursor.execute(sql)
result_list = cursor.fetchall()
for r in result_list:
paths = self.client.get_paths(r[0]+".m3u8")
if len(paths)<=0:
self.options.source_id = r[0]
d = DeleteAction(self.options)
d.doAction()
class CheckFavAction(Action):
def doAction(self):
self._setup_db()
sql = """select source_id, status, created_time from fy_video
where channel>2 and share_count>0 order by created_time limit 100"""
with self.db.cursor() as cursor:
cursor.execute(sql)
result_list = cursor.fetchall()
for r in result_list:
paths = self.client.get_paths(r[0]+".m3u8")
if len(paths)<=0:
self.options.source_id = r[0]
d = DeleteAction(self.options)
d.doAction()
class ClearKeysAction(Action):
def doAction(self):
try:
keylist = self.client.list_keys(self.options.key_prefix)
for k in keylist:
print "Delete %s from MogileFS." % k
self.client.delete(k)
except Exception as e:
print e
class AddAction(Action):
def doAction(self):
av_register_all()
pfmtcx = POINTER(AVFormatContext)()
if avformat_open_input(pfmtcx, self.options.filepath, None, None):
print "Cannot open file %s" % self.options.filepath
if avformat_find_stream_info(pfmtcx, None) != 0:
print "Cannot find stream info"
fmtcx = pfmtcx.contents
for i in range(fmtcx.nb_streams):
s = fmtcx.streams[i].contents
codec_type = s.codec.contents.codec_type
if codec_type == AVMEDIA_TYPE_VIDEO:
duration = s.duration * s.time_base.num / s.time_base.den
print duration
def main():
parser = OptionParser()
parser.add_option('--action', dest='action', help='action: add|list|delete|check|checkm3u8|checkfav')
parser.add_option('--source_id', dest='source_id', help='source_id of media file')
parser.add_option('--file', dest='filepath', help='input file')
parser.add_option('--key_prefix', dest='key_prefix', help='key prefix')
parser.add_option('--trackers', dest='trackers', default='127.0.0.1:7001', help='MogileFS trackers')
parser.add_option('--domain', dest='domain', default='fydomain', help='MogileFS Domain')
parser.add_option('--db-host', dest='host', default='mysql-server',
help='database host')
parser.add_option('--db-port', dest='port', default=3306, type='int',
help='database port')
parser.add_option('--db-user', dest='user', default='feiying',
help='database user')
parser.add_option('--db-password', dest='pwd', default='feiying123',
help='database password')
parser.add_option('--db-name', dest='db', default='feiying',
help='database name')
(options, args) = parser.parse_args()
if options.action==None:
parser.print_help()
sys.exit()
if options.action == 'list':
action = ListAction(options)
elif options.action == 'delete':
action = DeleteAction(options)
elif options.action == 'check':
action = CheckAction(options)
elif options.action == 'checkm3u8':
action = Checkm3u8Action(options)
elif options.action == 'add':
action = AddAction(options)
elif options.action == 'checkfav':
action = CheckFavAction(options)
elif options.action == 'clearkeys':
action = ClearKeysAction(options)
else:
parser.print_help()
sys.exit()
action.doAction();
if __name__ == '__main__':
main()