-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse-search
executable file
·34 lines (27 loc) · 1.38 KB
/
reverse-search
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
#! /usr/bin/env python3
'''
The above code is used to reverse search the image and return the result image page url.
It takes multiple argument and return the single page url.It support both remote url and local path of an image.
It searches one by one and open the reult url in the browser one by one
'''
import sys
import json
import requests
import subprocess
from concurrent.futures import ThreadPoolExecutor
image_paths = sys.argv[1:]
search_url = 'https://yandex.com/images/search'
def search_image(image_path):
if image_path.startswith("http"):
img_search_url = search_url + '?' + 'rpt=imageview&'+f'url={image_path}'
else:
files = {'upfile': ('blob', open(image_path, 'rb'), 'image/jpeg')}
params = {'rpt': 'imageview', 'format': 'json', 'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
response = requests.post(search_url, params=params, files=files)
query_string = json.loads(response.content)['blocks'][0]['params']['url']
img_search_url = search_url + '?' + query_string
subprocess.Popen(["nohup", "python3", "-c", f"import webbrowser; webbrowser.open(\"{img_search_url}\", new=2)"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
with ThreadPoolExecutor() as executor:
results = [executor.submit(search_image, image_path) for image_path in image_paths]
for f in results:
f.result()