示例#1
0
    def download(self, path, url):

        try:
            result = client.request(url)
            # f = os.path.splitext(urlparse(url).path)[1][1:]
            f = os.path.join(path, url.rpartition('/')[2])

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

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if not f.lower().endswith('.rar'):
                control.execute('Extract("%s","%s")' % (f, path))

            if control.infoLabel('System.Platform.Windows'):
                conversion = quote
            else:
                conversion = quote_plus

            if f.lower().endswith('.rar'):

                uri = "rar://{0}/".format(conversion(f))
                dirs, files = control.listDir(uri)

            else:

                for i in range(0, 10):

                    try:
                        dirs, files = control.listDir(path)
                        if len(files) > 1:
                            break
                        if control.aborted is True:
                            break
                        control.wait(1)
                    except BaseException:
                        pass

            filename = [
                i for i in files if any(
                    i.endswith(x) for x in ['.srt', '.sub'])
            ][0].decode('utf-8')
            subtitle = os.path.join(path, filename)

            if f.lower().endswith('.rar'):

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

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

                control.deleteFile(f)

                return subtitle

            else:

                control.deleteFile(f)

                return subtitle

        except BaseException:

            pass
示例#2
0
文件: client.py 项目: kenia-borges/Dr
def download_media(url, path, file_name, progress=None):

    try:
        if progress is None:
            progress = int(control.setting('progress_dialog'))

        active = not progress == PROGRESS.OFF
        background = progress == PROGRESS.BACKGROUND

        with control.ProgressDialog(
                control.addonInfo('name'), control.lang(30500).format(file_name), 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'] = randomagent()

            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)
            log_debug('Downloading: %s -> %s' % (url, full_path))

            path = control.transPath(control.legalfilename(path))

            try:
                control.makeFiles(path)
            except Exception as e:
                log_debug('Path Create Failed: %s (%s)' % (e, path))

            from os import sep

            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
                log_debug('Position : {0} / {1} = {2}%'.format(total_len, content_length, percent_progress))
                pd.update(percent_progress)

            file_desc.close()

        if not cancel:
            control.infoDialog(control.lang(30501).format(file_name))
            log_debug('Download Complete: {0} -> {1}'.format(url, full_path))

    except Exception as e:
        log_debug('Error ({0}) during download: {1} -> {2}'.format(str(e), url, file_name))
        control.infoDialog(control.lang(30502).format(str(e), file_name))
示例#3
0
    def download(self, path, url):

        try:
            frame, url, cjcfduid, cjphp, sub_, imdb_ = url.split('|')
            # xbmc.log('$#$ FRAME: %s | URL: %s | COOKIE: %s | SUB: %s | imdb: %s | ' % (frame, url, cjcfduid, sub_, imdb_))

            sub_ = urllib.unquote_plus(sub_)

            self.s.cookies.update({'__cfduid': cjcfduid, 'PHPSESSID': cjphp})
            # xbmc.log('$#$ FRAME-COOKIES: %s' % self.s.cookies)

            self.s.headers['Referer'] = frame
            init = self.s.get(url).text
            try:
                imdb = client.parseDOM(init,
                                       'input',
                                       ret='value',
                                       attrs={'name': 'uid'})[0]
            except IndexError:
                imdb = imdb_
            try:
                sub_name = client.parseDOM(init,
                                           'input',
                                           ret='value',
                                           attrs={'name': 'output'})[0]
            except IndexError:
                sub_name = '{}.srt'.format(sub_)

            self.s.headers.update({
                'Referer': url,
                'Origin': 'https://subztv.online'
            })

            # xbmc.log('$#$ FRAME-HEADERS: %s' % self.s.headers, xbmc.LOGNOTICE)
            post = {
                "langcode": "el",
                "uid": imdb,
                "output": sub_name.lower(),
                "dll": "1"
            }
            # post = urllib.urlencode(post)
            # xbmc.log('$#$ FRAME-POST: %s' % post)

            result = self.s.post(url, data=post)
            #xbmc.log('$#$POST-RESUL: %s' % result.content)
            f = os.path.join(path, urllib.quote(sub_) + '.srt')
            with open(f, 'wb') as subFile:
                subFile.write(result.content)

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if not f.lower().endswith('.rar'):
                control.execute('Extract("%s","%s")' % (f, path))

            if control.infoLabel('System.Platform.Windows'):
                conversion = urllib.quote
            else:
                conversion = urllib.quote_plus

            if f.lower().endswith('.rar'):

                uri = "rar://{0}/".format(conversion(f))
                dirs, files = control.listDir(uri)

            else:

                for i in range(0, 10):

                    try:
                        dirs, files = control.listDir(path)
                        if len(files) > 1:
                            break
                        if control.aborted is True:
                            break
                        control.wait(1)
                    except BaseException:
                        pass

            filename = [
                i for i in files if any(
                    i.endswith(x) for x in ['.srt', '.sub'])
            ][0].decode('utf-8')
            subtitle = os.path.join(path, filename)

            if f.lower().endswith('.rar'):

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

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

                return subtitle

            else:

                return subtitle

        except BaseException:
            pass
