示例#1
0
    def download(
        self,
        title: str,
        srt: bool = True,
        output_path: Optional[str] = None,
        filename_prefix: Optional[str] = None,
    ) -> str:
        """Write the media stream to disk.

        :param title:
            Output filename (stem only) for writing media file.
            If one is not specified, the default filename is used.
        :type title: str
        :param srt:
            Set to True to download srt, false to download xml. Defaults to True.
        :type srt bool
        :param output_path:
            (optional) Output path for writing media file. If one is not
            specified, defaults to the current working directory.
        :type output_path: str or None
        :param filename_prefix:
            (optional) A string that will be prepended to the filename.
            For example a number in a playlist or the name of a series.
            If one is not specified, nothing will be prepended
            This is separate from filename so you can use the default
            filename but still add a prefix.
        :type filename_prefix: str or None

        :rtype: str
        """
        if title.endswith(".srt") or title.endswith(".xml"):
            filename = ".".join(title.split(".")[:-1])
        else:
            filename = title

        if filename_prefix:
            filename = f"{safe_filename(filename_prefix)}{filename}"

        filename = safe_filename(filename)

        filename += f" ({self.code})"

        if srt:
            filename += ".srt"
        else:
            filename += ".xml"

        file_path = os.path.join(target_directory(output_path), filename)

        with open(file_path, "w", encoding="utf-8") as file_handle:
            if srt:
                file_handle.write(self.generate_srt_captions())
            else:
                file_handle.write(self.xml_captions)

        return file_path
 def get_file_path(
     self,
     filename: Optional[str] = None,
     output_path: Optional[str] = None,
     filename_prefix: Optional[str] = None,
 ) -> str:
     if not filename:
         filename = self.default_filename
     if filename_prefix:
         filename = f"{filename_prefix}{filename}"
     return os.path.join(target_directory(output_path), filename)
示例#3
0
 def get_file_path(
     self,
     filename: Optional[str],
     output_path: Optional[str],
     filename_prefix: Optional[str] = None,
 ) -> str:
     if filename:
         filename = f"{safe_filename(filename)}.{self.subtype}"
     else:
         filename = self.default_filename
     if filename_prefix:
         filename = f"{safe_filename(filename_prefix)}{filename}"
     return os.path.join(target_directory(output_path), filename)
示例#4
0
 def get_file_path(
     self,
     filename: Optional[str],
     output_path: Optional[str],
     filename_prefix=None,
 ) -> str:
     if filename:
         filename = "{}.{}".format(safe_filename(filename), self.subtype)
     else:
         filename = self.default_filename
     if filename_prefix:
         filename = "{}{}".format(safe_filename(filename_prefix), filename)
     return os.path.join(target_directory(output_path), filename)
示例#5
0
def test_target_directory_with_no_path(_, makedirs):  # noqa: PT019
    assert target_directory() == "/cwd"
    makedirs.assert_called()
示例#6
0
def test_target_directory_with_absolute_path(_, makedirs):  # noqa: PT019
    assert target_directory("/test") == "/test"
    makedirs.assert_called()
示例#7
0
def test_target_directory_with_relative_path(_, __, makedirs):  # noqa: PT019
    assert target_directory("test") == os.path.join("/cwd", "test")
    makedirs.assert_called()
示例#8
0
文件: streams.py 项目: Twixes/pytube3
    def download(
        self,
        output_path: Optional[str] = None,
        filename: Optional[str] = None,
        filename_prefix: Optional[str] = None,
        skip_existing: bool = True,
    ) -> str:
        """Write the media stream to disk.

        :param output_path:
            (optional) Output path for writing media file. If one is not
            specified, defaults to the current working directory.
        :type output_path: str or None
        :param filename:
            (optional) Output filename (stem only) for writing media file.
            If one is not specified, the default filename is used.
        :type filename: str or None
        :param filename_prefix:
            (optional) A string that will be prepended to the filename.
            For example a number in a playlist or the name of a series.
            If one is not specified, nothing will be prepended
            This is separate from filename so you can use the default
            filename but still add a prefix.
        :type filename_prefix: str or None
        :param skip_existing:
            (optional) skip existing files, defaults to True
        :type skip_existing: bool
        :returns:
            Path to the saved video
        :rtype: str

        """
        if filename:
            filename = f"{safe_filename(filename)}.{self.subtype}"
        else:
            filename = self.default_filename

        if filename_prefix:
            filename = f"{safe_filename(filename_prefix)}{filename}"

        file_path = os.path.join(target_directory(output_path), filename)

        if (skip_existing and os.path.isfile(file_path)
                and os.path.getsize(file_path) == self.filesize):
            # likely the same file, so skip it
            logger.debug("file %s already exists, skipping", file_path)
            return file_path

        bytes_remaining = self.filesize
        logger.debug(
            "downloading (%s total bytes) file to %s",
            self.filesize,
            file_path,
        )

        with open(file_path, "wb") as fh:
            for chunk in request.stream(self.url):
                # reduce the (bytes) remainder by the length of the chunk.
                bytes_remaining -= len(chunk)
                # send to the on_progress callback.
                self.on_progress(chunk, fh, bytes_remaining)
        self.on_complete(fh)
        return file_path
示例#9
0
def test_target_directory_with_relative_path(_, __, makedirs):
    assert target_directory("test") == "/cwd/test"
    makedirs.assert_called()