From 3c07d48eda67865f9e21489635b64fd4980ffbc7 Mon Sep 17 00:00:00 2001 From: Vlad <107487272+vladislav-spector@users.noreply.github.com> Date: Sat, 22 Oct 2022 20:26:49 +0300 Subject: [PATCH] Finish python_ex1 --- .gitignore | 5 ++++- Dockerfile | 8 ++++---- app.py | 26 ++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index bf0d076..f1883aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ # Created by .ignore support plugin (hsz.mobi) + +# Project Specific Ignores +.telegramToken + ### Python template # Byte-compiled / optimized / DLL files __pycache__/ @@ -153,4 +157,3 @@ com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties fabric.properties - diff --git a/Dockerfile b/Dockerfile index ac0a3bb..8c2ed4e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ 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 +CMD ["python3", "app.py"] diff --git a/app.py b/app.py index 45e12d7..e8d2dc8 100644 --- a/app.py +++ b/app.py @@ -46,7 +46,8 @@ 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) - # TODO save `data` as a photo in `file_info.file_path` path + with open(file_info.file_path, 'wb') as new_file: + new_file.write(data) def handle_message(self, message): """Bot Main message handler""" @@ -61,14 +62,31 @@ def handle_message(self, message): class YoutubeBot(Bot): - pass + def handle_message(self, message): + if self.is_current_msg_photo(): + self.download_user_photo() + return + + videos = search_download_youtube_video(message.text, 1) + + if not videos: + text_to_send = 'Error: Could not find a youtube video matching your search.' + self.send_text_with_quote(text_to_send, message_id=message.message_id) + return + + for video in videos: + self.send_text(video['url']) if __name__ == '__main__': with open('.telegramToken') as f: _token = f.read() - my_bot = Bot(_token) - my_bot.start() + # my_bot = Bot(_token) + # my_bot.start() + # quote_bot = QuoteBot(_token) + # quote_bot.start() + youtube_bot = YoutubeBot(_token) + youtube_bot.start()