def download_media(url, path, file_name, initiate_int='', completion_int='', exception_int='', progress=None): PROGRESS = enum(OFF=0, WINDOW=1, BACKGROUND=2) try: if progress is None: progress = int(control.setting('progress_dialog')) active = not progress == PROGRESS.OFF background = progress == PROGRESS.BACKGROUND if isinstance(initiate_int, int): line1 = control.lang(initiate_int).format(file_name) else: line1 = 'Downloading {0}'.format(file_name) with control.ProgressDialog(control.addonInfo('name'), line1, background=background, active=active) as pd: try: headers = dict([ item.split('=') for item in (url.split('|')[1]).split('&') ]) for key in headers: headers[key] = unquote(headers[key]) except: headers = {} if 'User-Agent' not in headers: headers['User-Agent'] = cache.get(randomagent, 12) request = urllib2.Request(url.split('|')[0], headers=headers) response = urllib2.urlopen(request) if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) else: content_length = 0 file_name += '.' + get_extension(url, response) full_path = control.join(path, file_name) if log_debug: log_debug('Downloading: %s -> %s' % (url, full_path)) else: print('Downloading: %s -> %s' % (url, full_path)) path = control.transPath(control.legalfilename(path)) try: control.makeFiles(path) except Exception as e: if log_debug: log_debug('Path Create Failed: %s (%s)' % (e, path)) else: print('Path Create Failed: %s (%s)' % (e, path)) if not path.endswith(sep): path += sep if not control.exists(path): raise Exception('Failed to create dir') file_desc = control.openFile(full_path, 'w') total_len = 0 cancel = False while 1: data = response.read(512 * 1024) if not data: break if pd.is_canceled(): cancel = True break total_len += len(data) if not file_desc.write(data): raise Exception('Failed to write file') percent_progress = total_len * 100 / content_length if content_length > 0 else 0 if log_debug: log_debug('Position : {0} / {1} = {2}%'.format( total_len, content_length, percent_progress)) else: print('Position : {0} / {1} = {2}%'.format( total_len, content_length, percent_progress)) pd.update(percent_progress) file_desc.close() if not cancel: if isinstance(completion_int, int): control.infoDialog( control.lang(completion_int).format(file_name)) else: control.infoDialog( 'Download_complete for file name {0}'.format(file_name)) if log_debug: log_debug('Download Complete: {0} -> {1}'.format( url, full_path)) else: print('Download Complete: {0} -> {1}'.format(url, full_path)) except Exception as e: if log_debug: log_debug('Error ({0}) during download: {1} -> {2}'.format( str(e), url, file_name)) else: print('Error ({0}) during download: {1} -> {2}'.format( str(e), url, file_name)) if isinstance(exception_int, int): control.infoDialog( control.lang(exception_int).format(str(e), file_name)) else: control.infoDialog( 'Download_complete for file name {0}'.format(file_name))
def url2name(url): from os.path import basename url = url.split('|')[0] return basename(unquote(urlsplit(url)[2]))
def url2name(url): url = url.split('|')[0] return basename(unquote(urlsplit(url)[2]))
def download(self, path, url): if url.startswith('http'): log_debug( 'Vipsubs.gr: Attempting downloading from this url: {0}'.format( url)) _filename = unquote('.'.join(urlparse(url).path.split('/')[3:5])) filename = control.join(path, _filename) else: filename = control.join(path, url) try: if url.startswith('http'): if 'dropbox' in url: url = client.request(url, output='geturl', timeout=control.setting('timeout')) req = Request(url) req.add_header('User-Agent', randomagent()) opener = urlopen(req) data = opener.read() zip_file = zipfile.ZipFile(BytesIO(data)) opener.close() if control.setting('keep_zips') == 'true': if control.setting('output_folder').startswith( 'special://'): output_path = control.transPath( control.setting('output_folder')) else: output_path = control.setting('output_folder') if not os.path.exists(output_path): control.makeFile(output_path) # noinspection PyUnboundLocalVariable output_filename = control.join(output_path, _filename) with open(output_filename, 'wb') as f: f.write(data) if control.setting('extract') == 'true': zip_file = zipfile.ZipFile(output_filename) output_path = control.join( output_path, os.path.splitext(_filename)[0]) if not os.path.exists(output_path): control.makeFile(output_path) zip_file.extractall(output_path) control.infoDialog(control.lang(30007)) else: if zipfile.is_zipfile(filename): zip_file = zipfile.ZipFile(filename) else: log_debug( 'Failed to load zip with regular python library, attempting built-in method' ) control.execute('Extract({0},{1})'.format(filename, path)) zip_file = None if zip_file: files = zip_file.namelist() subs = [ i for i in files if i.endswith(('.srt', '.sub', '.zip')) ] else: subs = [] for root, _, file_ in os.walk(path): for f in file_: subs.append(os.path.join(root, f)) subtitle = multichoice(subs) if not subtitle: return if zip_file: try: zip_file.extract(subtitle, path) except Exception: path = path.encode('utf-8') zip_file.extract(subtitle, path) subtitle = control.join(path, subtitle) if subtitle.endswith('.zip'): return self.download(path, subtitle) else: try: with closing(control.openFile(subtitle)) as fn: try: output = bytes(fn.readBytes()) except Exception: output = bytes(fn.read()) content = output.decode('utf-16') with closing(control.openFile(subtitle, 'w')) as subFile: subFile.write(bytearray(content.encode('utf-8'))) except Exception: pass return subtitle except Exception as e: _, __, tb = sys.exc_info() print(traceback.print_tb(tb)) log_debug( 'Vipsubs.gr subtitle download failed for the following reason: ' + str(e)) return