2021-04-14 23:23:56 +03:00
|
|
|
import logging
|
2021-04-15 12:30:26 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-04-14 23:23:56 +03:00
|
|
|
import requests
|
2021-04-15 12:30:26 +03:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2021-04-14 23:23:56 +03:00
|
|
|
|
|
|
|
class Iqdb:
|
2021-04-15 12:30:26 +03:00
|
|
|
@staticmethod
|
|
|
|
def search(file: str) -> Optional[str]:
|
2021-04-14 23:23:56 +03:00
|
|
|
logging.info('Searching %s', file)
|
|
|
|
files = {'file': open(file, 'rb')}
|
|
|
|
resp = requests.post('https://iqdb.org/', files=files, timeout=10)
|
|
|
|
doc = BeautifulSoup(resp.text, 'html.parser')
|
|
|
|
for tag in doc.select(".image a"):
|
|
|
|
url = tag.get("href")
|
|
|
|
if "danbooru.donmai.us/posts" in url:
|
|
|
|
if url.startswith("//"):
|
|
|
|
url = "https:" + url
|
|
|
|
return url
|
|
|
|
return None
|