diff --git a/Dockerfile b/Dockerfile index ac0a3bb..7e57bdf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ FROM python:3.8.12-slim-buster -# YOUR COMMANDS HERE -# .... -# .... +WORKDIR /app + +COPY . /app/ + +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..36a0906 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,6 @@ from utils import search_download_youtube_video from loguru import logger - class Bot: def __init__(self, token): @@ -46,7 +45,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 user_photo: + user_photo.write(data) def handle_message(self, message): """Bot Main message handler""" @@ -61,14 +61,19 @@ def handle_message(self, message): class YoutubeBot(Bot): - pass + def handle_message(self, message): + if self.is_current_msg_photo(): + self.download_user_photo(3) + return + youtube_video_link = search_download_youtube_video(message.text) + self.send_text(youtube_video_link[0]["url"]) if __name__ == '__main__': with open('.telegramToken') as f: _token = f.read() - my_bot = Bot(_token) + my_bot = YoutubeBot(_token) my_bot.start() diff --git a/utils.py b/utils.py index e5426e0..b258d4c 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,6 @@ import time from yt_dlp import YoutubeDL - +import os def search_download_youtube_video(video_name, num_results=1): """ @@ -11,14 +11,25 @@ def search_download_youtube_video(video_name, num_results=1): """ results = [] with YoutubeDL() as ydl: - videos = ydl.extract_info(f"ytsearch{num_results}:{video_name}", download=True)['entries'] + videos = ydl.extract_info(f"ytsearch{num_results}:{video_name}", download=False)['entries'] + + file_exists = False for video in videos: + for file in os.listdir(): + if file == ydl.prepare_filename(video): + file_exists = True + break + results.append({ 'filename': ydl.prepare_filename(video), 'video_id': video['id'], 'title': video['title'], 'url': video['webpage_url'] }) + + if file_exists == False: + videos = ydl.extract_info(f"ytsearch{num_results}:{video_name}", download=True)['entries'] + return results