예제 #1
0
def download(url, path, file_name=""):
    """Download the images from a RedditBooru gallery.

    Args:
        url: A url to a RedditBooru gallery.
        path: Path to the folder where the image should be saved.
        file_name: File name to use when saving the images.
            A number will be appended to the end of the name for
            each image in the album.
            If file_name is an empty string, the files will keep
            the names they have on the server.
    """
    response = requests.get(url)
    if not response.ok:
        raise DownloadError("Unable to download redditbooru gallery {}".format(url))
    soup = BeautifulSoup(response.content, "html.parser")
    images = soup.find_all("img")
    if len(images) < 1:
        raise DownloadError("Empty redditbooru gallery {}".format(url))
    if file_name == "":
        for image in images:
            direct.download(image["src"], path, file_name)
    else:
        for i, image in enumerate(images):
            direct.download(image["src"], path, "{}.{}".format(file_name, i + 1))
예제 #2
0
def download(url, path, file_name=""):
    """Download the image from a tumblr post.

    Args:
        url: A url to a tumblr post.
        path: Path to the folder where the image should be saved.
        file_name: File name to use when saving the image.
            If file_name is an empty string, name of the downloaded file will be used.
    """
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    post = soup(class_="media")
    images = post[0]("img", class_=False)
    direct.download(images[0]["src"], path, file_name)
예제 #3
0
def download(url, path, file_name=""):
    """Download the image from imgur link.

    Args:
        url: A url to an imgur image.
        path: Path to the folder where the image should be saved.
        file_name: File name to use when saving the image.
            If file_name is an empty string, name of the downloaded file will be used.
    """
    download_name = url.split('/')[-1]
    # The file extension doesn't have to be a .jpg,
    # Imgur returns the image as long as it is requested with a known file extension.
    # download function will correct the extension based on the headers.
    direct.download("https://i.imgur.com/{}.jpg".format(download_name), path, file_name)
예제 #4
0
def download(url, path, file_name=""):
    """Download an imgur gifv video.

    Args:
        url: A url to an imgur gifv.
        path: Path to the folder where the image should be saved.
        file_name: File name to use when saving the image.
            If file_name is an empty string, name of the downloaded file will be used.
    """
    # From an url "http://i.imgur.com/abcdef.gifv", first extract "abcdef.gifv" then
    # delete the extension to get "abcdef"
    download_name = url.split('/')[-1][:-5]
    if PREFER_WEBM:
        extension = "webm"
    else:
        extension = "mp4"
    direct.download("https://i.imgur.com/{}.{}".format(download_name, extension), path, file_name)
예제 #5
0
def download(url, path, file_name=""):
    """Download the file at url to path if it doesn't exist.

    Correct file extension will be given based on the response header.
    Byte mode is used for Windows.

    Args:
        url: A url to the file. Should start with http:// or https://.
        path: Path to the folder where the file should be saved.
        file_name: The file name to use when saving the file.
            file_name is an empty string, then name of the downloaded file will be used.
    """
    escaped_url = url.translate(_URL_ESCAPE)
    request = requests.get(_DEVIANTART_API_URL.format(escaped_url))
    if not request.ok:
        raise DownloadError("Failed while getting data from deviantart API for {}".format(url))
    direct.download(request.json()["url"], path, file_name)
예제 #6
0
def download(url, path, file_name=""):
    """Download the file at url to path if it doesn't exist.

    Correct file extension will be given based on the response header.
    Byte mode is used for Windows.

    Args:
        url: A url to the file. Should start with http:// or https://.
        path: Path to the folder where the file should be saved.
        file_name: The file name to use when saving the file.
            file_name is an empty string, then name of the downloaded file will be used.
    """
    request = requests.get(url)
    if not request.ok:
        raise DownloadError("Failed while getting data from Twitter for {}".format(url))
    soup = BeautifulSoup(request.content, "html.parser")
    # Twitter stores image link in meta property.
    image = soup.head.find("meta", property=_TWITTER_SEARCH_PROPERTY)["content"]
    # Ensure that we extracted the correct link
    if not _TWITTER_IMAGE_MATCH(image):
        raise DownloadError("Unable to locate image in Twitter post {}".format(url))
    direct.download(image, path, file_name)