From 67980e565e7575fe7071323cae3a7978bde9e767 Mon Sep 17 00:00:00 2001 From: idotuvia Date: Sun, 16 Oct 2022 12:54:15 +0300 Subject: [PATCH 1/4] this is situation where we send url of youtube. not enough --- .gitignore | 1 + Dockerfile | 7 +++---- app.py | 21 ++++++++++++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index bf0d076..de11e53 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ instance/ # Scrapy stuff: .scrapy +.telegramToken # Sphinx documentation docs/_build/ diff --git a/Dockerfile b/Dockerfile index ac0a3bb..b9011fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,6 @@ FROM python:3.8.12-slim-buster - -# YOUR COMMANDS HERE -# .... -# .... +WORKDIR /TelegramAI +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..61e5e85 100644 --- a/app.py +++ b/app.py @@ -31,6 +31,9 @@ def send_text(self, text): 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 send_video(self, video_path): + self.bot.send_video(self.current_msg.chat.id, video_path) + def is_current_msg_photo(self): return self.current_msg['content_type'] != 'photo' @@ -41,10 +44,17 @@ def download_user_photo(self, quality=0): :return: """ if self.current_msg['content_type'] != 'photo': - raise RuntimeError(f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}') + 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) + if self.is_current_msg_photo(): + with open(f'photos/{self.current_msg.photo[quality].file_id}.jpg', 'wb') as f: + f.write(data) + else: + raise RuntimeError( + f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}') # TODO save `data` as a photo in `file_info.file_path` path @@ -61,14 +71,15 @@ def handle_message(self, message): class YoutubeBot(Bot): - pass + def handle_message(self, message): + if message.text != 'Don\'t download me please': + x = search_download_youtube_video(message.text) + self.send_text(x[0].get("url")) if __name__ == '__main__': with open('.telegramToken') as f: _token = f.read() - my_bot = QuoteBot(_token) + my_bot = YoutubeBot(_token) my_bot.start() - - From e1125d0bca0bc21b81c36c6533969b402783e1f4 Mon Sep 17 00:00:00 2001 From: idotuvia Date: Sun, 16 Oct 2022 16:22:00 +0300 Subject: [PATCH 2/4] working ffs --- app.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 5a096be..a6d4d99 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ import telebot from utils import search_download_youtube_video from loguru import logger +import os class Bot: @@ -31,8 +32,7 @@ def send_text(self, text): 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 send_video(self, video_path): - self.bot.send_video(self.current_msg.chat.id, video_path) + def is_current_msg_photo(self): return self.current_msg.content_type == 'photo' @@ -71,9 +71,12 @@ def handle_message(self, message): class YoutubeBot(Bot): def handle_message(self, message): - if message.text != 'Don\'t download me please': - x = search_download_youtube_video(message.text) - self.send_text(x[0].get("url")) + youtube = search_download_youtube_video(message.text) + self.send_video(message, os.path.join('./', youtube[0].get("filename"))) + + def send_video(self, message, path): + video = open(path, 'rb') + self.bot.send_video(message.chat.id, video) if __name__ == '__main__': From ec3ee881cd3472c498f69c4d6147438019758836 Mon Sep 17 00:00:00 2001 From: idotuvia Date: Mon, 17 Oct 2022 14:16:17 +0300 Subject: [PATCH 3/4] working ffs --- app.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index a6d4d99..62f4253 100644 --- a/app.py +++ b/app.py @@ -48,12 +48,17 @@ 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) - if self.is_current_msg_photo(): - with open(f'photos/{self.current_msg.photo[quality].file_id}.jpg', 'wb') as f: - f.write(data) - else: - raise RuntimeError( - f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}') + folder_name = 'photos' + + if not os.path.exists(folder_name): + os.makedirs(folder_name) + + with open(file_info.file_path, 'wb') as f: + f.write(data) + f.flush() + + self.send_text(f'Photo saved to {folder_name}/{file_info.file_path}') + return file_info # TODO save `data` as a photo in `file_info.file_path` path @@ -71,8 +76,12 @@ def handle_message(self, message): class YoutubeBot(Bot): def handle_message(self, message): - youtube = search_download_youtube_video(message.text) - self.send_video(message, os.path.join('./', youtube[0].get("filename"))) + if self.is_current_msg_photo(): + self.download_user_photo(quality=3) + else: + youtube = search_download_youtube_video(message.text) + self.send_video(message, os.path.join('./', youtube[0].get("filename"))) + self.send_text('thank you, send me another photo or a video name') def send_video(self, message, path): video = open(path, 'rb') From 307251ce35237828dc50832863eb8619e08fae3b Mon Sep 17 00:00:00 2001 From: idotuvia Date: Mon, 17 Oct 2022 14:24:08 +0300 Subject: [PATCH 4/4] changed quality to 0!!! --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 62f4253..fdeeb96 100644 --- a/app.py +++ b/app.py @@ -77,7 +77,7 @@ def handle_message(self, message): class YoutubeBot(Bot): def handle_message(self, message): if self.is_current_msg_photo(): - self.download_user_photo(quality=3) + self.download_user_photo() else: youtube = search_download_youtube_video(message.text) self.send_video(message, os.path.join('./', youtube[0].get("filename")))