mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Search DuckDuckGo for a cover image
This commit is contained in:
25
lib/api_hooks.py
Normal file
25
lib/api_hooks.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import sys
|
||||||
|
import requests
|
||||||
|
sys.path.insert(1, 'lib/')
|
||||||
|
|
||||||
|
|
||||||
|
class DuckDuckGo:
|
||||||
|
"""duckduckgo related searching"""
|
||||||
|
def __init__(self):
|
||||||
|
self.url = "https://api.duckduckgo.com/?q="
|
||||||
|
|
||||||
|
def image_result(self, query):
|
||||||
|
"""
|
||||||
|
Returns json containing url to image
|
||||||
|
:param _key: &t=h_&iar=images&iax=images&ia=images&format=json&pretty=1
|
||||||
|
"""
|
||||||
|
_key = '&t=h_&iar=images&iax=images&ia=images&format=json&pretty=1'
|
||||||
|
try: query = query.string
|
||||||
|
except AttributeError: query = query
|
||||||
|
search_result = requests.get(self.url+query+_key)
|
||||||
|
if search_result.status_code == 200 and search_result.json()['Image'] != '':
|
||||||
|
image = requests.get(search_result.json()['Image'], stream=True)
|
||||||
|
image.raw.decode_content = True
|
||||||
|
return image.raw
|
||||||
|
else: return False
|
||||||
Reference in New Issue
Block a user