bots: Refactor Youtube bot.

This commit is contained in:
Sivagiri Visakan 2017-12-09 15:32:16 +05:30 committed by showell
parent f947ff44f8
commit 6f9d010ed3
18 changed files with 663 additions and 42 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

View file

@ -1,11 +0,0 @@
# Youtube bot
Youtube bot is a Zulip bot that can fetch first video from youtube
search results for a specified term. To use youtube bot you can simply
call it with `@mention-bot` followed by a command. Like this:
```
@mention-bot <search term>
```
![example usage](assets/screen.png)

View file

@ -1,31 +0,0 @@
# See readme.md for instructions on running this bot.
import requests
from bs4 import BeautifulSoup
class YoutubeHandler(object):
def usage(self):
return '''
This bot will return the first Youtube search result for the give query.
'''
def handle_message(self, message, bot_handler):
help_content = '''
To use the, Youtube Bot send `@mention-bot search terms`
Example:
@mention-bot funny cats
'''.strip()
if message['content'] == '':
bot_handler.send_reply(message, help_content)
else:
text_to_search = message['content']
url = "https://www.youtube.com/results?search_query=" + text_to_search
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
video_id = soup.find(attrs={'class': 'yt-uix-tile-link'})
try:
link = 'https://www.youtube.com' + video_id['href']
bot_handler.send_reply(message, link)
except TypeError:
bot_handler.send_reply(message, 'No video found for specified search terms')
handler_class = YoutubeHandler