示例#4
0
    def download(self, path, url):

        try:
            url, php= url.split('|')
            if 'subs4series' in url:
                headers = {
                    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3',
                    'Referer': url,
                    'Origin': 'https://www.subs4series.com/'}
                cj = {'PHPSESSID': php}

                r = requests.get(url, headers=headers, cookies=cj).content
                # r = re.sub(r'[^\x00-\x7F]+', ' ', r)
                if six.PY2:
                    r = re.sub(r'[^\x00-\x7F]+', ' ', r)
                # try:
                #     r = r.decode('utf-8', errors='replace')
                # except AttributeError:
                #     pass
                # xbmc.log('@@HTML:%s' % r)

                pos = re.findall(r'''href=["'](/getSub-.+?)["']''', r, re.I | re.DOTALL)[0]
                # xbmc.log('@@POSSSSS:%s' % pos)
                post_url = urljoin(self.base_TVlink, pos)
                # xbmc.log('@@POStttt:%s' % post_url)
                r = requests.get(post_url, headers=headers, cookies=cj)
                surl = r.url
                result = client.request(surl)


            else:
                headers = {
                    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3',
                    'Referer': url,
                    'Origin': 'https://www.sf4-industry.com'}
                cj = {'PHPSESSID': php}
                post_url = 'https://www.sf4-industry.com/getSub.php'

                r = requests.get(url, headers=headers, cookies=cj).text
                if six.PY2:
                    r = re.sub(r'[^\x00-\x7F]+', ' ', r)
                # try:
                #     r = r.decode('utf-8', errors='replace')
                # except AttributeError:
                #     pass
                # xbmc.log('@@HTMLLL:%s' % r)
                pos = client.parseDOM(r, 'div', attrs={'class': 'download-btn'})[0]
                pos = client.parseDOM(pos, 'input', ret='value', attrs={'name': 'id'})[0]
                # pos = re.findall(r'getSub-(\w+)\.html', r, re.I | re.DOTALL)[0]
                post = {'id': pos,
                        'x': '107',
                        'y': '35'}

                r = requests.post(post_url, headers=headers, data=post, cookies=cj)
                # surl = r.headers['Location']
                surl = r.url
                result = client.request(surl)
                # surl = self.base_link + surl if surl.startswith('/') else surl

            # path = 'special://userdata/addon_data/service.subtitles.greeksubs/temp/'
            f = os.path.join(path, surl.rpartition('/')[2])
            # if f.lower().endswith('.rar') and not control.condVisibility('system.platform.osx'):
            #     return control.okDialog('GreekSubs', 'Το αρχείο υποτίτλου είναι σε μορφή rar\n και δεν μπορεί να ληφθεί.\n'
            #                             'Δοκιμάστε άλλον υπότιτλο!')

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

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if not f.lower().endswith('.rar'):
                control.execute('Extract("{}","{}")'.format(f, path))

            if control.condVisibility('system.platform.windows'):
                conversion = quote
            else:
                conversion = quote_plus

            if f.lower().endswith('.rar'):
                    import xbmcvfs
                # if control.condVisibility('system.platform.osx'):
                #     uri = "rar://{}/".format(conversion(f))
                    uri = 'rar://%(archive_file)s' % {'archive_file': conversion(control.transPath(f))}
                    dirs, files = control.listDir(uri)
                # else:
                #     return

            else:

                for i in range(0, 10):

                    try:
                        dirs, files = control.listDir(path)
                        if len(files) > 1:
                            break
                        if control.aborted is True:
                            break
                        control.wait(1)
                    except BaseException:
                        pass

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

            if len(filenames) == 1:
                filename = filenames[0]
            else:
                filename = multichoice(filenames)

            try:
                filename = filename.decode('utf-8')
            except BaseException:
                pass

            subtitle = os.path.join(path, filename)

            if f.lower().endswith('.rar'):

                content = control.openFile(path + filename).read()

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

                control.deleteFile(f)
                return subtitle

            else:

                return subtitle

        except BaseException:
            pass
