Skip to content

Commit

Permalink
Suitable downloader for milestone #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Sep 20, 2019
1 parent b8d3794 commit 3136482
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 0 deletions.
23 changes: 23 additions & 0 deletions YouTubeMDBot/__init__.py
@@ -0,0 +1,23 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .bot import PROGRAM_ARGS
from .bot import main

from .logging_utils import LoggingHandler
from .logging_utils import setup_logging

from .decorators import send_action
from .decorators import restricted
15 changes: 15 additions & 0 deletions YouTubeMDBot/__main__.py
@@ -0,0 +1,15 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
37 changes: 37 additions & 0 deletions YouTubeMDBot/bot.py
@@ -0,0 +1,37 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from telegram.ext import Updater
from telegram.ext import CommandHandler

PROGRAM_ARGS = None


def main(args: dict):
global PROGRAM_ARGS
PROGRAM_ARGS = args
updater = Updater(args["token"], workers=args["workers"])

dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("hello", main))

if args["use_webhook"]:
updater.start_webhook(listen=args["ip"],
port=args["port"],
url_path=args["token"],
webhook_url=args["public_url"] + '/' + args["token"])
else:
updater.start_polling(args["poll_interval"])
updater.idle()
24 changes: 24 additions & 0 deletions YouTubeMDBot/commands/StartHandler.py
@@ -0,0 +1,24 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .. import LoggingHandler


class StartHandler(object):
def __init__(self):
self._user_data = {}

def start(self, bot, update):
self._user_data[]
15 changes: 15 additions & 0 deletions YouTubeMDBot/commands/__init__.py
@@ -0,0 +1,15 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17 changes: 17 additions & 0 deletions YouTubeMDBot/constants/__init__.py
@@ -0,0 +1,17 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..constants.app_constants import ydl_options
from ..constants.app_constants import STREAM_OFFSET
21 changes: 21 additions & 0 deletions YouTubeMDBot/constants/app_constants.py
@@ -0,0 +1,21 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
ydl_options: dict = {
"format": "bestaudio[ext=m4a]",
"outtmpl": "-",
"logger": ""
}
STREAM_OFFSET: int = 0
17 changes: 17 additions & 0 deletions YouTubeMDBot/decorators/__init__.py
@@ -0,0 +1,17 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..decorators.decorators import restricted
from ..decorators.decorators import send_action
55 changes: 55 additions & 0 deletions YouTubeMDBot/decorators/decorators.py
@@ -0,0 +1,55 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from functools import wraps

from .. import PROGRAM_ARGS
from .. import LoggingHandler


logging = LoggingHandler()


def send_action(action):
"""
Sends an action while processing a command.
:param action: the action to be performed.
:return: itself.
"""

def decorator(func):
@wraps(func)
def command_func(update, context, *args, **kwargs):
context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action=action)
return func(update, context, *args, **kwargs)
return command_func

return decorator


def restricted(func):
"""
Restricts a specific function to be accessed from non-authorized users.
:param func: function to be wrapped.
:return: itself.
"""
@wraps(func)
def wrapped(update, context, *args, **kwargs):
user_id = update.effective_user.id
if user_id not in PROGRAM_ARGS["admin"]:
logging.warning("Unauthorized access denied for {}.".format(user_id))
return
return func(update, context, *args, **kwargs)
return wrapped
16 changes: 16 additions & 0 deletions YouTubeMDBot/downloader/__init__.py
@@ -0,0 +1,16 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..downloader.youtube_downloader import YouTubeDownloader
39 changes: 39 additions & 0 deletions YouTubeMDBot/downloader/youtube_downloader.py
@@ -0,0 +1,39 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import youtube_dl

from contextlib import redirect_stdout as save_to
from io import BytesIO

from ..constants.app_constants import ydl_options
from ..constants.app_constants import STREAM_OFFSET


class YouTubeDownloader(object):
def __init__(self, url: str,
logger: logging = logging.getLogger("empty-logger")):
self.__url: str = url
self.__options: dict = ydl_options
self.__options["logger"] = logger

def download(self, io: BytesIO = BytesIO()) -> BytesIO:
with save_to(io):
with youtube_dl.YoutubeDL(self.__options) as yt_downloader:
yt_downloader.download([self.__url])

io.seek(STREAM_OFFSET)
return io
17 changes: 17 additions & 0 deletions YouTubeMDBot/logging_utils/__init__.py
@@ -0,0 +1,17 @@
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..logging_utils.utils import LoggingHandler
from ..logging_utils.utils import setup_logging

0 comments on commit 3136482

Please sign in to comment.