From b4bf1dc66c931f3ab05f3340ccc32a89c7b619a2 Mon Sep 17 00:00:00 2001 From: Lidor Date: Fri, 30 Sep 2022 16:51:45 +0300 Subject: [PATCH 1/7] updated gitignore to ignore t_token --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From 25c729f7720021cab71f65b1dc4d70ec5caa394f Mon Sep 17 00:00:00 2001 From: Lidor Date: Fri, 30 Sep 2022 16:53:34 +0300 Subject: [PATCH 2/7] finshed todo for downloading photo and implemented saveing photo to dir --- app.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 27b5161..99f5692 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 = file_info.file_path.split('/')[0] + + if not os.path.exists(folder): + os.makedirs(folder) + + with open(file_info.file_path, 'wb') as photo: + photo.write(data) + - # TODO save `data` as a photo in `file_info.file_path` path def handle_message(self, message): """Bot Main message handler""" @@ -61,14 +71,25 @@ def handle_message(self, message): class YoutubeBot(Bot): - pass + + def handle_message(self, message): + + if self.current_msg. + + + if self.is_current_msg_photo(): + self.download_user_photo(quality=3) + return + + video = search_download_youtube_video(message.text) + video_link = video[0].get("url") + self.send_text(video_link) + if __name__ == '__main__': with open('.telegramToken') as f: _token = f.read() - my_bot = QuoteBot(_token) + my_bot = YoutubeBot(_token) my_bot.start() - - From cd8af93e1bacadafdc85b68b94f87a8e81908638 Mon Sep 17 00:00:00 2001 From: Lidor Date: Fri, 30 Sep 2022 16:57:02 +0300 Subject: [PATCH 3/7] requierment file --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 70aa17c5c98237fcad509122600d091e09f9c3e7 Mon Sep 17 00:00:00 2001 From: Lidor Date: Fri, 30 Sep 2022 16:58:51 +0300 Subject: [PATCH 4/7] updated docker file --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From b1d5e7d630f3b13d2f15159c9c4d63fe7de2b89d Mon Sep 17 00:00:00 2001 From: Lidor Date: Fri, 30 Sep 2022 17:06:57 +0300 Subject: [PATCH 5/7] fixed typo --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 99f5692..831589c 100644 --- a/app.py +++ b/app.py @@ -74,7 +74,7 @@ class YoutubeBot(Bot): def handle_message(self, message): - if self.current_msg. + if self.is_current_msg_photo(): From 8ee7265066ca75ee36d559b2b533c53a6fa711af Mon Sep 17 00:00:00 2001 From: Lidor Date: Sun, 2 Oct 2022 14:27:38 +0300 Subject: [PATCH 6/7] added dock string and created method to send video --- app.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 831589c..8cadedf 100644 --- a/app.py +++ b/app.py @@ -48,16 +48,14 @@ def download_user_photo(self, quality=0): file_info = self.bot.get_file(self.current_msg.photo[quality].file_id) data = self.bot.download_file(file_info.file_path) - folder = file_info.file_path.split('/')[0] + folder_name = file_info.file_path.split('/')[0] - if not os.path.exists(folder): - os.makedirs(folder) + if not os.path.exists(folder_name): + os.makedirs(folder_name) with open(file_info.file_path, 'wb') as photo: photo.write(data) - - def handle_message(self, message): """Bot Main message handler""" logger.info(f'Incoming message: {message}') @@ -71,20 +69,44 @@ def handle_message(self, message): class YoutubeBot(Bot): + """ + the class getting message and searching in youtube for videos that have the same name as the message + """ def handle_message(self, message): + """ + youtube bot message handler + """ + if self.is_current_msg_photo(): + self.download_user_photo(quality=3) + return + video = self.download_video_from_youtube(message) + self.send_text(self.get_downloaded_video_link(video)) + def send_video(self, message, path): + video = open(path, 'rb') + self.bot.send_video(message.chat.id, video) - if self.is_current_msg_photo(): - self.download_user_photo(quality=3) - return + 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) - video_link = video[0].get("url") - self.send_text(video_link) + 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__': From 33f3d435a6f63ee712b0555fe1b91b8c0c8bdefc Mon Sep 17 00:00:00 2001 From: lidoror Date: Sun, 9 Oct 2022 00:05:56 +0300 Subject: [PATCH 7/7] bot finished --- app.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 8cadedf..a0fdd14 100644 --- a/app.py +++ b/app.py @@ -56,6 +56,8 @@ def download_user_photo(self, quality=0): with open(file_info.file_path, 'wb') as photo: photo.write(data) + return file_info + def handle_message(self, message): """Bot Main message handler""" logger.info(f'Incoming message: {message}') @@ -73,22 +75,41 @@ class YoutubeBot(Bot): 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 send_video(self, message, path): + 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