arrrlo / Google-Images-Search

[PYTHON] Search for image using Google Custom Search API and resize & crop afterwards
MIT License
176 stars 34 forks source link

Search parameters not being updated #159

Closed notchum closed 1 year ago

notchum commented 1 year ago

Certain queries in the search parameters that I pass to GIS cause any queries after to have the same search parameters.

Reproducible Code

import os
from dotenv import load_dotenv
from google_images_search import GoogleImagesSearch

load_dotenv()

google_key = os.environ['GOOGLE_KEY']
google_search_id = os.environ['GOOGLE_SEARCH_ID']
gis = GoogleImagesSearch(google_key, google_search_id)

characters = {
    "Eren Jaeger": "Shingeki no Kyojin",
    "Megumin": "Kono Subarashii Sekai ni Shukufuku wo!",
    "Yukionna": "Inochi Taimanin Asagi",
    "Kaede Saitou": "Encouragement of Climb",
    "Astraea": "Sora no Otoshimono",    #!!! suspect
    "Mikasa Ackermann": "Shingeki no Kyojin",
    "Aqua": "Kono Subarashii Sekai ni Shukufuku wo!",
}

for name, series_name in characters.items():
    _search_params = {
        'q': f"{name} {series_name}",
        'num': 1,
        'fileType': 'jpg|png'
    }

    gis.search(search_params=_search_params)
    for image in gis.results():
        print(f"Retrieved [{image.url}] with query [{_search_params['q']}]")

In the above code, once _search_params['q'] is set to the "suspect" character/show combination string, any GIS searches that occur afterward will just return the same image URL every time.

Logs

Retrieved [https://i.pinimg.com/originals/9b/ce/7c/9bce7c1ab55253c882fe04e767d57717.jpg] with query [Eren Jaeger Shingeki no Kyojin]
Retrieved [https://static.wikia.nocookie.net/konosuba/images/3/3f/Megumin-anime.png/revision/latest/top-crop/width/360/height/360?cb=20180328143334] with query [Megumin Kono Subarashii Sekai ni Shukufuku wo!]
Retrieved [https://static.wikia.nocookie.net/taimanin/images/7/7e/Yukionna_Inochi.png/revision/latest?cb=20201207171637] with query [Yukionna Inochi Taimanin Asagi]
Retrieved [https://64.media.tumblr.com/bcf4abab113b2e5cd6dd35b1bc391f9d/tumblr_nd9zkrQ4EK1s2s76zo1_500.gifv] with query [Kaede Saitou Encouragement of Climb]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Astraea Sora no Otoshimono]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Mikasa Ackermann Shingeki no Kyojin]
### The last two lines show the same URL - but with a different query

And if I shift the characters keys around so that the suspect character/show combination is earlier in the dict, the same thing happens:

Retrieved [https://i.pinimg.com/originals/9b/ce/7c/9bce7c1ab55253c882fe04e767d57717.jpg] with query [Eren Jaeger Shingeki no Kyojin]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Astraea Sora no Otoshimono]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Megumin Kono Subarashii Sekai ni Shukufuku wo!]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Yukionna Inochi Taimanin Asagi]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Kaede Saitou Encouragement of Climb]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Mikasa Ackermann Shingeki no Kyojin]
Retrieved [https://static.zerochan.net/Astraea.full.422518.jpg] with query [Aqua Kono Subarashii Sekai ni Shukufuku wo!]
### Most of these lines show the same URL - different queries each time

I have messed around with the timing of calling gis.search() with no avail. With a breakpoint, I can see that my local _search_params is being updated each time with the new query string (the logs above also prove that) - even after the suspect string. But at the same time, gis._search_params doesn't get updated once the suspect string is searched.

Any help would be highly appreciated!

arrrlo commented 1 year ago

Hi @notchum

Well caught! Data reset between searches wasn't working as it should. This is fixed now, new version is live.

Cheers!