Exemplo n.º 1
0
    def isdownload(self, url, resume=None, redirect=True):
        link      = False
        maxredirs = 10

        if resume is None:
            resume = self.resume_download

        if type(redirect) is int:
            maxredirs = max(redirect, 1)

        elif redirect:
            maxredirs = self.get_config("maxredirs", default=maxredirs, plugin="UserAgentSwitcher")

        for i in xrange(maxredirs):
            self.log_debug("Redirect #%d to: %s" % (i, url))

            header = self.load(url, just_header=True)

            if 'content-disposition' in header:
                link = url

            elif header.get('location'):
                location = self.fixurl(header.get('location'), url)
                code     = header.get('code')

                if code == 302:
                    link = location

                elif code == 301:
                    url = location
                    if redirect:
                        continue

                if resume:
                    url = location
                    continue

            else:
                mimetype    = ""
                contenttype = header.get('content-type')
                extension   = os.path.splitext(parse_name(url))[-1]

                if contenttype:
                    mimetype = contenttype.split(';')[0].strip()

                elif extension:
                    mimetype = mimetypes.guess_type(extension, False)[0] or "application/octet-stream"

                if mimetype and (link or 'html' not in mimetype):
                    link = url
                else:
                    link = False

            return link
Exemplo n.º 2
0
    def get_info(cls, url="", html=""):
        url = fixurl(url, unquote=True)
        info = {
            "name": parse_name(url),
            "pattern": {},
            "size": 0,
            "status": 3 if url else 8,
            "url": replace_patterns(url, cls.URL_REPLACEMENTS),
        }

        try:
            info["pattern"] = re.match(cls.__pattern__, url).groupdict()

        except Exception:
            pass

        return info
Exemplo n.º 3
0
    def download(self, url, get={}, post={}, ref=True, cookies=True, disposition=True, resume=None, chunks=None):
        """
        Downloads the content at url to download folder

        :param url:
        :param get:
        :param post:
        :param ref:
        :param cookies:
        :param disposition: if True and server provides content-disposition header\
        the filename will be changed if needed
        :return: The location where the file was saved
        """
        self.check_status()

        if self.pyload.debug:
            self.log_debug("DOWNLOAD URL " + url,
                           *["%s=%s" % (key, val) for key, val in locals().items() if key not in ("self", "url", "_[1]")])

        dl_url      = self.fixurl(url)
        dl_basename = parse_name(self.pyfile.name)

        self.pyfile.name = dl_basename

        self.captcha.correct()

        if self.pyload.config.get("download", "skip_existing"):
            self.check_filedupe()

        self.pyfile.setStatus("downloading")

        dl_folder   = self.pyload.config.get("general", "download_folder")
        dl_dirname  = os.path.join(dl_folder, self.pyfile.package().folder)
        dl_filename = os.path.join(dl_dirname, dl_basename)

        dl_dir  = encode(dl_dirname)
        dl_file = encode(dl_filename)  #@TODO: Move safe-filename check to HTTPDownload in 0.4.10

        if not exists(dl_dir):
            try:
                os.makedirs(dl_dir)

            except Exception, e:
                self.fail(e)
Exemplo n.º 4
0
    def _create_packages(self):
        """
        Create new packages from self.packages
        """
        package_folder   = self.pyfile.package().folder
        package_password = self.pyfile.package().password
        package_queue    = self.pyfile.package().queue

        folder_per_package    = self.pyload.config.get("general", "folder_per_package")
        use_subfolder         = self.get_config('use_subfolder', folder_per_package)
        subfolder_per_package = self.get_config('subfolder_per_package', True)

        for name, links, folder in self.packages:
            self.log_info(_("Parsed package: %s")  % name,
                          _("Found %d links")      % len(links),
                          _("Saved to folder: %s") % folder if folder else _("Saved to default download folder"))

            links = map(self.fixurl, links)
            self.log_debug("LINKS for package " + name, *links)

            pid = self.pyload.api.addPackage(name, links, package_queue)

            if package_password:
                self.pyload.api.setPackageData(pid, {'password': package_password})

            #: Workaround to do not break API addPackage method
            set_folder = lambda x="": self.pyload.api.setPackageData(pid, {'folder': fixname(x)})

            if use_subfolder:
                if not subfolder_per_package:
                    set_folder(package_folder)
                    self.log_debug("Set package %(name)s folder to: %(folder)s" % {'name': name, 'folder': folder})

                elif not folder_per_package or name is not folder:
                    if not folder:
                        folder = parse_name(name)

                    set_folder(folder)
                    self.log_debug("Set package %(name)s folder to: %(folder)s" % {'name': name, 'folder': folder})

            elif folder_per_package:
                set_folder()
Exemplo n.º 5
0
            self.pyfile.size = self.req.size

        if self.req.code in (404, 410):
            bad_file = fs_join(dl_dirname, newname)
            try:
                os.remove(bad_file)

            except OSError, e:
                self.log_debug(_("Error removing `%s`") % bad_file, e)

            else:
                return ""

        #@TODO: Recheck in 0.4.10
        if disposition and newname:
            safename = parse_name(newname.split(' filename*=')[0])

            if safename != newname:
                try:
                    old_file = fs_join(dl_dirname, newname)
                    new_file = fs_join(dl_dirname, safename)
                    os.rename(old_file, new_file)

                except OSError, e:
                    self.log_warning(_("Error renaming `%s` to `%s`")
                                     % (newname, safename), e)
                    safename = newname

                self.log_info(_("`%s` saved as `%s`") % (self.pyfile.name, safename))

            self.pyfile.name = safename