Skip to content

Commit

Permalink
Separated telegram related code from download upload related code
Browse files Browse the repository at this point in the history
Signed-off-by: lzzy12 <[email protected]>
  • Loading branch information
lzzy12 committed Sep 26, 2019
1 parent d6b6933 commit 1c56b18
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 307 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ config.ini
downloads/*
download/*
data*
.vscode
.vscode
.idea
*.json
*.pickle
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a telegram bot writen in python for mirroring files on the internet to our beloved Google Drive.\n
Documentation is coming as soon as we finish implementing all the basic features of the bot
2 changes: 1 addition & 1 deletion aria.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aria2c --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true --allow-overwrite=true
aria2c --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true --allow-overwrite=true
37 changes: 23 additions & 14 deletions bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import logging
import configparser
from telegram.ext import Updater

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

level=logging.INFO)

config = configparser.ConfigParser()
config.read('bot/config.ini')


def getConfig(name: str):
return config['DEFAULT'][name]

return config['DEFAULT'][name]


LOGGER = logging.getLogger(__name__)

try:
Expand All @@ -21,13 +20,23 @@ def getConfig(name: str):
exit()
except KeyError:
pass

BOT_TOKEN = getConfig('BOT_TOKEN')
CLIENT_ID = getConfig('G_DRIVE_CLIENT_ID')
CLIENT_SECRET = getConfig('G_DRIVE_CLIENT_SECRET')
parent_id = getConfig('GDRIVE_FOLDER_ID')
DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR')
if DOWNLOAD_DIR[-1] != '/' or DOWNLOAD_DIR[-1] != '\\':
DOWNLOAD_DIR = DOWNLOAD_DIR + '/'

DOWNLOAD_DIR = None
BOT_TOKEN = None
try:
BOT_TOKEN = getConfig('BOT_TOKEN')
CLIENT_ID = getConfig('G_DRIVE_CLIENT_ID')
CLIENT_SECRET = getConfig('G_DRIVE_CLIENT_SECRET')
parent_id = getConfig('GDRIVE_FOLDER_ID')
DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR')
if DOWNLOAD_DIR[-1] != '/' or DOWNLOAD_DIR[-1] != '\\':
DOWNLOAD_DIR = DOWNLOAD_DIR + '/'
DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL'))
download_list = {}

except KeyError as e:
LOGGER.error("One or more env variables missing! Exiting now")
exit(1)

updater = Updater(token=BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher = updater.dispatcher
13 changes: 9 additions & 4 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
from bot import dispatcher, LOGGER, updater
import bot.mirror


@run_async
def start(update, context):
print(update)
context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
print(update)
context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")


LOGGER.info('__main__.py')


def main():
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
LOGGER.info("Bot Started!")
updater.start_polling()

main()


main()
3 changes: 2 additions & 1 deletion bot/config_sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ BOT_TOKEN =
G_DRIVE_CLIENT_ID =
G_DRIVE_CLIENT_SECRET =
GDRIVE_FOLDER_ID =
DOWNLOAD_DIR = ./download
DOWNLOAD_DIR = ./download
DOWNLOAD_STATUS_UPDATE_INTERVAL = 5
94 changes: 0 additions & 94 deletions bot/helper/ariaTools.py

This file was deleted.

8 changes: 8 additions & 0 deletions bot/helper/download_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Message:
pass

class Download:
def __init__(self, gid, download):
self.isDownloading = download.is_complete
self.gid = gid
self.download = download
85 changes: 85 additions & 0 deletions bot/helper/download_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import aria2p
from time import sleep
from bot import LOGGER, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_list
from bot.helper.listeners import *
from urllib.parse import urlparse
from aria2p import Options


def is_url(url: str):
try:
urlparse(url)
return True
except ValueError:
return False


def is_magnet(url: str):
if "magnet" in url:
return True
else:
return False


class DownloadHelper:

def __init__(self, listener: MirrorListeners):
self.__aria2 = aria2p.API(
aria2p.Client(
host="http://localhost",
port=6800,
secret="",
)
)
self.__listener = listener

def add_download(self, link: str):
download = None
if is_url(link):
download = self.__aria2.add_uris([link], {'dir': DOWNLOAD_DIR + str(self.__listener.update.update_id),
'max_download_limit': 1})
elif is_magnet(link):
download = self.__aria2.add_magnet(link, {'dir': DOWNLOAD_DIR})
else:
self.__listener.onDownloadError("No download URL or URL malformed")
return
download_list[self.__listener.update.update_id] = download.gid
self.__listener.onDownloadStarted(link)
self.__update_download_status()

def get_downloads_status_str_list(self):
"""
Generates a human readable string of progress of all the downloads
:return: list of strings of progress of all the downloads
"""
str_list = []
LOGGER.info(download_list)
for gid in list(download_list.values()):
download = self.__aria2.get_download(gid)
str_list.append("<b>" + download.name + "</b>:- "
+ download.progress_string() + " of "
+ download.total_length_string()
+ " at " + download.download_speed_string()
+ " ,ETA: " + download.eta_string()
)
return str_list

def __get_download(self):
return self.__aria2.get_download(download_list[self.__listener.update.update_id])

def __update_download_status(self):
# TODO: Try to find a way to move this code to mirror.py and send only a
# list of Download objects to onDownloadProgress()
previous = None
LOGGER.info(self.get_downloads_status_str_list())
while not self.__get_download().is_complete:
if self.__get_download().has_failed:
self.__listener.onDownloadError(self.__get_download().error_message)
break
progress_str_list = self.get_downloads_status_str_list()
if progress_str_list != previous:
self.__listener.onDownloadProgress(progress_str_list)
previous = progress_str_list
sleep(DOWNLOAD_STATUS_UPDATE_INTERVAL)

self.__listener.onDownloadComplete(self.__get_download())
Loading

0 comments on commit 1c56b18

Please sign in to comment.