diff --git a/.gitignore b/.gitignore index bf0d076..53e59cd 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,5 @@ crashlytics.properties crashlytics-build.properties fabric.properties + +.telegramToken diff --git a/Dockerfile b/Dockerfile index ac0a3bb..90445f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/app.py b/app.py index 27b5161..a0fdd14 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +import os.path + import telebot from utils import search_download_youtube_video from loguru import logger @@ -32,7 +34,7 @@ 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): """ @@ -40,13 +42,21 @@ def download_user_photo(self, quality=0): :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""" @@ -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() - - diff --git a/requirements.txt b/requirements.txt index 1e50522..cf0c9c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyTelegramBotAPI yt-dlp>=2022.6.29 -loguru \ No newline at end of file +loguru~=0.6.0 \ No newline at end of file