forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added-Movie-Info-Telegram-Bot in Python (HarshCasper#835)
* Added-Movie-Info-Telegram-Bot in Python * Added movie-info-bot in python Fixes HarshCasper#832 * Updated bot.py * Formatted code
- Loading branch information
Showing
4 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
API_KEY = "xxxxxxxxxxxxxxxxxxx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Movie Info Telegram Bot | ||
|
||
## Description | ||
A telegram Bot made using python which scrapes IMDb website and has the following functionalities | ||
1. Replies to a movie name with genre and rating of the movie | ||
2. Replies to a genre with a list of top movies and tv shows belonging to that genre | ||
|
||
## Setup Instructions | ||
|
||
1. Install required packages: | ||
|
||
pip install -r requirements.txt | ||
|
||
2. Create a bot in telegram: | ||
|
||
1. Go to @BotFather and click /start and type /newbot and give it a name. | ||
2. Choose a username and get the token | ||
3. Paste the token in a .env file (Take [.env.example](.env.example) as an example) | ||
|
||
4. Run the python script to start the bot | ||
|
||
5. Type /start command to start conversation with the chatbot. | ||
|
||
6. Type /name <movie_name> to get the genre and Rating of the movie. The bot replies with atmost three results. | ||
7. Type /genre \<genre> to get a list of movies and TV shows belonging to that genres | ||
|
||
## Output | ||
|
||
### /start command | ||
|
||
<img src="https://i.ibb.co/jwpJHKX/start.png"> | ||
|
||
### /name command | ||
|
||
<img src="https://i.ibb.co/FzrGjSQ/movie.png"> | ||
|
||
### /genre command | ||
|
||
<img src="https://i.ibb.co/VJQy108/genre.png"> | ||
|
||
## Author | ||
|
||
[Aishwarya A J](https://github.com/aish2002) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import logging | ||
import requests | ||
import re | ||
import urllib.request | ||
import urllib.parse | ||
import urllib.error | ||
from bs4 import BeautifulSoup | ||
import ssl | ||
import itertools | ||
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters | ||
import decouple | ||
|
||
# Enable logging | ||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
level=logging.INFO) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
TOKEN = decouple.config("API_KEY") | ||
|
||
# Define a few command handlers. These usually take the two arguments update and | ||
# context. Error handlers also receive the raised TelegramError object in error. | ||
|
||
|
||
def start(update, context): | ||
"""Send a message when the command /start is issued.""" | ||
update.message.reply_text( | ||
'What can this bot do?\n\nThis bot gives brief information about any movie from IMDb website' | ||
+ '\nSend /name movie_name to know the genre and rating of the movie.\nSend /genre genre_name to' | ||
+ 'get the list of movies belonging to that genre' | ||
) | ||
|
||
|
||
def help(update, context): | ||
"""Send a message when the command /help is issued.""" | ||
update.message.reply_text('Help!') | ||
|
||
|
||
def genre(update, context): | ||
"""Send a list of movies when the command /genre is issued.""" | ||
url = 'https://www.imdb.com/search/title/' | ||
genre = str(update.message.text)[7:] | ||
print(genre) | ||
r = requests.get(url+'?genres='+genre) | ||
soup = BeautifulSoup(r.text, "html.parser") | ||
title = soup.find('title') | ||
if title.string == 'IMDb: Advanced Title Search - IMDb': | ||
update.message.reply_text("Sorry,No such genre.Try again") | ||
else: | ||
res = [] | ||
res.append(title.string+'\n') | ||
tags = soup('a') | ||
for tag in tags: | ||
movie = re.search('<a href=\"/title/.*>(.*?)</a>', str(tag)) | ||
try: | ||
if "&" in movie.group(1): | ||
movie.group(1).replace("&", "&") | ||
res.append(movie.group(1)) | ||
except: | ||
pass | ||
stri = "" | ||
for i in res: | ||
stri += i+'\n' | ||
update.message.reply_text(stri) | ||
|
||
|
||
def name(update, context): | ||
"""Send the first 3 search results of the movie name in IMDb site when the command /name is issued.""" | ||
movie = str(update.message.text)[6:] | ||
print(movie) | ||
res = get_info(movie) | ||
stri = "" | ||
for i in res: | ||
for a in i: | ||
stri += a+'\n' | ||
stri += '\n' | ||
update.message.reply_text(stri) | ||
|
||
|
||
def error(update, context): | ||
"""Log Errors caused by Updates.""" | ||
logger.warning('Update "%s" caused error "%s"', update, context.error) | ||
|
||
|
||
def get_info(movie): | ||
"To scrape IMDb and get genre and rating of the movie " | ||
url = 'https://www.imdb.com/find?q=' | ||
r = requests.get(url+movie+'&ref_=nv_sr_sm') | ||
soup = BeautifulSoup(r.text, "html.parser") | ||
title = soup.find('title') | ||
tags = soup('a') | ||
pre_url = "" | ||
count = 0 | ||
lis = [] | ||
res = [] | ||
for tag in tags: | ||
if(count > 2): | ||
break | ||
m = re.search('<a href=.*>(.*?)</a>', str(tag)) | ||
try: | ||
lis = [] | ||
link = re.search('/title/(.*?)/', str(m)) | ||
new_url = 'https://www.imdb.com'+str(link.group(0)) | ||
if new_url != pre_url: | ||
html = requests.get(new_url) | ||
soup_each_title = BeautifulSoup(html.text, "html.parser") | ||
movie_title = soup_each_title.find( | ||
'title').string.replace('- IMDb', ' ') | ||
anchor = soup_each_title('a') | ||
genre_string = "Genre : " | ||
for each in anchor: | ||
genre = re.search( | ||
'<a href=\"/search/title\?genres=.*> (.*?)</a>', str(each)) | ||
try: | ||
genre_string += genre.group(1)+' ' | ||
except: | ||
pass | ||
strong_tag = soup_each_title('strong') | ||
for i in strong_tag: | ||
rating = re.search('<strong title=\"(.*?) based', str(i)) | ||
try: | ||
rating_string = "IMDb Rating : "+rating.group(1) | ||
except: | ||
pass | ||
details = "For more details : "+new_url | ||
lis.append(movie_title) | ||
lis.append(genre_string) | ||
lis.append(rating_string) | ||
lis.append(details) | ||
pre_url = new_url | ||
count += 1 | ||
res.append(lis) | ||
except: | ||
pass | ||
return res | ||
|
||
|
||
def main(): | ||
"""Start the bot.""" | ||
# Create the Updater and pass it your bot's token. | ||
# Make sure to set use_context=True to use the new context based callbacks | ||
updater = Updater(TOKEN, use_context=True) | ||
|
||
# Get the dispatcher to register handlers | ||
dp = updater.dispatcher | ||
|
||
# on different commands - reply in Telegram | ||
dp.add_handler(CommandHandler("start", start)) | ||
dp.add_handler(CommandHandler("help", help)) | ||
dp.add_handler(CommandHandler("name", name)) | ||
dp.add_handler(CommandHandler("genre", genre)) | ||
|
||
# log all errors | ||
dp.add_error_handler(error) | ||
|
||
# Start the Bot | ||
updater.start_polling() | ||
|
||
# Run the bot until you press Ctrl-C or the process receives SIGINT, | ||
# SIGTERM or SIGABRT. This should be used most of the time, since | ||
# start_polling() is non-blocking and will stop the bot gracefully. | ||
updater.idle() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
APScheduler==3.6.3 | ||
beautifulsoup4==4.9.3 | ||
certifi==2020.12.5 | ||
python-dateutil==2.8.1 | ||
python-decouple==3.4 | ||
python-telegram-bot==13.1 | ||
pytz==2020.4 | ||
requests==2.25.1 | ||
six==1.15.0 | ||
soupsieve==2.1 | ||
tornado==6.1 | ||
urllib3==1.26.2 |