-
Notifications
You must be signed in to change notification settings - Fork 2
/
megaupload.py
93 lines (77 loc) · 2.55 KB
/
megaupload.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
import os
import re
import time
from threading import Thread
import logger
import shutil
from util import UrlOpen
megalink_re = re.compile('<a.*?href="(http://.*megaupload.*/files/.*?)"')
url_open = UrlOpen()
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
self._last_read = 0
def get_megalink(self, link):
megalink = megalink_re.findall(url_open(link))
if megalink:
time.sleep(45)
return megalink[0]
return None
def open(self):
self._released = False
def release(self):
self._released = 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):
logger.log('offset: "%s" size: "%s"' % (offset, size), 'READ')
self._last_read = time.time()
while offset+size > self.size:
# EOF
if not self.running:
return ''
time.sleep(1)
logger.log('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 = self.get_megalink(self.url)
if not url:
logger.log('Cant retrieve megaupload url')
return
handle = url_open(url, handle=True)
fd = open(self.cache_file, 'w')
while True:
if self._released and self._last_read < time.time()-30:
# Remove file from cache if released
# before finish the download
# and 30 seconds happend from last read
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