forked from Roger/pycavane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fucavane.py
319 lines (252 loc) · 8.64 KB
/
fucavane.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import shutil
from errno import *
from stat import *
import fuse
import pycavane
import fusqlogger
from memo import Memoized
from threading import Thread
class MegaFile(Thread):
def __init__ (self, url, cachedir):
Thread.__init__(self)
self.url = url
self.filename = url.rsplit('/', 1)[1]
self.cachedir = cachedir
self.released = False
self.running = True
@property
def cache_file(self):
filename = self.cachedir+'/'+self.filename
if os.path.exists(filename+'.mp4'):
return filename+'.mp4'
return filename
@property
def size(self):
size = 0
if os.path.exists(self.cache_file):
size = os.path.getsize(self.cache_file)
return size
def read(self, offset, size):
fusqlogger.dump('offset: "%s" size: "%s"' % (offset, size), 'READ')
while offset+size > self.size:
# EOF
if not self.running:
return ''
time.sleep(1)
fusqlogger.dump('offset: "%s" size: "%s"' % \
(offset+size, self.size), 'WAIT')
with open(self.cache_file) as fd:
fd.seek(offset)
data = fd.read(size)
return data
def run(self):
if not os.path.exists(self.cache_file):
url = pycavane.get_megalink(self.url)
handle = pycavane.url_open(url, handle=True)
fd = open(self.cache_file, 'w')
while True:
if self.released:
# Remove file from cache if released
# before finish the download
os.remove(self.cache_file)
break
data = handle.read(1024)
if not data:
shutil.move(self.cache_file, self.cache_file+'.mp4')
fd.close()
break
fd.write(data)
fd.flush()
self.running = False
fuse.fuse_python_api = (0, 2)
class Metadata(fuse.Stat):
@fusqlogger.log()
def __init__(self, mode, is_dir):
fuse.Stat.__init__(self)
if is_dir:
self.st_mode = S_IFDIR | mode
self.st_nlink = 2
else:
self.st_mode = S_IFREG | mode
self.st_nlink = 1
now = int(time.time())
self.st_atime = now
self.st_mtime = now
self.st_ctime = now
self.st_uid = os.getuid()
self.st_gid = os.getgid()
#self.st_size = 0
class FuCavane(fuse.Fuse):
@fusqlogger.log()
def __init__(self, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
self.series = {}
self.episodes = {}
root_mode = S_IRUSR|S_IXUSR|S_IWUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH
file_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
# Create shared metadata for files and directories
self.dir_metadata = Metadata(root_mode, True)
self.file_metadata = Metadata(file_mode, False)
# Dictionary mapping inode_path -> (size, is_directory)
self.paths = ['/']
def main(self):
Memoized.set_cache_dir(self.cache)
# Fill with all tables as folders
for serie in self.get_series():
# TODO: better path sanitize
serie_name = serie[1].replace('/', '|')
serie_path = "/" + serie_name
self.paths.append(serie_path)
self.series[serie_path] = serie[0]
return fuse.Fuse.main(self)
@Memoized
def get_series(self):
return pycavane.get_series()
@Memoized
def get_seassons(self, serie):
return pycavane.get_seassons([serie])
@Memoized
def get_subtitle(self, serie):
return pycavane.get_subtitle([serie])
@Memoized
def get_episodes(self, seasson):
return pycavane.get_episodes([seasson])
@Memoized
def get_direct_link(self, episode):
return pycavane.get_direct_links([episode], 'megaupload')[1]
@Memoized
def get_file_size(self, episode):
ret = self.get_direct_link(episode)
fd = pycavane.url_open(ret, handle=True)
fz = '<strong>File size:</strong> '
for line in fd:
if fz in line:
try:
line = line.split(fz)[1].split('<br />', 1)[0]
size, measure = line.split(' ')
return float(size)*1024*1024
except Exception, error:
print error
print 'Sonthing change in MU implementation?'
return 0
break
@fusqlogger.log()
def getattr(self, path):
spath = path.split("/")
is_dir = len(spath) != 4
if path in self.paths:
if is_dir:
result = self.dir_metadata
else:
result = self.file_metadata
#result.st_size = 1024*1024
#if path[-3:] == 'mp4':
#episode = self.series[path[:-4]]
#result.st_size = self.get_file_size(episode)
else:
result = -ENOENT
return result
@fusqlogger.log()
def open(self, path, flags):
if path[-3:] == 'mp4':
episode = self.series[path[:-4]]
if episode not in self.episodes:
ret = self.get_direct_link(episode)
self.episodes[episode] = MegaFile(ret, self.cache)
self.episodes[episode].start()
return 0
@fusqlogger.log()
def read(self, path, size, offset):
episode = self.series[path[:-4]]
if path[-3:] == 'srt':
handle = self.get_subtitle(episode)
else:
if episode not in self.episodes:
raise Exception, 'WTF?'
handle = self.episodes[episode]
return handle.read(offset, size)
return handle[offset:offset+size]
@fusqlogger.log(showReturn=True)
def mknod(self, path, mode, rdev):
return 0
@fusqlogger.log(showReturn=True)
def write(self, path, buf, offset, fh=None):
return 0
@fusqlogger.log()
def truncate(self, path, size, fh=None):
return 0
@fusqlogger.log()
def unlink(self, path):
return 0
@fusqlogger.log(showReturn=True)
def rename(self, path_from, path_to):
return 0
@fusqlogger.log()
def chmod(self, path, mode):
return 0
@fusqlogger.log()
def chown(self, path, uid, gid):
return 0
@fusqlogger.log()
def utime(self, path, times):
return 0
@fusqlogger.log(showReturn=True)
def mkdir(self, path, mode):
return 0
@fusqlogger.log(showReturn=True)
def rmdir(self, path):
return 0
@fusqlogger.log()
def readdir(self, path, offset):
result = ['.', '..']
spath = path.split('/')
if len(spath) == 2 and spath[1]:
seassons = self.get_seassons(self.series[path])
for i in seassons:
new_path = path+'/'+i[1]
if new_path not in self.paths:
self.paths.append(new_path)
self.series[new_path] = i[0]
yield fuse.Direntry(i[1])
return
if len(spath) == 3 and spath[2]:
episodes = self.get_episodes(self.series[path])
for i in episodes:
name = '%03i - %s' %(int(i[1]), i[2].strip())
new_path = path+'/'+name
if new_path not in self.paths:
self.paths.append(new_path+'.mp4')
self.paths.append(new_path+'.srt')
self.series[new_path] = i[0]
yield fuse.Direntry(name+'.mp4')
yield fuse.Direntry(name+'.srt')
return
if path != "/":
path = path + "/"
for i in self.paths:
if i.startswith(path) and i != "/":
name = i.split(path)[1]
name = name.split("/")[0]
if name not in result:
result.append(name)
for i in result:
yield fuse.Direntry(i)
@fusqlogger.log()
def release(self, path, fh=None):
if path[-3:] == 'mp4':
episode = self.series[path[:-4]]
# TODO: handle multiple opens
if episode in self.episodes:
self.episodes[episode].released = True
del(self.episodes[episode])
return 0
if __name__ == '__main__':
fs = FuCavane()
fs.parser.add_option("-c", "--cache", metavar="CACHE",
default='.', help="[default:.mountpoint]")
fs.parse(values=fs, errex=1)
fs.main()