def download(self, path, url):

        try:

            req = Request(url)
            req.add_header('User-Agent', randomagent())
            opener = urlopen(req)
            data = opener.read()
            zip_file = zipfile.ZipFile(BytesIO(data))
            opener.close()
            files = zip_file.namelist()
            srt = [i for i in files if i.endswith(('.srt', '.sub'))][0]
            subtitle = control.join(path, srt)

            zip_file.extractall(path)

            return subtitle

        except Exception as e:

            _, __, tb = sys.exc_info()

            print(traceback.print_tb(tb))

            log_debug(
                'Podnapisi.net subtitle download failed for the following reason: '
                + str(e))

            return
Пример #2
0
def _load_from_uri(uri,
                   timeout=None,
                   headers={},
                   custom_tags_parser=None,
                   verify_ssl=True):

    request = Request(uri, headers=headers)
    context = None

    if not verify_ssl:
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE

    resource = urlopen(request, timeout=timeout, context=context)
    base_uri = _parsed_url(resource.geturl())

    if PYTHON_MAJOR_VERSION < (3, ):
        content = _read_python2x(resource)
    else:
        content = _read_python3x(resource)

    return M3U8(content,
                base_uri=base_uri,
                custom_tags_parser=custom_tags_parser)
Пример #3
0
def _load_from_uri(uri, timeout=None, headers=None):
    if headers is None:
        headers = {}
    request = Request(uri, headers=headers)
    resource = urlopen(request, timeout=timeout)
    base_uri = _parsed_url(_url_for(request))
    if PYTHON_MAJOR_VERSION < (3, ):
        content = _read_python2x(resource)
    else:
        content = _read_python3x(resource)
    return M3U8(content, base_uri=base_uri)
Пример #4
0
def _load_from_uri(uri, timeout=None, headers={}, custom_tags_parser=None, verify_ssl=True):
    request = Request(uri, headers=headers)
    context = None
    if not verify_ssl:
        context = ssl._create_unverified_context()
    resource = urlopen(request, timeout=timeout, context=context)
    base_uri = _parsed_url(resource.geturl())
    if PYTHON_MAJOR_VERSION < (3,):
        content = _read_python2x(resource)
    else:
        content = _read_python3x(resource)
    return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)
Пример #5
0
def _url_for(request):
    return urlopen(request).geturl()
Пример #6
0
    def download(self, path, url):

        try:

            url = re.findall(r'/(\d+)/', url + '/', re.I)[-1]
            url = ''.join([self.download_link, '/getp.php?id={0}'.format(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()
            files = zip_file.namelist()
            files = [i for i in files if i.startswith('subs/')]

            srt = [i for i in files if i.endswith(('.srt', '.sub'))]
            archive = [i for i in files if i.endswith(('.rar', '.zip'))]

            if len(srt) > 0:

                if len(srt) > 1:
                    srt = multichoice(srt)
                else:
                    srt = srt[0]

                result = zip_file.open(srt).read()

                subtitle = basename(srt)

                try:
                    subtitle = control.join(path, subtitle.decode('utf-8'))
                except Exception:
                    subtitle = control.join(path, subtitle)

                with open(subtitle, 'wb') as subFile:
                    subFile.write(result)

                return subtitle

            elif len(archive) > 0:

                if len(archive) > 1:
                    archive = multichoice(archive)
                else:
                    archive = archive[0]

                result = zip_file.open(archive).read()

                f = control.join(path, os_split(url)[1])

                with open(f, 'wb') as subFile:
                    subFile.write(result)

                dirs, files = control.listDir(path)

                if len(files) == 0:
                    return

                if zipfile.is_zipfile(f):

                    zipped = zipfile.ZipFile(f)
                    zipped.extractall(path)

                if not zipfile.is_zipfile(f):

                    if control.infoLabel('System.Platform.Windows'):
                        uri = "rar://{0}/".format(quote(f))
                    else:
                        uri = "rar://{0}/".format(quote_plus(f))

                    dirs, files = control.listDir(uri)

                else:

                    dirs, files = control.listDir(path)

                if dirs and not zipfile.is_zipfile(f):

                    for dir in dirs:

                        _dirs, _files = control.listDir(control.join(uri, dir))

                        [files.append(control.join(dir, i)) for i in _files]

                        if _dirs:

                            for _dir in _dirs:

                                _dir = control.join(_dir, dir)

                                __dirs, __files = control.listDir(
                                    control.join(uri, _dir))

                                [
                                    files.append(control.join(_dir, i))
                                    for i in __files
                                ]

                filenames = [i for i in files if i.endswith(('.srt', '.sub'))]

                filename = multichoice(filenames)

                try:

                    filename = filename.decode('utf-8')

                except Exception:

                    pass

                if not control.exists(control.join(
                        path,
                        os_split(filename)[0])) and not zipfile.is_zipfile(f):
                    control.makeFiles(control.join(path,
                                                   os_split(filename)[0]))

                subtitle = control.join(path, filename)

                if not zipfile.is_zipfile(f):

                    with closing(control.openFile(uri + filename)) 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')))

                fileparts = os_split(subtitle)[1].split('.')
                # noinspection PyTypeChecker
                result = control.join(
                    os_split(subtitle)[0],
                    'subtitles.' + fileparts[len(fileparts) - 1])

                control.rename(subtitle, result)

                return result

        except Exception as e:

            _, __, tb = sys.exc_info()

            print(traceback.print_tb(tb))

            log_debug(
                'Subtitles.gr subtitle download failed for the following reason: '
                + str(e))

            return
    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
Пример #8
0
    def download(self, path, url):

        # try:

        url = re.findall('/(\d+)/', url + '/', re.I)[-1]
        url = 'http://www.greeksubtitles.info/getp.php?id={0}'.format(url)
        url = client.request(url, output='geturl')

        data = urlopen(url, timeout=20).read()
        zip_file = zipfile.ZipFile(StringIO(data))
        files = zip_file.namelist()
        files = [i for i in files if i.startswith('subs/')]

        srt = [i for i in files if i.endswith(('.srt', '.sub'))]
        archive = [i for i in files if i.endswith(('.rar', '.zip'))]

        if len(srt) > 0:

            if len(srt) > 1:
                srt = multichoice(srt)
            else:
                srt = srt[0]

            result = zip_file.open(srt).read()

            subtitle = basename(srt)

            try:
                subtitle = control.join(path, subtitle.decode('utf-8'))
            except Exception:
                subtitle = control.join(path, subtitle)

            with open(subtitle, 'wb') as subFile:
                subFile.write(result)

            return subtitle

        elif len(archive) > 0:

            if len(archive) > 1:
                archive = multichoice(archive)
            else:
                archive = archive[0]

            result = zip_file.open(archive).read()

            f = control.join(path, os_split(url)[1])

            with open(f, 'wb') as subFile:
                subFile.write(result)

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if zipfile.is_zipfile(f):

                # try:
                #     zipped = zipfile.ZipFile(f)
                #     zipped.extractall(path)
                # except Exception:
                control.execute('Extract("{0}","{0}")'.format(f, path))

            if not zipfile.is_zipfile(f):

                if control.infoLabel('System.Platform.Windows'):
                    uri = "rar://{0}/".format(quote(f))
                else:
                    uri = "rar://{0}/".format(quote_plus(f))

                dirs, files = control.listDir(uri)

            else:

                dirs, files = control.listDir(path)

            if dirs and not zipfile.is_zipfile(f):

                for dir in dirs:

                    _dirs, _files = control.listDir(control.join(uri, dir))

                    [files.append(control.join(dir, i)) for i in _files]

                    if _dirs:

                        for _dir in _dirs:

                            _dir = control.join(_dir, dir)

                            __dirs, __files = control.listDir(
                                control.join(uri, _dir))

                            [
                                files.append(control.join(_dir, i))
                                for i in __files
                            ]

            filenames = [i for i in files if i.endswith(('.srt', '.sub'))]

            filename = multichoice(filenames)

            try:

                filename = filename.decode('utf-8')

            except Exception:

                pass

            if not control.exists(control.join(
                    path,
                    os_split(filename)[0])) and not zipfile.is_zipfile(f):
                control.makeFiles(control.join(path, os_split(filename)[0]))

            subtitle = control.join(path, filename)

            if not zipfile.is_zipfile(f):

                content = openFile(uri + filename).read()

                with open(subtitle, 'wb') as subFile:
                    subFile.write(content)

            fileparts = os_split(subtitle)[1].split('.')
            result = control.join(
                os_split(subtitle)[0],
                'subtitles.' + fileparts[len(fileparts) - 1])

            rename(subtitle, result)

            return result