-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarry.py
70 lines (64 loc) · 2.71 KB
/
marry.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import asyncio # Thanks To Jisan Catuserbot Admin
import os # https://t.me/sohailkhan_anime don't forget credits.
from datetime import datetime # Thanks TheFallenAngel And Sandeep Catuserbot Admim To Teach.
import requests
from bs4 import BeautifulSoup
def progress(current, total):
logger.info(
"Downloaded {} of {}\nCompleted {}".format(
current, total, (current / total) * 100
)
)
@bot.on(admin_cmd(pattern="m"))
async def _(event):
if event.fwd_from:
return
start = datetime.now()
BASE_URL = "http://www.google.com"
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
previous_message_text = previous_message.message
if previous_message.media:
downloaded_file_name = await borg.download_media(
previous_message, Config.TMP_DOWNLOAD_DIRECTORY
)
SEARCH_URL = "{}/searchbyimage/upload".format(BASE_URL)
multipart = {
"encoded_image": (
downloaded_file_name,
open(downloaded_file_name, "rb"),
),
"image_content": "",
}
# https://stackoverflow.com/a/28792943/4723940
google_rs_response = requests.post(
SEARCH_URL, files=multipart, allow_redirects=False
)
the_location = google_rs_response.headers.get("Location")
os.remove(downloaded_file_name)
else:
previous_message_text = previous_message.message
SEARCH_URL = "{}/searchbyimage?image_url={}"
request_url = SEARCH_URL.format(BASE_URL, previous_message_text)
google_rs_response = requests.get(request_url, allow_redirects=False)
the_location = google_rs_response.headers.get("Location")
await event.edit(".")
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
}
response = requests.get(the_location, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
# document.getElementsByClassName("r5a77d"): PRS
prs_div = soup.find_all("div", {"class": "r5a77d"})[0]
prs_anchor_element = prs_div.find("a")
prs_url = BASE_URL + prs_anchor_element.get("href")
prs_text = prs_anchor_element.text
# document.getElementById("jHnbRc")
img_size_div = soup.find(id="jHnbRc")
img_size = img_size_div.find_all("div")
end = datetime.now()
ms = (end - start).seconds
OUTPUT_STR = """/marry {prs_text}""".format(
**locals()
)
await event.edit(OUTPUT_STR, parse_mode="HTML", link_preview=False)