Example #1
0
async def img_sampler(event):
    if event.fwd_from:
        return
    await edit_or_reply(event, "`Processing...`")
    reply = await event.get_reply_message()
    if event.pattern_match.group(2):
        queryo = event.pattern_match.group(2)
    elif reply:
        queryo = reply.message
    else:
        await edit_or_reply(
            event,
            "`um, mind mentioning what I actually need to search for ;_;`")
        return
    query = queryo + "hd wallpaper"
    lim = findall(r"lim=\d+", query)
    # lim = event.pattern_match.group(1)
    try:
        lim = lim[0]
        lim = lim.replace("lim=", "")
        query = query.replace("lim=" + lim[0], "")
    except IndexError:
        lim = 10
    response = googleimagesdownload()

    # creating list of arguments
    arguments = {
        "keywords": query,
        "limit": lim,
        "format": "jpg",
        "no_directory": "no_directory",
    }

    # passing the arguments to the function
    paths = response.download(arguments)
    lst = paths[0][query]
    await event.client.send_file(
        await event.client.get_input_entity(event.chat_id), lst)
    shutil.rmtree(os.path.dirname(os.path.abspath(lst[0])))
    await event.delete()
Example #2
0
async def okgoogle(img):
    if img.fwd_from:
        return
    """ For .reverse command, Google search images and stickers. """
    if os.path.isfile("okgoogle.png"):
        os.remove("okgoogle.png")
    event = img
    message = await img.get_reply_message()
    if message and message.media:
        photo = io.BytesIO()
        await bot.download_media(message, photo)
    else:
        await img.edit("`Reply to photo or sticker nigger.`")
        return

    if photo:
        await img.edit("`Processing...`")
        try:
            image = Image.open(photo)
        except OSError:
            await img.edit("`Unsupported sexuality, most likely.`")
            return
        name = "okgoogle.png"
        image.save(name, "PNG")
        image.close()
        # https://stackoverflow.com/questions/23270175/google-reverse-image-search-using-post-request#28792943
        searchUrl = "https://www.google.com/searchbyimage/upload"
        multipart = {
            "encoded_image": (name, open(name, "rb")),
            "image_content": ""
        }
        response = requests.post(searchUrl,
                                 files=multipart,
                                 allow_redirects=False)
        fetchUrl = response.headers["Location"]

        if response != 400:
            await img.edit("`Image successfully uploaded to Google. Maybe.`"
                           "\n`Parsing source now. Maybe.`")
        else:
            await img.edit("`Google told me to f**k off.`")
            return

        os.remove(name)
        match = await ParseSauce(fetchUrl +
                                 "&preferences?hl=en&fg=1#languages")
        guess = match["best_guess"]
        imgspage = match["similar_images"]

        if guess and imgspage:
            await img.edit(
                f"[{guess}]({fetchUrl})\n\n`Looking for this Image...`")
        else:
            await img.edit("`Can't find this piece of shit.`")
            return
        lim = findall(r"lim=\d+", guess)
        try:
            lim = lim[0]
            lim = lim.replace("lim=", "")
            guess = guess.replace("lim=" + lim[0], "")
        except IndexError:
            lim = 5
        response = googleimagesdownload()
        logger.info(guess)
        arguments = {
            "keywords": guess,
            "silent_mode": True,
            "limit": lim,
            "format": "jpg",
            "no_directory": "no_directory",
        }
        paths = response.download(arguments)
        lst = paths[0][guess]
        await event.edit(
            f"[{guess}]({fetchUrl})\n\n[Visually similar images]({imgspage})")
        await event.client.send_file(
            await event.client.get_input_entity(event.chat_id), lst)
        shutil.rmtree(os.path.dirname(os.path.abspath(lst[0])))