Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,5 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties


.telegramToken
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM python:3.8.12-slim-buster

# YOUR COMMANDS HERE
# ....
# ....
WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD ["python3", "app.py"]
80 changes: 72 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os.path

import telebot
from utils import search_download_youtube_video
from loguru import logger
Expand Down Expand Up @@ -32,21 +34,29 @@ def send_text_with_quote(self, text, message_id):
self.bot.send_message(self.current_msg.chat.id, text, reply_to_message_id=message_id)

def is_current_msg_photo(self):
return self.current_msg['content_type'] != 'photo'
return self.current_msg.content_type == 'photo'

def download_user_photo(self, quality=0):
"""
Downloads photos sent to the Bot to `photos` directory (should be existed)
:param quality: integer representing the file quality. Allowed values are [0, 1, 2, 3]
:return:
"""
if self.current_msg['content_type'] != 'photo':
raise RuntimeError(f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}')
if self.current_msg.content_type != 'photo':
raise RuntimeError(
f'Message content of type \'photo\' expected, but got {self.current_msg.content_type}')

file_info = self.bot.get_file(self.current_msg.photo[quality].file_id)
data = self.bot.download_file(file_info.file_path)
folder_name = file_info.file_path.split('/')[0]

if not os.path.exists(folder_name):
os.makedirs(folder_name)

# TODO save `data` as a photo in `file_info.file_path` path
with open(file_info.file_path, 'wb') as photo:
photo.write(data)

return file_info

def handle_message(self, message):
"""Bot Main message handler"""
Expand All @@ -61,14 +71,68 @@ def handle_message(self, message):


class YoutubeBot(Bot):
pass
"""
the class getting message and searching in youtube for videos that have the same name as the message
"""

def __init__(self, token):
super().__init__(token)
self.cache = {}

def handle_message(self, message):
"""
youtube bot message handler
"""

if self.is_current_msg_photo():
self.download_user_photo(quality=3)
return


if message.text in self.cache:
file_path = os.path.join('./', self.cache.get(message.text))
if self.file_exist(file_path):
self.send_video(message, file_path)
return

video = self.download_video_from_youtube(message)
self.cache[message.text] = video[0].get('filename')
self.send_text(self.get_downloaded_video_link(video))

def file_exist(self, file_path):
return os.path.exists(file_path)

def send_video(self, message, path):
video = open(path, 'rb')
self.bot.send_video(message.chat.id, video)

def send_photo(self, message, path):
photo = open(path, 'rb')
self.bot.send_photo(message.chat.id, photo)

def download_video_from_youtube(self, message):
"""
":param: message: the message sent from the user
this method downloads video from youtube
:return: the downloaded video
"""

video = search_download_youtube_video(message.text)
return video

def get_downloaded_video_link(self, video):
"""
:param: video: video sent to the method
this method gets video
:return: url of teh video
"""
video_link = video[0].get("url")
return video_link


if __name__ == '__main__':
with open('.telegramToken') as f:
_token = f.read()

my_bot = QuoteBot(_token)
my_bot = YoutubeBot(_token)
my_bot.start()


2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pyTelegramBotAPI
yt-dlp>=2022.6.29
loguru
loguru~=0.6.0