Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
13 changes: 9 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from utils import search_download_youtube_video
from loguru import logger


class Bot:

def __init__(self, token):
Expand Down Expand Up @@ -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"""
Expand All @@ -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()


15 changes: 13 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from yt_dlp import YoutubeDL

import os

def search_download_youtube_video(video_name, num_results=1):
"""
Expand All @@ -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