forked from nuhmanpk/WebScrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
310 lines (267 loc) · 11 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# © BugHunterCodeLabs ™
# © bughunter0
# © Nuhman Pk
# 2021 - 2023
# Copyright - https://en.m.wikipedia.org/wiki/Fair_use
import os
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
from bs4 import BeautifulSoup
import requests
import os
import shutil
from urllib.parse import quote
from dotenv import load_dotenv
import os
# Carga las variables de entorno desde el archivo .env
load_dotenv()
# Retrieve values from environment variables
bot_token = os.getenv('BOT_TOKEN')
api_id = os.getenv('ID_AS_STRING')
api_hash = os.getenv('API_HASH')
if bot_token is None or api_id is None or api_hash is None:
raise ValueError("Please set the BOT_TOKEN, API_ID, and API_HASH environment variables.")
REPO = 'https://github.com/nuhmanpk/WebScrapper/'
FINISHED_PROGRESS_STR = "▓"
UN_FINISHED_PROGRESS_STR = "░"
START_BUTTON = InlineKeyboardMarkup(
[
[
InlineKeyboardButton('Raw Data', callback_data='cbrdata'),
InlineKeyboardButton('HTML Data', callback_data='cbhtmldata')
],
[
InlineKeyboardButton('All Links', callback_data='cballlinks'),
InlineKeyboardButton(
'All Paragraphs', callback_data='cballparagraphs')
],
[
InlineKeyboardButton('All Images', callback_data='cballimages')
]
]
)
CLOSE_BUTTON = InlineKeyboardMarkup(
[[InlineKeyboardButton('Back', callback_data='cbclose')]]
)
@app.on_message(filters.command(["start"]))
async def start(_, message: Message):
# Edit Your Start string here
text = f"Hello , I am a web scrapper bot." \
"\nSend me any link for scrapping.\n\nJoin @BugHunterBots"
await message.reply_text(text=text, disable_web_page_preview=True, quote=True)
async def download_image(base_url, image_url, idx):
try:
r = requests.get(image_url, stream=True)
r.raise_for_status()
with open(f"image{idx}.jpg", 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
with open(f"image{idx}.jpg", "rb") as f:
image_data = f.read()
os.remove(f"image{idx}.jpg")
return image_data
except requests.exceptions.HTTPError as errh:
print(f"HTTP Error: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
print(f"Request Exception: {err}")
except Exception as e:
print(f"Error downloading image from {image_url}: {e}")
return None
@app.on_callback_query()
async def cb_data(bot, update):
if update.data == "cbrdata":
await raw_data_scraping(update)
elif update.data == "cbhtmldata":
await html_data_scraping(update)
elif update.data == "cballlinks":
await all_links_scraping(update)
elif update.data == "cballparagraphs":
await all_paragraph_scraping(update)
elif update.data == "cballimages":
await all_images_scraping(update)
else:
await update.message.edit_text(
text="something",
disable_web_page_preview=True,
reply_markup=START_BUTTON
)
async def scrape(url):
try:
request = requests.get(url)
soup = BeautifulSoup(request.content, 'html5lib')
return request, soup
except Exception as e:
print(e)
return None, None
async def raw_data_scraping(query):
try:
message = query.message
request, soup = await scrape(message.text)
file_write = open(f'RawData-{message.chat.username}.txt', 'a+')
file_write.write(f"{request.content}")
file_write.close()
await message.reply_document(f"RawData-{message.chat.username}.txt", caption="©@BugHunterBots", quote=True)
os.remove(f"RawData-{message.chat.username}.txt")
return
except Exception as e:
os.remove(f"RawData-{message.chat.username}.txt")
error = f"ERROR: {(str(e))}"
error_link = f"{REPO}/issues/new?title={quote(error)}"
text = f'Something Bad occurred !!!\nCreate an issue here'
issue_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton("Create Issue", url=error_link)]])
await message.reply_text(text, disable_web_page_preview=True, quote=True, reply_markup=issue_markup)
return e
async def html_data_scraping(query):
try:
message = query.message
request, soup = await scrape(message.text)
file_write = open(f'HtmlData-{message.chat.username}.txt', 'a+')
soup.data = soup.prettify()
file_write.write(f"{soup.data}")
file_write.close()
await message.reply_document(f"HtmlData-{message.chat.username}.txt", caption="©@BugHunterBots", quote=True)
os.remove(f"HtmlData-{message.chat.username}.txt")
except Exception as e:
os.remove(f"HtmlData-{message.chat.username}.txt")
error = f"ERROR: {(str(e))}"
error_link = f"{REPO}/issues/new?title={quote(error)}"
text = f'Something Bad occurred !!!\nCreate an issue here'
issue_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton("Create Issue", url=error_link)]])
await message.reply_text(text, disable_web_page_preview=True, quote=True, reply_markup=issue_markup)
return e
async def all_links_scraping(query):
try:
message = query.message
request, soup = await scrape(message.text)
file_write = open(f'AllLinks-{message.chat.username}.txt', 'a+')
for link in soup.find_all('a'):
links = link.get('href')
file_write.write(f"{links}\n\n")
file_write.close()
await message.reply_document(
f"AllLinks-{message.chat.username}.txt",
caption="©@BugHunterBots"
)
os.remove(f"AllLinks-{message.chat.username}.txt")
except Exception as e:
os.remove(f"AllLinks-{message.chat.username}.txt")
error = f"ERROR: {(str(e))}"
error_link = f"{REPO}/issues/new?title={quote(error)}"
text = f'Something Bad occurred !!!\nCreate an issue here'
issue_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton("Create Issue", url=error_link)]])
await message.reply_text(text, disable_web_page_preview=True, quote=True, reply_markup=issue_markup)
return e
async def all_paragraph_scraping(query):
try:
message = query.message
request, soup = await scrape(message.text)
file_write = open(f'AllParagraph-{message.chat.username}.txt', 'a+')
paragraph = ""
for para in soup.find_all('p'):
paragraph = para.get_text()
file_write.write(f"{paragraph}\n\n")
file_write.close()
await message.reply_document(
f"AllParagraph-{message.chat.username}.txt",
caption="©@BugHunterBots",
quote=True
)
os.remove(f"AllParagraph-{message.chat.username}.txt")
except Exception as e:
os.remove(f"AllParagraph-{message.chat.username}.txt")
error = f"ERROR: {(str(e))}"
error_link = f"{REPO}/issues/new?title={quote(error)}"
text = f'Something Bad occurred !!!\nCreate an issue here'
issue_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton("Create Issue", url=error_link)]])
await message.reply_text(text, disable_web_page_preview=True, quote=True, reply_markup=issue_markup)
return e
async def all_images_scraping(query):
try:
message = query.message
txt = await message.reply_text("Scraping url ...", quote=True)
request, soup = await scrape(message.text)
image_links = []
for img_tag in soup.find_all('img'):
img_url = img_tag.get('src')
image_links.append(img_url)
await txt.edit(text=f"Found {len(image_links)} Images", disable_web_page_preview=True)
if len(image_links):
status = await message.reply_text("Downloading...", quote=True)
folder_name = f"{message.chat.id}"
os.makedirs(folder_name, exist_ok=True)
for idx, image_link in enumerate(image_links):
image_data = await download_image(message.text, image_link, idx)
if image_data:
with open(f"{folder_name}/image{idx}.jpg", "wb") as file:
file.write(image_data)
progress, finished_length = await progress_bar(idx+1, len(image_links))
try:
await status.edit(f"Percentage: {finished_length*10}%\nProgress: {progress}\n")
except:
pass
zip_filename = f"{message.chat.id}.zip"
shutil.make_archive(folder_name, 'zip', folder_name)
await message.reply_document(open(zip_filename, "rb"), caption="Here are the images!")
await status.delete()
shutil.rmtree(folder_name)
os.remove(zip_filename)
return
else:
await txt.edit(text=f"No Images Found!!!", disable_web_page_preview=True)
return
except Exception as e:
error = f"ERROR: {(str(e))}"
error_link = f"{REPO}/issues/new?title={quote(error)}"
text = f'Something Bad occurred !!!\nCreate an issue here'
issue_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton("Create Issue", url=error_link)]])
await message.reply_text(text, disable_web_page_preview=True, quote=True, reply_markup=issue_markup)
return e
async def progress_bar(current, total):
percentage = current / total
finished_length = int(percentage * 10)
unfinished_length = 10 - finished_length
progress = f"{FINISHED_PROGRESS_STR * finished_length}{UN_FINISHED_PROGRESS_STR * unfinished_length}"
return progress, finished_length
@app.on_message((filters.regex("https") | filters.regex("http") | filters.regex("www")) & filters.private)
async def scrapping(bot, message):
await send_message_with_options(message)
async def send_message_with_options(message):
reply_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton('Raw Data', callback_data='cbrdata',),
InlineKeyboardButton('HTML Data', callback_data='cbhtmldata')
],
[
InlineKeyboardButton('All Links', callback_data='cballlinks'),
InlineKeyboardButton(
'All Paragraphs', callback_data='cballparagraphs')
],
[
InlineKeyboardButton('All Images', callback_data='cballimages')
]
]
)
await message.reply_text('Choose an Option')
await message.reply_text(message.text, reply_markup=reply_markup)
# Use soup.find_all('tag_name') to Extract Specific Tag Details
"""
soup.title
# <title>This is Title</title>
soup.title.name
# u'title'
soup.title.string
# u'This is a string'
soup.title.parent.name
# u'head'
"""
app.run(print('Bot Running....'))