1
# -*- coding: utf-8 -*-
12
import BeautifulSoup as BS
14
from PyQt4.QtCore import QObject, pyqtSignal, QString, QLocale
15
from PyQt4.QtGui import QApplication
18
class PlusLoader(QObject):
19
loadProgress = pyqtSignal(float)
20
loadingFailed = pyqtSignal(str, str)
21
def __init__(self, main):
22
super(PlusLoader, self).__init__()
25
self.is_loading = False
26
self.kill_request = False
31
def set_url(self, url):
34
def set_target(self, target):
35
self.target = target.encode(sys.getfilesystemencoding(), 'ignore')
37
def set_destination(self, dest):
38
self.dest = dest.encode(sys.getfilesystemencoding(), 'ignore')
42
print "URL: %s" % self.url
43
print "Tgt: %s" % self.target
44
print "Dst: %s" % self.dest
46
for percent in self.rtmp_download(self.url, self.target):
49
self.loadProgress.emit(percent)
50
if self.main.abort_download or self.main.stop:
51
if self.pid is not None:
53
os.kill(self.pid, signal.SIGINT)
55
# Not sure is still alive
60
print "Downloading error :", why
61
self.loadingFailed.emit('Error', str(why))
63
os.rename(self.target, self.dest)
64
except Exception as e:
65
print "Renaming error: ", e
67
print "File %s renamed %s " % (self.target, self.dest)
69
def rtmp_download(self, link, destination="/dev/null", try_resume=True,
73
if try_resume and os.path.isfile(destination):
74
for percent in self.rtmp_download(link, destination, False, True):
77
need_more_dl = percent != 100.0
81
cmd = "".join(['flvstreamer -r "', self.url, '" '])
82
cmd_dl = "".join([cmd, '--flv "', self.target, '"'])
83
cmd_resume = "".join([cmd, '--resume --flv "', self.target, '"'])
84
cmd_resume_skip = "".join([cmd, '--resume --skip 1 --flv "',
88
percent_re = re.compile("\((.+)%\)$")
91
if some_dl_done or resuming:
97
whole_stderr_buff = ""
98
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE,
101
while ret_code is None:
102
fds_read, fds_write, fds_exception = select.select([p.stderr],
103
[], [], SECONDS_TO_WAIT)
104
if len(fds_read) == 1:
106
#print "p.stderr.read", p.stderr.read()
107
whole_stderr_buff += c
108
#print "whole_stderr_buff", c
110
match = percent_re.search(stderr_buff)
111
if match is not None:
112
# If anyframe was retreived, then reset available
115
yield float(match.group(1))
120
whole_stderr_buff += p.stderr.read()
129
for line in whole_stderr_buff.split("\n"):
130
if line.find("Couldn't resume FLV file, try --skip 1") != -1:
133
if must_resume and max_skip_cnt >= 0:
135
cmd = cmd_resume_skip
137
#print whole_stderr_buff
142
class LiveLoader(QObject):
143
loadProgress = pyqtSignal(float)
144
loadComplete = pyqtSignal(str, str, str)
145
def __init__(self, main):
146
super(LiveLoader, self).__init__()
148
self.is_loading = False
149
self.kill_request = False
152
self.pattern = re.compile("(?P<scheme>[^:]*)://(?P<host>[^/^:]*):"
153
"{0,1}(?P<port>[^/]*)/(?P<app>.*?)/"
154
"(?P<playpath>\w*?\:.*)", re.DOTALL)
156
def set_url(self, url):
159
def set_target(self, target):
160
self.target = target.encode(sys.getfilesystemencoding(), 'ignore')
162
def set_destination(self, dest):
163
self.dest = dest.encode(sys.getfilesystemencoding(), 'ignore')
165
def format_command(self):
166
match = re.match(self.pattern, self.url)
168
print "Format command: ", self.url
170
cmd = "rtmpdump --host %host% --port %port% --protocol %scheme% --app %app% --playpath %playpath%"
171
cmd = cmd.replace( "%scheme%", match.group( "scheme" ) ).replace( "%host%", match.group( "host" ) ).replace( "%app%", match.group( "app" ) ).replace( "%playpath%", match.group( "playpath" ) )
172
if( match.group( "port" ) != "" ):
173
cmd = cmd.replace("%port%", match.group("port"))
174
elif(self.url[ :6 ] == "rtmpte"):
175
cmd = cmd.replace( "%port%", "80")
176
elif(self.url[ :5 ] == "rtmpe"):
177
cmd = cmd.replace( "%port%", "1935")
178
elif(self.url[ :5 ] == "rtmps"):
179
cmd = cmd.replace( "%port%", "443")
180
elif(self.url[ :5 ] == "rtmpt"):
181
cmd = cmd.replace("%port%", "80")
183
cmd = cmd.replace("%port%", "1935")
185
cmd = "rtmpdump -r " + self.url
189
if(self.url[ :4 ] == "rtmp"):
190
cmd = self.format_command()
191
cmd = "".join([cmd, " -o ", self.target])
192
elif(self.url[:4] == "http" or self.url[:3] == "ftp" or self.url[:3] == "mms"):
193
cmd = "".join(["msdl -c ", self.url, " -o ", self.target])
196
arguments = cmd.split()
197
print "\nDownloading ......\n %s\n" % arguments
199
reply = subprocess.Popen(arguments, universal_newlines=True,
200
stdout=subprocess.PIPE,
201
stderr=subprocess.STDOUT)
202
self.is_loading = True
203
except (IOError, OSError) as exc:
204
self.loadComplete.emit(self.target, "0", exc)
205
self.is_loading = False
211
text = reply.stdout.readline()[:-1]
212
if type(text) != str or text == '' and reply.poll() != None:
213
self.is_loading = False
215
elif type(text) == str and len(text) > 6:
217
self.is_loading = False
220
elif " sec (" in text:
221
chains = text.split(" ")
227
percent = float(chains[5][1:-2])
228
except Exception as exc:
229
print "Loading error in percent: ", exc
231
self.loadProgress.emit(percent)
232
print "text: %s >> %s" % (text, int(percent))
233
if self.kill_request:
235
self.is_loading = False
236
err = "Loading aborted by user"
239
print "Process break: ", text, reply.poll()
240
print "Done at: %s renaming ..." % datetime.datetime.now()
241
print "\\t %s" % self.target
242
print "\\t >> %s" % self.dest
243
self.loadComplete.emit(self.target, duration, err)
245
os.rename(self.target, self.dest)
246
except Exception as e:
247
print "Warning Renaming error: ", e
249
print "File %s renamed %s " % (self.target, self.dest)
251
class RateCounter(object):
252
def __init__(self, site, fname):
253
"""Calculate and show the downloading rate.
256
site -- ArtePlus or ArteLiveWeb instance
257
movie -- instance of movie
260
lang = QLocale.system().name()
261
self.locale = QLocale(lang)
263
self.time_base = time.time()
265
if os.path.isfile(self.target):
266
inf = os.stat(self.target)
267
self.size = inf.st_size
269
def counter(self, p):
270
"""Evaluate the rate of downloading.
276
Bandwidth, estimated remaining time
282
size = os.path.getsize(self.target)
291
delta = size - self.size
292
t = time.time() - self.time_base
293
r = int ((size / t) / 1024)
294
rate = self.locale.toString(r)
295
rem = ((t / p) * 100) - t
296
m, s = divmod(rem, 60)
297
st = QString(QApplication.translate("MainWindow",
299
QApplication.UnicodeUTF8))
300
st2 = QString(QApplication.translate("MainWindow",
301
"Remaining time:", None,
302
QApplication.UnicodeUTF8))
303
rate_txt = "%s %s Kio/sec." % (st, rate)
304
rem_txt = "%s %s min. %s sec." % (st2, int(m), int(s))
305
return rate_txt, rem_txt