Exemplo n.º 1
0
def unsplash_parse_resp(subject):
    """
    From Unsplash API, collect the top 4 images from results.
        :param subject: The subject to be used for the image search or type(None). If None, random photos are fetched.
        :rtype images: A list containing data on the fetched images.
        :except AttributeErrror: Occurs when resp fails to fetch images and enumerate cannot parse resp.
    """
    py_un = PyUnsplash(api_key=UNSPLASH_CLIENT_ID)
    images = []
    if subject is not None:
        resp = py_un.search("photos", query=subject, per_page=4)
    else:
        resp = py_un.photos(type_="random", count=4)
    # Gather data from resp object.
    try:
        for num, item in enumerate(resp.entries, 1):
            image_info = {
                "author_name": item.body["user"]["name"],
                "full_image": item.body["urls"]["full"],
                "image_id": item.id,
                "author_profile":
                f"{item.body['user']['links']['html']}?utm_source=Wallie&utm_medium=referral",
                "download_location": item.link_download_location,
            }
            images.append(image_info)
        return images
    except AttributeError as err:
        handle_err(
            f"Failed to parse unsplash resp object: {err}\nCheck that your API_KEYs are setup correctly."
        )
Exemplo n.º 2
0
def pixabay_parse_resp(subject):
    """
    From Pixabay API, collect the top 4 images from results.
        :param subject: The subject to be used for the image search or type(None). If None, random photos are fetched.
        :rtype images: A list containing data on the fetched images.
        :except AttributeErrror: Occurs when resp fails to fetch images and enumerate cannot parse resp.
    """
    pix = Image(PIXABAY_API_KEY)
    if subject is not None:
        resp = pix.search(q=subject, per_page=4, image_type="photo")
    else:
        # give more randomized image selections
        resp = pix.search(q=randomize_query(),
                          per_page=4,
                          image_type="photo",
                          page=randint(1, 10))
    images = []
    try:
        for num, item in enumerate(resp["hits"], 1):
            image_info = {
                "author_name":
                item["user"],
                "full_image":
                item["largeImageURL"],
                "image_id":
                item["id"],
                "author_profile":
                f"https://pixabay.com/en/users/{item['user_id']}",
            }
            images.append(image_info)
        return images
    except AttributeError as err:
        handle_err(
            f"Failed to parse pixabay resp object: {err}\nCheck that your API_KEYs are setup correctly."
        )
Exemplo n.º 3
0
def pexels_parse_resp(subject):
    """
    From Pexels API resp, collect the top 4 images from results.
        :param subject: The subject to be used for the image search or type(None). If None, random photos are fetched.
        :rtype images: A list containing data on the fetched images.
        :except AttributeErrror: Occurs when resp fails to fetch images and enumerate cannot parse resp.
    """
    py_pexel = PyPexels(api_key=PEXELS_API_KEY)
    if subject is not None:
        resp = py_pexel.search(query=subject, per_page=4)
    else:
        resp = py_pexel.random(per_page=4)
    images = []
    try:
        for num, item in enumerate(resp.entries, 1):
            image_info = {
                "author_name": item.photographer,
                "full_image": item.src["original"],
                "image_id": item.id,
                "author_profile": item.photographer_url,
            }
            images.append(image_info)
        return images
    except AttributeError as err:
        handle_err(
            f"Failed to parse pexels resp object: {err}\nCheck that your API_KEYs are setup correctly."
        )
Exemplo n.º 4
0
def solve_image(img):
    Tile.tiles = [Tile(img, x, y) for x in range(Consts.TILES_AMOUNT) for y in range(Consts.TILES_AMOUNT)]
    sol = Image.new('RGB', (Consts.TILE_SIZE * Consts.TILES_AMOUNT, Consts.TILE_SIZE * Consts.TILES_AMOUNT))

    t = tu.go_to_top_left(Tile.tiles[0])
    tu.fill(t, 'right')
    utils.handle_err(len(tu.get_row(t)), 0, True)

    for x, tile_first_row in enumerate(tu.get_row(t)):
        tu.fill(tile_first_row, 'down')
        utils.handle_err(len(tu.get_col(tile_first_row)), x, False)

        for y, tile in enumerate(tu.get_col(tile_first_row)):
            utils.paste_tile_on_image(sol, tile, x, y)

    return sol
Exemplo n.º 5
0
def random(api):
    """
    Sets the Wallpaper to a random image.
        :param api: The api option to fetch images from.
    """
    if api == "unsplash":
        images = unsplash_parse_resp(None)
        user_choice = present_images(images)
        unsplash_trigger_download(user_choice["download_location"])
    elif api == "pexels":
        images = pexels_parse_resp(None)
        user_choice = present_images(images)
    elif api == "pixabay":
        images = pixabay_parse_resp(None)
        user_choice = present_images(images)
    else:
        handle_err("Invalid API option.")
    apply_wallpaper(user_choice, "random")
Exemplo n.º 6
0
def set(api, subject):
    """Sets the Wallpaper of the device.
        :param api: The api to fetch images from.
        :param subject: The subject of the image to be fetched.
    """
    click.secho(f"Searching {api} for {subject} images...", fg="bright_yellow")
    if api == "unsplash":
        images = unsplash_parse_resp(subject)
        user_choice = present_images(images)
        unsplash_trigger_download(user_choice["download_location"])
    elif api == "pexels":
        images = pexels_parse_resp(subject)
        user_choice = present_images(images)
    elif api == "pixabay":
        images = pixabay_parse_resp(subject)
        user_choice = present_images(images)
    else:
        handle_err("Invalid API option.")
    apply_wallpaper(user_choice, subject)
Exemplo n.º 7
0
def unsplash_trigger_download(download_location):
    """
    Trigger a download event to Unsplash API. Required by Unsplash API.
    See: https://help.unsplash.com/api-guidelines/more-on-each-guideline/guideline-triggering-a-download
        :param download_location: A string url to trigger download to Unsplash API.
    """
    resp = requests.get(
        download_location,
        headers={
            "Accept-Version": "v1",
            "Authorization": f"Client-ID {UNSPLASH_CLIENT_ID}",
        },
    )
    if (resp.status_code) == (requests.codes.ok):
        pass
    else:
        # Raise the error if status code is not ok ie evaluates to True.
        handle_err(
            f"Unsplash trigger download failed: {resp.raise_for_status()}")