-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathharem.py
134 lines (121 loc) · 4.65 KB
/
harem.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright (C) 2020-2021 by DevsExpo@Github, < https://github.com/DevsExpo >.
#
# This file is part of < https://github.com/DevsExpo/FridayUserBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/DevsExpo/blob/master/LICENSE >
#
# All rights reserved.
import aiohttp
from pyrogram import filters
from xtraplugins.dB.harem_heckdb import add_chat, is_chat_in_db, rm_chat
from main_startup.config_var import Config
from main_startup.core.decorators import friday_on_cmd, listen
from main_startup.helper_func.basic_helpers import (
edit_or_reply,
edit_or_send_as_file,
get_text,
get_user,
iter_chats,
run_in_exc
)
from main_startup.helper_func.logger_s import LogIt
from plugins import devs_id
import io
import os
import asyncio
import re
import urllib
import shutil
from re import findall
import requests
from bs4 import BeautifulSoup
u_ = """Mozilla/5.0 (Linux; Android 9; SM-G960F Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.157 Mobile Safari/537.36"""
headers_ = [("User-agent", u_)]
async def ParseSauce(googleurl):
"""Parse/Scrape the HTML code for the info we want."""
async with aiohttp.ClientSession(headers=headers_) as session:
async with session.get(googleurl) as resp:
source = await resp.read()
soup = BeautifulSoup(source, "html.parser")
results = {"similar_images": "", "best_guess": ""}
try:
for similar_image in soup.findAll("input", {"class": "gLFyf"}):
url = "https://www.google.com/search?tbm=isch&q=" + urllib.parse.quote_plus(
similar_image.get("value")
)
results["similar_images"] = url
except BaseException:
pass
for best_guess in soup.findAll("div", attrs={"class": "r5a77d"}):
results["best_guess"] = best_guess.get_text()
return results
@friday_on_cmd(
["ahc"],
cmd_help={
"help": "Add A Chat To Harem List.",
"example": "{ch}ahc (current chat will be taken)",
},
)
async def add_harem_hc(client, message):
pablo = await edit_or_reply(message, "`Processing..`")
if await is_chat_in_db(int(message.chat.id)):
await pablo.edit("`This Chat is Already In My DB`")
return
await add_chat(int(message.chat.id))
await pablo.edit("`Successfully Added Chat To Harem Watch.`")
@friday_on_cmd(
["rmhc"],
group_only=True,
cmd_help={"help": "Remove Chat From Harem List.", "example": "{ch}rmhc"},
)
async def remove_nsfw(client, message):
pablo = await edit_or_reply(message, "`Processing..`")
if not await is_chat_in_db(int(message.chat.id)):
await pablo.edit("`This Chat is Not in dB.`")
return
await rm_chat(int(message.chat.id))
await pablo.edit("`Successfully Removed Chat From Harem Watch.`")
async def is_harem_enabled(f, client, message):
if Config.ENABLE_WAIFU_FOR_ALL_CHATS:
return bool(True)
if await is_chat_in_db(int(message.chat.id)):
return bool(True)
else:
return bool(False)
async def harem_event(f, client, message):
if not message:
return bool(False)
if not message.photo:
return bool(False)
if not message.caption:
return bool(False)
if "add" in message.caption.lower():
return bool(True)
return bool(False)
@run_in_exc
def get_data(img):
searchUrl = "https://www.google.com/searchbyimage/upload"
file_img = {"encoded_image": (img, open(img, "rb")), "image_content": ""}
response = requests.post(searchUrl, files=file_img, allow_redirects=False)
if os.path.exists(img):
os.remove(img)
if response.status_code == 400:
return logging.info("(Waifu Catch Failed) - [Invalid Response]")
return response.headers["Location"]
harem_event = filters.create(func=harem_event, name="harem_event")
is_harem_enabled = filters.create(func=is_harem_enabled, name="is_harem_enabled")
@listen(filters.user([int(792028928)]) & ~filters.edited & is_harem_enabled & harem_event & filters.group)
async def harem_catcher(client, message):
img = await message.download()
fetchUrl = await get_data(img)
match = await ParseSauce(fetchUrl + "&preferences?hl=en&fg=1#languages")
guessp = match["best_guess"]
if not guessp:
return logging.info("(Waifu Catch Failed.) \nERROR : 404: Waifu Not Found.")
guess = guessp.replace("Results for", "")
kek = await message.reply_text(f"/protecc {guess}")
await asyncio.sleep(5)
await kek.delete()
log = LogIt(message)
msg_to_log = f"[{guess}] - New Waifu Appeared - ({message.chat.title}) - Sucessfully Tried To Protecc"
await log.log_msg(client, msg_to_log)