diff --git a/.gitignore b/.gitignore index bf0d076..740173b 100644 --- a/.gitignore +++ b/.gitignore @@ -116,7 +116,7 @@ pip-selfcheck.json .idea/dictionaries .idea/vcs.xml .idea/jsLibraryMappings.xml - +.telegramToken # Sensitive or high-churn files: .idea/dataSources.ids .idea/dataSources.xml diff --git a/Dockerfile b/Dockerfile index ac0a3bb..6644060 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ FROM python:3.8.12-slim-buster -# YOUR COMMANDS HERE -# .... -# .... +WORKDIR /TelegramAI-Naor + +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 45e12d7..29a650e 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import time import telebot from utils import search_download_youtube_video from loguru import logger @@ -41,10 +42,13 @@ 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) + with open(file_info.file_path, 'wb') as new_file: + new_file.write(data) # TODO save `data` as a photo in `file_info.file_path` path @@ -61,14 +65,26 @@ def handle_message(self, message): class YoutubeBot(Bot): - pass + def handle_message(self, message): + if self.is_current_msg_photo(): + self.download_user_photo(quality=3) + return + + if message.text == "/start": + self.send_text("welcome to the youtubeBot") + time.sleep(2) + self.send_text("in order to find a song write its name") + + if message.text is not self.is_current_msg_photo(): + link = search_download_youtube_video(message.text) + self.send_text(link[0].get("url")) + time.sleep(2) + self.send_text("if you would like another song just write it's name (: ") if __name__ == '__main__': with open('.telegramToken') as f: _token = f.read() - my_bot = Bot(_token) - my_bot.start() - - +my_bot = YoutubeBot(_token) +my_bot.start() diff --git a/requirements.txt b/requirements.txt index 1e50522..5238436 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyTelegramBotAPI -yt-dlp>=2022.6.29 +yt-dlp==2022.10.4 loguru \ No newline at end of file diff --git a/utils.py b/utils.py index e5426e0..9b6cc40 100644 --- a/utils.py +++ b/utils.py @@ -1,10 +1,10 @@ -import time + from yt_dlp import YoutubeDL def search_download_youtube_video(video_name, num_results=1): """ - This function downloads the first num_results search results from Youtube + This function downloads the first num_results search results from YouTube :param video_name: string of the video name :param num_results: integer representing how many videos to download :return: list of paths to your downloaded video files