From 915fb87df137b8fec43dd3ba29e87de0aba7f94b Mon Sep 17 00:00:00 2001 From: thezero Date: Sat, 19 Jun 2021 20:45:58 +0200 Subject: [PATCH] docker --- Dockerfile | 5 +++ docker-compose.yml | 11 +++++++ main.py | 2 +- requirements.txt | 1 + db.py => src/db.py | 0 src/main.py | 55 ++++++++++++++++++++++++++++++++ vid_utils.py => src/vid_utils.py | 2 +- 7 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 requirements.txt rename db.py => src/db.py (100%) create mode 100644 src/main.py rename vid_utils.py => src/vid_utils.py (97%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9f77fae --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8-slim-buster +WORKDIR /bot +COPY requirements.txt . +RUN pip3 install -r requirements.txt +COPY src src \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e16d62e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3.3" +services: + bot: + build: . + command: python3 /bot/src/main.py + volumes: + - ./out:/bot/out + - ./conf:/bot/conf + environment: + - CONF_FOLDER=/bot/conf/ + user: 1000:1000 \ No newline at end of file diff --git a/main.py b/main.py index cf650b1..9120b1c 100644 --- a/main.py +++ b/main.py @@ -42,7 +42,7 @@ def download_choosen_format(update, context): message_id=query.message.message_id) TOKEN = None -with open('bot.token') as f: +with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f: TOKEN = f.read().strip() updater = Updater(token=TOKEN) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..964d1d5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +python-telegram-bot==13.0 \ No newline at end of file diff --git a/db.py b/src/db.py similarity index 100% rename from db.py rename to src/db.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..9120b1c --- /dev/null +++ b/src/main.py @@ -0,0 +1,55 @@ +import logging +import os + +from telegram import InlineKeyboardMarkup +from telegram.ext import Updater, CallbackQueryHandler, MessageHandler, Filters + +from vid_utils import Video, BadLink + +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) +logger = logging.getLogger(__name__) + + +def get_format(update, context): + logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history" + + try: + video = Video(link=update.message.text, init_keyboard=True) + except BadLink as e: + update.message.reply_text("Bad link: {}".format(e)) + else: + reply_markup = InlineKeyboardMarkup(video.keyboard) + update.message.reply_text('Choose format:', reply_markup=reply_markup) + + +def download_choosen_format(update, context): + query = update.callback_query + video_index = query.data + + context.bot.edit_message_text(text="Downloading...", + chat_id=query.message.chat_id, + message_id=query.message.message_id) + + video = Video(vid=video_index) + video.download() + + with video.send() as files: + for f in files: + logger.log("Sending... {} ".format(f)) + context.bot.send_document(chat_id=query.message.chat_id, document=open(f, 'rb')) + + context.bot.delete_message(chat_id=query.message.chat_id, + message_id=query.message.message_id) + +TOKEN = None +with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f: + TOKEN = f.read().strip() + +updater = Updater(token=TOKEN) + +updater.dispatcher.add_handler(MessageHandler(Filters.text, get_format)) +updater.dispatcher.add_handler(CallbackQueryHandler(download_choosen_format)) + + +updater.start_polling() +updater.idle() diff --git a/vid_utils.py b/src/vid_utils.py similarity index 97% rename from vid_utils.py rename to src/vid_utils.py index 3325927..5022eb2 100644 --- a/vid_utils.py +++ b/src/vid_utils.py @@ -13,7 +13,7 @@ class BadLink(Exception): class Video: def __init__(self, link=None, vid=None, init_keyboard=False): - self.db = VidDatabase("viddb.sqlite3") + self.db = VidDatabase(os.path.join(os.environ['CONF_FOLDER'], "viddb.sqlite3")) if not self.db.is_valid: # Database file not present # Create a new database