forked from TeamUltroid/UltroidAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.py
95 lines (84 loc) · 2.93 KB
/
images.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
# Ultroid - UserBot
# Copyright (C) 2020 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available
• `{i}freepik <query> ; limit`
search images on freepik.
• `{i}shutter <query> ; limit`
search images on shutterstock.
"""
import os
from random import shuffle
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
from telethon.errors.rpcerrorlist import WebpageCurlFailedError
from . import *
@ultroid_cmd(pattern="freepik ?(.*)")
async def fnew_pik(event):
match = event.pattern_match.group(1)
limit = 5
if not match:
return await event.eor("`Give Something to Search!`")
event = await event.eor("`...`")
if " ; " in match:
_ = match.split(" ; ", maxsplit=1)
match = _[0]
limit = int(_[1])
match = match.replace(" ", "%20")
content = urlopen(
f"https://www.freepik.com/search?format=search&page=1&query={match}"
)
reso = bs(content, "html.parser", from_encoding="utf-8")
con = reso.find_all("img", src=re.compile("img.freepik.com"))
if not con:
return await event.eor("No Results Found!")
shuffle(con)
lml = [a["src"] for a in con[:limit]]
try:
await event.client.send_message(
event.chat_id, f"Uploaded {len(lml)} Images!", file=lml
)
except WebpageCurlFailedError:
NaN = []
for _ in lml:
NaN.append(await download_file(_, check_filename("freepik.png")))
await event.client.send_message(
event.chat_id, f"Uploaded {len(NaN)} Images!", file=NaN
)
[os.remove(a) for a in NaN]
await event.delete()
@ultroid_cmd(pattern="shutter ?(.*)")
async def snew_pik(event):
match = event.pattern_match.group(1)
limit = 5
if not match:
return await event.eor("`Give Something to Search!`")
event = await event.eor("`...`")
if " ; " in match:
_ = match.split(" ; ", maxsplit=1)
match = _[0]
limit = int(_[1])
content = urlopen(f"https://www.shutterstock.com/search/{match.replace(' ', '+')}")
reso = bs(content, "html.parser", from_encoding="utf-8")
con = reso.find_all("img", src=re.compile("image.shutterstock.com"))
if not con:
return await event.eor("No Results Found!")
shuffle(con)
lml = [a["src"] for a in con[:limit]]
try:
await event.client.send_message(
event.chat_id, f"Uploaded {len(lml)} Images!", file=lml
)
except WebpageCurlFailedError:
NaN = []
for _ in lml:
NaN.append(await download_file(_, check_filename("shutter.png")))
await event.client.send_message(
event.chat_id, f"Uploaded {len(NaN)} Images!", file=NaN
)
[os.remove(a) for a in NaN]
await event.delete()