Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d496483

Browse files
Merge pull request avinashkranjan#2320 from Swapnil-Singh-99/telegram-book-bot
Added a Telegram Book Bot
2 parents c509819 + e6b0be9 commit d496483

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

‎Telegram_Book_Bot/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Book-bot-Telegram
2+
This telegram bot that provides user with a book which the user wanted using Telegram.
3+
4+
<br/>
5+
<br/>
6+
7+
**Create environment variables**
8+
9+
```javascript
10+
YOUR_BOT_TOKEN
11+
OWNER_TELEGRAM_ID
12+
```
13+
14+
**RUN**
15+
```javascript
16+
python main.py
17+
```

‎Telegram_Book_Bot/book.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# for scraping books
2+
from bs4 import BeautifulSoup as bs
3+
import requests
4+
# to identify emoji unicode characters
5+
import demoji
6+
7+
8+
def link_to_get(link):
9+
"""This function will get the url of the image & book download direct link using the given link for book download"""
10+
response = requests.get(link)
11+
th_html = bs(response.text , "html.parser")
12+
td_all = th_html.find_all("td" ,id ="info")
13+
td_all = td_all[0]
14+
td_a = td_all.find_all("a")
15+
link_href = td_a[1].get("href")
16+
img_link_td = td_all.find("img" ,alt="cover")
17+
img_link_src = img_link_td.get("src")
18+
img_link = f"http://library.lol{img_link_src}"
19+
return [link_href, img_link]
20+
21+
22+
23+
def book_get(name, mainres=25, results=5):
24+
"""This function returns the list of books for the given name
25+
26+
You can give in name :
27+
1. title of book
28+
2. isbn of book
29+
3. author of book
30+
4. publisher of book
31+
32+
Results:
33+
[ 0.Book Name,
34+
1.Author,
35+
2.Publisher,
36+
3.Size,
37+
4.Book Type,
38+
5.Book Link,
39+
6.Book Image Link
40+
7.Language]"""
41+
42+
Books = []
43+
name = demoji.replace(name, '')
44+
if name == "":
45+
return "Error: enter name"
46+
name = name.replace(" ", "+")
47+
# getting request and response
48+
url = f"http://libgen.is/search.php?req={name}&lg_topic=libgen&open=0&view=simple&res={mainres}&phrase=1&column=def"
49+
response = requests.get(url)
50+
bs_html = bs(response.text , "html.parser")
51+
52+
if "Search string must contain minimum 3 characters.." in bs_html.body:
53+
return "Error: Title Too Short"
54+
55+
# scraping the site for response
56+
table = bs_html.find_all("table")
57+
table = table[2]
58+
table_rows = table.find_all("tr")
59+
a = len(table_rows)
60+
table_rows.pop(0)
61+
if a > 1 :
62+
counter = 0
63+
for i in table_rows :
64+
if counter <= results:
65+
# make book list
66+
book_lst = []
67+
# getting all table datas
68+
table_datas = i.find_all("td")
69+
# book name
70+
book_name = table_datas[2].get_text()
71+
# author name
72+
author = table_datas[1].get_text()
73+
# publisher name
74+
publisher = table_datas[3].get_text()
75+
if publisher == "":
76+
publisher = "unknown"
77+
# getting link to book
78+
link_row = table_datas[9]
79+
a = link_row.find("a" , href = True)
80+
link = a.get("href")
81+
# getting image url & direct book download link
82+
link_all = link_to_get(link)
83+
# getting language
84+
language_row = table_datas[6]
85+
language = language_row.get_text()
86+
# getting size of book
87+
size_row = table_datas[7]
88+
size = size_row.get_text()
89+
# getting type of book
90+
type_row = table_datas[8]
91+
type_ofit = type_row.get_text()
92+
# this will only take pdfs in English Language
93+
if (type_ofit != "pdf" and type_ofit != "epub") or language != "English":
94+
continue
95+
book_lst.append(book_name)
96+
book_lst.append(author)
97+
book_lst.append(publisher)
98+
book_lst.append(size)
99+
book_lst.append(type_ofit)
100+
book_lst.append(link_all[0])
101+
book_lst.append(link_all[1])
102+
book_lst.append(language)
103+
Books.append(book_lst)
104+
counter+=1
105+
if len(Books) >=1 :
106+
return Books
107+
else :
108+
return "Error: no results found"
109+
else:
110+
return "Error: no results found"
111+
112+
113+
114+
if __name__ == "__main__":
115+
a = book_get("Python",25,5)
116+
if "Error" not in a:
117+
for i in a :
118+
print(f"\n\nName : {i[0]}\nAuthor : {i[1]}\nPublisher : {i[2]}\nSize : {i[3]}\nFormat : {i[4]}\nLink : {i[5]}\nImage : {i[6]}\n\n")
119+
else:
120+
print(a)

‎Telegram_Book_Bot/main.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from unittest import result
2+
from bs4 import BeautifulSoup as bs
3+
import requests
4+
import telebot
5+
import os
6+
import time
7+
from book import book_get
8+
9+
# BOT HEADERS............
10+
11+
bot = telebot.TeleBot(os.environ['YOUR_BOT_TOKEN'])
12+
results = 5
13+
mainres = 25
14+
15+
# ////////////////
16+
# BOT COMMANDS............\\\
17+
18+
@bot.message_handler(commands=["start"])
19+
def starting(message):
20+
text = f"I am a simple book bot.\nI will help you to download books of your own choice\nOur syntax is easy just send\n/book<space><book name>\nWithout without bracket sign.\nEXAMPLE : /book abc\n\nSend /help to get help"
21+
bot.reply_to(message , text)
22+
23+
24+
@bot.message_handler(commands=["help"])
25+
def help(message):
26+
text = f"Commands:\n1. /book <book_name>"
27+
bot.reply_to(message , text)
28+
29+
30+
@bot.message_handler(commands = ["book"])
31+
def books_get(message):
32+
id = message.from_user.id
33+
given_name = message.text[6:]
34+
messageId = bot.reply_to(message , "Please wait...").message_id
35+
chatId = message.chat.id
36+
data = book_get(given_name)
37+
if data == "Error: emoji":
38+
bot.reply_to(message , "Error: emoji\nPlease do not use Emojis😂")
39+
elif data == "Error: no results found":
40+
bot.reply_to(message , "Error: no results found\nPlease try for another book.")
41+
elif data == "Error: enter name":
42+
bot.reply_to(message , "Error: enter name\nPlease provide the name of book you are looking for")
43+
elif data == "Error: Title Too Short":
44+
bot.reply_to(message , "Error: Title Too Short\nPlease provide full title for better results")
45+
else:
46+
counter = 0
47+
bot.delete_message(chatId, messageId)
48+
for i in data:
49+
if counter <= results:
50+
dn = f"[DOWNLOAD NOW]({i[5]})"
51+
caption_all = f"*Name* : {i[0]}\n*Author* : {i[1]}\n*Size* : {i[3]}\n*Format* : {i[4]}\n{dn}"
52+
bot.send_photo(id ,i[6] , caption = caption_all ,parse_mode ="Markdown" )
53+
counter+=1
54+
55+
56+
while True:
57+
try:
58+
bot.polling(non_stop=True, interval=0)
59+
except Exception as e:
60+
print(e)
61+
time.sleep(5)
62+
continue
63+
64+
65+
# ////////////////

‎Telegram_Book_Bot/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
beautifulsoup4==4.11.1
2+
demoji==1.1.0
3+
pyTelegramBotAPI==4.12.0
4+
pyTelegramBotAPI==4.12.0
5+
Requests==2.31.0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /