fix search_img_ddg

This commit is contained in:
arman-hk 2024-04-14 19:03:23 +03:30
parent 054cd8a62c
commit 5c615090a1

View File

@ -42,31 +42,37 @@ def search_images_bing(key, term, min_sz=128, max_images=150):
# - # -
def search_images_ddg(key,max_n=200): def search_images_ddg(key, max_n=200):
"""Search for 'key' with DuckDuckGo and return a unique urls of 'max_n' images """
(Adopted from https://github.com/deepanprabhu/duckduckgo-images-api) Search for 'key' with DuckDuckGo and return unique URLs of up to 'max_n' images.
""" If an error occurs or token parsing fails, prints an error message and returns collected URLs.
url = 'https://duckduckgo.com/' """
params = {'q':key} url = 'https://duckduckgo.com/'
res = requests.post(url,data=params) params = {'q': key}
searchObj = re.search(r'vqd=([\d-]+)\&',res.text) try:
if not searchObj: print('Token Parsing Failed !'); return res = requests.post(url, data=params)
requestUrl = url + 'i.js' searchObj = re.search(r'vqd=([\d-]+)&', res.text)
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0'} if not searchObj:
params = (('l','us-en'),('o','json'),('q',key),('vqd',searchObj.group(1)),('f',',,,'),('p','1'),('v7exp','a')) print('Token Parsing Failed! Unable to retrieve search token.')
urls = [] return L(set()) # return an empty list-like object
while True: requestUrl = url + 'i.js'
try: headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0'}
res = requests.get(requestUrl,headers=headers,params=params) params = (('l', 'us-en'), ('o', 'json'), ('q', key), ('vqd', searchObj.group(1)), ('f', ',,,'), ('p', '1'), ('v7exp', 'a'))
data = json.loads(res.text) urls = []
for obj in data['results']: while max_n > 0:
urls.append(obj['image']) res = requests.get(requestUrl, headers=headers, params=params)
max_n = max_n - 1 data = json.loads(res.text)
if max_n < 1: return L(set(urls)) # dedupe for obj in data['results']:
if 'next' not in data: return L(set(urls)) urls.append(obj['image'])
requestUrl = url + data['next'] max_n -= 1
except: if max_n < 1:
pass return L(set(urls)) # dedupe and return
if 'next' not in data:
return L(set(urls))
requestUrl = url + data['next']
except Exception as e:
print(f"An error occurred: {e}")
return L(set(urls)) # return whatever has been gathered so far
def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)): def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):