Esempio n. 1
0
    def video_download_by_url_origin(
        self, url: str
    ) -> bytes:
        """
        Download video using URL

        Parameters
        ----------
        url: str
            URL for a media

        Returns
        -------
        bytes
            Bytes for the file downloaded
        """
        response = requests.get(url, stream=True)
        response.raise_for_status()
        content_length = int(response.headers.get("Content-Length"))
        file_length = len(response.content)
        if content_length != file_length:
            raise VideoNotDownload(
                f'Broken file from url "{url}" (Content-length={content_length}, but file length={file_length})'
            )
        return response.content
Esempio n. 2
0
    def video_download_by_url(
        self, url: str, filename: str = "", folder: Path = ""
    ) -> Path:
        """
        Download video using URL

        Parameters
        ----------
        url: str
            URL for a media
        filename: str, optional
            Filename for the media
        folder: Path, optional
            Directory in which you want to download the video, default is "" and will download the files to working
                directory

        Returns
        -------
        Path
            Path for the file downloaded
        """
        fname = urlparse(url).path.rsplit("/", 1)[1]
        filename = "%s.%s" % (filename, fname.rsplit(".", 1)[1]) if filename else fname
        path = Path(folder) / filename
        response = requests.get(url, stream=True)
        response.raise_for_status()
        try:
            content_length = int(response.headers.get("Content-Length"))
        except TypeError:
            print("The program detected an mis-formatted link, and hence can't download it.")
            print("The problem is located wherever the url is passed into the 'video_download_by_url()' or the 'clip_download_by_url()' function.")
            print("Convert the url input into a formattable link.")
            print("Use this code: url=self.cl.media_info(self.cl.media_pk_from_url('insert the url (or a variable with the url) here').video_url)")
            print("You can remove the 'self' from the code above if needed.")
            raise Exception("The program detected an mis-formatted link.")
        file_length = len(response.content)
        if content_length != file_length:
            raise VideoNotDownload(
                f'Broken file "{path}" (Content-length={content_length}, but file length={file_length})'
            )
        with open(path, "wb") as f:
            f.write(response.content)
            f.close()
        return path.resolve()
Esempio n. 3
0
    def video_download_by_url(self,
                              url: str,
                              filename: str = "",
                              folder: Path = "") -> Path:
        """
        Download video using media pk

        Parameters
        ----------
        url: str
            URL for a media
        filename: str, optional
            Filename for the media
        folder: Path, optional
            Directory in which you want to download the album, default is "" and will download the files to working
                directory

        Returns
        -------
        Path
            Path for the file downloaded
        """
        fname = urlparse(url).path.rsplit("/", 1)[1]
        filename = "%s.%s" % (filename,
                              fname.rsplit(".", 1)[1]) if filename else fname
        path = Path(folder) / filename
        response = requests.get(url, stream=True)
        response.raise_for_status()
        content_length = int(response.headers.get("Content-Length"))
        file_length = len(response.content)
        if content_length != file_length:
            raise VideoNotDownload(
                'Broken file "%s" (Content-length=%s, but file length=%s)' %
                (path, content_length, file_length))
        with open(path, "wb") as f:
            f.write(response.content)
            f.close()
        return path.resolve()