-
Notifications
You must be signed in to change notification settings - Fork 0
/
gris.py
executable file
·54 lines (42 loc) · 1.86 KB
/
gris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
from bs4 import BeautifulSoup
BASE_URL = "https://www.google.com"
USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11\
(KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
RES_OPTIONS = ['All sizes', 'Middle', 'Large']
def search_by_url(image_url, timeout=None):
""" Creates a SearchResults object for the specified image URL """
return SearchResults(image_url=image_url, timeout=timeout)
class SearchResults:
""" Contains the search results of the GRIS query """
def __init__(self, image_url=None, timeout=None):
self.best_guess = None
self.timeout = 10 if not timeout else timeout
self.res_urls = dict()
for i in RES_OPTIONS:
self.res_urls[i] = None
if image_url:
self.__get_results_for_image(image_url=image_url)
else:
raise ValueError("Invalid image URL")
def __get_results_for_image(self, image_url=None):
headers = {'User-Agent': USER_AGENT}
params = {'image_url': image_url}
r = requests.get(BASE_URL + "/searchbyimage", headers=headers, params=params, timeout=self.timeout)
soup = BeautifulSoup(r.text)
for link in soup.find(id="topstuff").findAll("a"):
if link.text in RES_OPTIONS:
self.res_urls[link.text] = link.attrs['href']
elif link.text:
self.best_guess = link.text
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: %s <image_url>" % sys.argv[0])
sys.exit()
urls_string = ""
results = search_by_url(sys.argv[1])
for k, v in results.res_urls.iteritems():
urls_string += "\t%s: %s\n" % (k, v) if v else ""
print("Tags: %s" % results.best_guess)
print("URLs:\n" + urls_string if urls_string else "")