Пример #1
0
    def _createType(self, meta_name, root, movie_info, group, file_type, i):  # Get file path
        camelcase_method = underscoreToCamel(file_type.capitalize())
        name = getattr(self, "get" + camelcase_method + "Name")(meta_name, root, i)

        if name and (self.conf("meta_" + file_type) or self.conf("meta_" + file_type) is None):

            # Get file content
            content = getattr(self, "get" + camelcase_method)(movie_info=movie_info, data=group, i=i)
            if content:
                log.debug("Creating %s file: %s", (file_type, name))
                if os.path.isfile(content):
                    content = sp(content)
                    name = sp(name)

                    if not os.path.exists(os.path.dirname(name)):
                        os.makedirs(os.path.dirname(name))

                    shutil.copy2(content, name)
                    shutil.copyfile(content, name)

                    # Try and copy stats seperately
                    try:
                        shutil.copystat(content, name)
                    except:
                        pass
                else:
                    self.createFile(name, content)
                    group["renamed_files"].append(name)

                try:
                    os.chmod(sp(name), Env.getPermission("file"))
                except:
                    log.debug("Failed setting permissions for %s: %s", (name, traceback.format_exc()))
Пример #2
0
    def makeDir(self, path):
        path = sp(path)
        try:
            if not os.path.isdir(path):
                os.makedirs(path, Env.getPermission('folder'))
            return True
        except Exception as e:
            log.error('Unable to create folder "%s": %s', (path, e))

        return False
Пример #3
0
    def createFile(self, path, content, binary = False):
        path = sp(path)

        self.makeDir(os.path.dirname(path))

        if os.path.exists(path):
            log.debug('%s already exists, overwriting file with new version', path)

        write_type = 'w+' if not binary else 'w+b'

        # Stream file using response object
        if isinstance(content, requests.models.Response):

            # Write file to temp
            with open('%s.tmp' % path, write_type) as f:
                for chunk in content.iter_content(chunk_size = 1048576):
                    if chunk:  # filter out keep-alive new chunks
                        f.write(chunk)
                        f.flush()

            # Rename to destination
            os.rename('%s.tmp' % path, path)

        else:
            try:
                f = open(path, write_type)
                f.write(content)
                f.close()

                try:
                    os.chmod(path, Env.getPermission('file'))
                except:
                    log.error('Failed writing permission to file "%s": %s', (path, traceback.format_exc()))

            except:
                log.error('Unable to write file "%s": %s', (path, traceback.format_exc()))
                if os.path.isfile(path):
                    os.remove(path)
Пример #4
0
    def download(self, data=None, media=None, filedata=None):
        """ Send a torrent/nzb file to the downloader

        :param data: dict returned from provider
            Contains the release information
        :param media: media dict with information
            Used for creating the filename when possible
        :param filedata: downloaded torrent/nzb filedata
            The file gets downloaded in the searcher and send to this function
            This is done to have failed checking before using the downloader, so the downloader
            doesn't need to worry about that
        :return: boolean
            One faile returns false, but the downloaded should log his own errors
        """

        if not media:
            media = {}
        if not data:
            data = {}

        directory = self.conf("directory")

        # The folder needs to exist
        if not directory or not os.path.isdir(directory):
            log.error("No directory set for blackhole %s download.", data.get("protocol"))
        else:
            try:
                # Filedata can be empty, which probably means it a magnet link
                if not filedata or len(filedata) < 50:
                    try:
                        if data.get("protocol") == "torrent_magnet":
                            filedata = self.magnetToTorrent(data.get("url"))
                            data["protocol"] = "torrent"
                    except:
                        log.error("Failed download torrent via magnet url: %s", traceback.format_exc())

                    # If it's still empty, don't know what to do!
                    if not filedata or len(filedata) < 50:
                        log.error("No nzb/torrent available: %s", data.get("url"))
                        return False

                # Create filename with imdb id and other nice stuff
                file_name = self.createFileName(data, filedata, media)
                full_path = os.path.join(directory, file_name)

                # People want thinks nice and tidy, create a subdir
                if self.conf("create_subdir"):
                    try:
                        new_path = os.path.splitext(full_path)[0]
                        if not os.path.exists(new_path):
                            os.makedirs(new_path)
                            full_path = os.path.join(new_path, file_name)
                    except:
                        log.error("Couldnt create sub dir, reverting to old one: %s", full_path)

                try:

                    # Make sure the file doesn't exist yet, no need in overwriting it
                    if not os.path.isfile(full_path):
                        log.info("Downloading %s to %s.", (data.get("protocol"), full_path))
                        with open(full_path, "wb") as f:
                            f.write(filedata)
                        os.chmod(full_path, Env.getPermission("file"))
                        return self.downloadReturnId("")
                    else:
                        log.info("File %s already exists.", full_path)
                        return self.downloadReturnId("")

                except:
                    log.error("Failed to download to blackhole %s", traceback.format_exc())
                    pass

            except:
                log.info("Failed to download file %s: %s", (data.get("name"), traceback.format_exc()))
                return False

        return False