forked from nuhmanpk/pyDF-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (146 loc) · 6.68 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
import os
from os import error, system, name
import logging
import pyrogram
import PyPDF2
import time
from decouple import config
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ForceReply
from pyrogram.types import User, Message, Document
bughunter0 = Client(
"PyDF-BOT",
bot_token = os.environ["BOT_TOKEN"],
api_id = int(os.environ["API_ID"]),
api_hash = os.environ["API_HASH"]
)
START_STR = """
Hi **{}**, I'm PyDF Bot. I can Provide all Help regarding PDF file
"""
ABOUT = """
**BOT:** `PYDF BOT`
**AUTHOR :** [bughunter0](https://t.me/bughunter0)
**SERVER :** `Heroku`
**LIBRARY :** `Pyrogram`
**SOURCE :** [BugHunterBots](https://t.me/bughunterbots)
**LANGUAGE :** `Python 3.9`
"""
HELP = """
Send me a pdf file to Move on
"""
DOWNLOAD_LOCATION = os.environ.get("DOWNLOAD_LOCATION", "./DOWNLOADS/PyDF/")
# TXT_LOCATION = os.environ.get("TXT_LOCATION", "./DOWNLOADS/txt/")
path = './DOWNLOADS/txt/bughunter0.txt'
Disclaimer = """ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE """
START_BUTTON = InlineKeyboardMarkup(
[[
InlineKeyboardButton('ABOUT',callback_data='cbabout'),
InlineKeyboardButton('HELP',callback_data='cbhelp')
],
[
InlineKeyboardButton('↗ Join Here ↗', url='https://t.me/BughunterBots'),
]]
)
CLOSE_BUTTON = InlineKeyboardMarkup(
[[
InlineKeyboardButton('Back',callback_data='cbclose'),
]]
)
@bughunter0.on_callback_query() # callbackQuery()
async def cb_data(bot, update):
if update.data == "cbhelp":
await update.message.edit_text(
text=HELP,
reply_markup=CLOSE_BUTTON,
disable_web_page_preview=True
)
elif update.data == "cbabout":
await update.message.edit_text(
text=ABOUT,
reply_markup=CLOSE_BUTTON,
disable_web_page_preview=True
)
else:
await update.message.edit_text(
text=START_STR.format(update.from_user.mention),
disable_web_page_preview=True,
reply_markup=START_BUTTON
)
@bughunter0.on_message(filters.command(["start"])) # StartCommand
async def start(bot, update):
await update.reply_text(
text=START_STR.format(update.from_user.mention),
disable_web_page_preview=True,
reply_markup=START_BUTTON
)
@bughunter0.on_message(filters.document | (filters.document & filters.forwarded))
async def document(bot, message):
message_id=int(message.message_id)
chat_id=int(message.chat.id)
await bot.send_message(text=" ◆ /pdf2txt - Extract text to Txt file \n ◆ /info to Get PDF information",reply_to_message_id=message_id,chat_id=chat_id)
@bughunter0.on_message(filters.command(["pdf2txt"])) # PdfToText
async def pdf_to_text(bot, message):
try :
if message.reply_to_message:
pdf_path = DOWNLOAD_LOCATION + f"{message.chat.id}.pdf" #pdfFileObject
txt = await message.reply("Downloading.....")
await message.reply_to_message.download(pdf_path)
await txt.edit("Downloaded File")
pdf = open(pdf_path,'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf) #pdfReaderObject
await txt.edit("Getting Number of Pages....")
num_of_pages = pdf_reader.getNumPages() # Number of Pages
await txt.edit(f"Found {num_of_pages} Page")
page_no = pdf_reader.getPage(0) # pageObject
await txt.edit("Extracting Text from PDF...")
page_content = """ """ # EmptyString
with open(f'{message.chat.id}.txt', 'a+') as text_path:
for page in range (0,num_of_pages):
file_write = open(f'{message.chat.id}.txt','a+')
page_no = pdf_reader.getPage(page) # Iteration of page number
page_content = page_no.extractText()
file_write.write(f"\n page number - {page} \n") # writing Page Number as Title
file_write.write(f" {page_content} ") # writing page content
file_write.write(f"\n © BugHunterBots \n ") # Adding Page footer
# await message.reply_text(f"**Page Number : {page} **\n\n ` {page_content} `\n @BugHunterBots\n\n") # Use this Line of code to get Pdf Text as Messages
with open(f'{message.chat.id}.txt', 'a+') as text_path:
await message.reply_document(f"{message.chat.id}.txt",caption="©@BugHunterBots")
os.remove(pdf_path)
os.remove(f"{message.chat.id}.txt")
else :
await message.reply("Please Reply to PDF file")
except Exception as error :
await txt.delete()
await message.reply_text(f"{error}")
os.remove(pdf_path)
os.remove(f"{message.chat.id}.txt")
@bughunter0.on_message(filters.command(["info"]))
async def info(bot, message):
try:
if message.reply_to_message:
txt = await message.reply_text("Validating Pdf ")
pdf_path = DOWNLOAD_LOCATION + f"{message.chat.id}.pdf" #pdfFileObject
await txt.edit("Downloading.....")
await message.reply_to_message.download(pdf_path)
await txt.edit("Downloaded File")
pdf = open(pdf_path,'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf) #pdfReaderObject
await txt.edit("Getting Number of Pages....")
num_of_pages = pdf_reader.getNumPages()
await txt.edit(f"Found {num_of_pages} Page")
await txt.edit("Getting PDF info..")
info = pdf_reader.getDocumentInfo()
await txt.edit(f"""
**Author :** `{info.author}`
**Creator :** `{info.creator}`
**Producer :** `{info.producer}`
**Subject :** `{info.subject}`
**Title :** `{info.title}`
**Pages :** `{num_of_pages}`""")
os.remove(pdf_path)
else:
await message.reply_text("Please Reply to a Pdf File")
except Exception as error :
await message.reply_text(f"Oops , {error}")
# @bughunter0.on_message(filters.command(["merge"])) # Under Maintenance
bughunter0.run()