示例#5
0
    def download(self, path, url):

        try:
            url, sub_, php, imdb = url.split('|')

            headers = {
                'User-Agent':
                'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3',
                'Referer': url
            }

            cj = {'PHPSESSID': php, 'share_show_hide_status': 'active'}

            post = {
                'langcode': 'el',
                'uid': imdb,
                'output': '%s.srt' % sub_,
                'dll': '1'
            }

            r = requests.get(url, headers=headers, cookies=cj)
            result = requests.post(url, headers=headers, data=post,
                                   cookies=cj).content

            f = os.path.join(path, urllib.quote(sub_) + '.srt')

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

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if not f.lower().endswith('.rar'):
                control.execute('Extract("%s","%s")' % (f, path))

            if control.infoLabel('System.Platform.Windows'):
                conversion = urllib.quote
            else:
                conversion = urllib.quote_plus

            if f.lower().endswith('.rar'):

                uri = "rar://{0}/".format(conversion(f))
                dirs, files = control.listDir(uri)

            else:

                for i in range(0, 10):

                    try:
                        dirs, files = control.listDir(path)
                        if len(files) > 1:
                            break
                        if control.aborted is True:
                            break
                        control.wait(1)
                    except BaseException:
                        pass

            filename = [
                i for i in files if any(
                    i.endswith(x) for x in ['.srt', '.sub'])
            ][0].decode('utf-8')
            subtitle = os.path.join(path, filename)

            if f.lower().endswith('.rar'):

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

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

                return subtitle

            else:

                return subtitle

        except BaseException:
            pass
示例#6
0
    def download(self, path, url):

        try:
            url, php, cfd = url.split('|')
            if 'subs4series' in url:
                headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3',
                           'Referer': url,
                           'Origin': 'https://www.subs4series.com/'}
                cj = {'PHPSESSID': php,
                      '__cfduid': cfd}

                r = requests.get(url, headers=headers, cookies=cj).content
                r = re.sub(r'[^\x00-\x7F]+', ' ', r)

                pos = re.findall('\/(getSub-\w+\.html)', r, re.I|re.DOTALL)[0]
                post_url = urlparse.urljoin(self.base_TVlink, pos)
                r = requests.get(post_url, headers=headers, cookies=cj)
                result = r.content
                surl = r.url

            else:
                headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3',
                           'Referer': url,
                           'Origin': 'https://www.sf4-industry.com'}
                cj = {'PHPSESSID': php,
                      '__cfduid': cfd}
                post_url = 'https://www.sf4-industry.com/getSub.html'

                r = requests.get(url, headers=headers, cookies=cj).content
                r = re.sub(r'[^\x00-\x7F]+', ' ', r)
                #pos = client.parseDOM(r, 'tr', attrs={'class':'stylepps'})[0]
                pos = re.findall('getSub-(\w+)\.html', r, re.I | re.DOTALL)[0]
                post = {'id': pos,
                        'x': '107',
                        'y': '35'}

                r = requests.post(post_url, headers=headers, data=post, cookies=cj)
                result = r.content
                surl = r.url

            f = os.path.join(path, surl.rpartition('/')[2])

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

            dirs, files = control.listDir(path)

            if len(files) == 0:
                return

            if not f.lower().endswith('.rar'):
                control.execute('Extract("%s","%s")' % (f, path))

            if control.infoLabel('System.Platform.Windows'):
                conversion = urllib.quote
            else:
                conversion = urllib.quote_plus

            if f.lower().endswith('.rar'):

                uri = "rar://{0}/".format(conversion(f))
                dirs, files = control.listDir(uri)

            else:

                for i in range(0, 10):

                    try:
                        dirs, files = control.listDir(path)
                        if len(files) > 1:
                            break
                        if control.aborted is True:
                            break
                        control.wait(1)
                    except BaseException:
                        pass

            filename = [i for i in files if any(i.endswith(x) for x in ['.srt', '.sub'])][0].decode('utf-8')
            subtitle = os.path.join(path, filename)

            if f.lower().endswith('.rar'):

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

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

                return subtitle

            else:

                return subtitle

        except BaseException:
            pass