Skip to content

Commit

Permalink
Added Qbittorrent
Browse files Browse the repository at this point in the history
use /mirror qb to mirror with qbitorrent
and use /mirror qbs to select files before downloading

Signed-off-by: anas <[email protected]>
  • Loading branch information
anasty17 committed Aug 6, 2021
1 parent 82cb9b2 commit 0bfba52
Show file tree
Hide file tree
Showing 21 changed files with 874 additions and 45 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM breakdowns/mega-sdk-python:latest
FROM anasty17/megasdk:latest

WORKDIR /usr/src/app
RUN chmod 777 /usr/src/app

RUN apt-get install -y xz-utils neofetch unzip && apt-get autoremove -y

COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt

Expand Down
11 changes: 11 additions & 0 deletions alive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import time
import requests
import os
from dotenv import load_dotenv

load_dotenv('config.env')

url = os.environ.get("BASE_URL_OF_BOT")
while True:
time.sleep(1000)
status = requests.get(url).status_code
21 changes: 8 additions & 13 deletions aria.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
export MAX_DOWNLOAD_SPEED=0
tracker_list=$(curl -Ns https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/all.txt https://ngosang.github.io/trackerslist/trackers_all_http.txt https://raw.githubusercontent.com/DeSireFire/animeTrackerList/master/AT_all.txt https://raw.githubusercontent.com/hezhijie0327/Trackerslist/main/trackerslist_combine.txt | awk '$0' | tr '\n' ',')
export MAX_CONCURRENT_DOWNLOADS=7

aria2c --enable-rpc --rpc-listen-all=false --check-certificate=false \
aria2c --enable-rpc --check-certificate=false \
--max-connection-per-server=10 --rpc-max-request-size=1024M \
--bt-tracker="[$tracker_list]" --bt-max-peers=0 --bt-tracker-connect-timeout=300 --bt-stop-timeout=1200 --min-split-size=10M \
--follow-torrent=mem --split=10 \
--daemon=true --allow-overwrite=true --max-overall-download-limit=$MAX_DOWNLOAD_SPEED \
--max-overall-upload-limit=1K --max-concurrent-downloads=$MAX_CONCURRENT_DOWNLOADS \
--bt-stop-timeout=1200 --min-split-size=10M --follow-torrent=mem --split=10 \
--daemon=true --allow-overwrite=true --max-overall-download-limit=0 \
--max-overall-upload-limit=1K --max-concurrent-downloads=15 --continue=true \
--peer-id-prefix=-qB4360- --user-agent=qBittorrent/4.3.6 --peer-agent=qBittorrent/4.3.6 \
--disk-cache=64M --file-allocation=prealloc --continue=true \
--max-file-not-found=0 --max-tries=20 --auto-file-renaming=true \
--bt-enable-lpd=true --seed-time=0.01 --seed-ratio=1.0 \
--content-disposition-default-utf8=true --http-accept-gzip=true --reuse-uri=true --netrc-path=/usr/src/app/.netrc
--disk-cache=64M --bt-enable-lpd=true --seed-time=0 --max-file-not-found=0 \
--max-tries=20 --auto-file-renaming=true --reuse-uri=true --http-accept-gzip=true \
--content-disposition-default-utf8=true --netrc-path=/usr/src/app/.netrc

38 changes: 38 additions & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import string

import aria2p
import qbittorrentapi as qba
import telegram.ext as tg
from dotenv import load_dotenv
from pyrogram import Client
Expand Down Expand Up @@ -66,6 +67,18 @@ def mktable():
)
)


def get_client() -> qba.TorrentsAPIMixIn:
qb_client = qba.Client(host="localhost", port=8090, username="admin", password="adminadmin")
try:
qb_client.auth_log_in()
qb_client.application.set_preferences({"disk_cache":64, "incomplete_files_ext":True, "max_connec":3000, "max_connec_per_torrent":300, "async_io_thread":32})
return qb_client
except qba.LoginFailed as e:
LOGGER.error(str(e))
return None


DOWNLOAD_DIR = None
BOT_TOKEN = None

Expand Down Expand Up @@ -313,6 +326,31 @@ def mktable():
except KeyError:
pass

try:
BASE_URL = getConfig('BASE_URL_OF_BOT')
if len(BASE_URL) == 0:
BASE_URL = None
except KeyError:
logging.warning('BASE_URL_OF_BOT not provided!')
BASE_URL = None

try:
IS_VPS = getConfig('IS_VPS')
if IS_VPS.lower() == 'true':
IS_VPS = True
else:
IS_VPS = False
except KeyError:
IS_VPS = False

try:
SERVER_PORT = getConfig('SERVER_PORT')
if len(SERVER_PORT) == 0:
SERVER_PORT = None
except KeyError:
logging.warning('SERVER_PORT not provided!')
SERVER_PORT = None

updater = tg.Updater(token=BOT_TOKEN)
bot = updater.bot
dispatcher = updater.dispatcher
8 changes: 7 additions & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import shutil, psutil
import signal
import os
import asyncio

from pyrogram import idle
from bot import app
from sys import executable

from telegram import ParseMode
from telegram.ext import CommandHandler
from bot import bot, dispatcher, updater, botStartTime, IGNORE_PENDING_REQUESTS
from wserver import start_server_async
from bot import bot, dispatcher, updater, botStartTime, IGNORE_PENDING_REQUESTS, IS_VPS, SERVER_PORT
from bot.helper.ext_utils import fs_utils
from bot.helper.telegram_helper.bot_commands import BotCommands
from bot.helper.telegram_helper.message_utils import *
Expand Down Expand Up @@ -201,6 +203,10 @@ def bot_help(update, context):

def main():
fs_utils.start_cleanup()

if IS_VPS:
asyncio.get_event_loop().run_until_complete(start_server_async(SERVER_PORT))

# Check if the bot is restarting
if os.path.isfile(".restartmsg"):
with open(".restartmsg") as f:
Expand Down
16 changes: 16 additions & 0 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MirrorStatus:
STATUS_CLONING = "Cloning...♻️"
STATUS_WAITING = "Queued...📝"
STATUS_FAILED = "Failed 🚫. Cleaning Download..."
STATUS_PAUSE = "Paused...⭕️"
STATUS_ARCHIVING = "Archiving...🔐"
STATUS_EXTRACTING = "Extracting...📂"

Expand Down Expand Up @@ -76,6 +77,7 @@ def getDownloadByGid(gid):
return dl
return None


def getAllDownload():
with download_dict_lock:
for dlDetails in list(download_dict.values()):
Expand All @@ -84,6 +86,7 @@ def getAllDownload():
return dlDetails
return None


def get_progress_bar_string(status):
completed = status.processed_bytes() / 8
total = status.size_raw() / 8
Expand Down Expand Up @@ -134,6 +137,11 @@ def get_readable_message():
f" | <b>Peers:</b> {download.aria_download().connections}"
except:
pass
try:
msg += f"\n<b>Seeders:</b> {download.torrent_info().num_seeds}" \
f" | <b>Leechers:</b> {download.torrent_info().num_leechs}"
except:
pass
msg += f"\n<b>To Stop:</b> <code>/{BotCommands.CancelMirror} {download.gid()}</code>"
msg += "\n\n"
if STATUS_LIMIT is not None:
Expand All @@ -151,6 +159,7 @@ def get_readable_message():
return msg, button
return msg, ""


def flip(update, context):
query = update.callback_query
query.answer()
Expand All @@ -171,6 +180,7 @@ def flip(update, context):
PAGE_NO -= 1
message_utils.update_all_messages()


def get_readable_time(seconds: int) -> str:
result = ''
(days, remainder) = divmod(seconds, 86400)
Expand All @@ -196,12 +206,15 @@ def is_url(url: str):
return True
return False


def is_gdrive_link(url: str):
return "drive.google.com" in url


def is_mega_link(url: str):
return "mega.nz" in url or "mega.co.nz" in url


def get_mega_link_type(url: str):
if "folder" in url:
return "folder"
Expand All @@ -211,12 +224,14 @@ def get_mega_link_type(url: str):
return "folder"
return "file"


def is_magnet(url: str):
magnet = re.findall(MAGNET_REGEX, url)
if magnet:
return True
return False


def new_thread(fn):
"""To use as decorator to make a function call threaded.
Needs import
Expand All @@ -229,6 +244,7 @@ def wrapper(*args, **kwargs):

return wrapper


next_handler = CallbackQueryHandler(flip, pattern="nex", run_async=True)
previous_handler = CallbackQueryHandler(flip, pattern="pre", run_async=True)
dispatcher.add_handler(next_handler)
Expand Down
3 changes: 2 additions & 1 deletion bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from bot import aria2, LOGGER, DOWNLOAD_DIR
from bot import aria2, LOGGER, DOWNLOAD_DIR, get_client
import shutil
import os
import pathlib
Expand All @@ -23,6 +23,7 @@ def start_cleanup():

def clean_all():
aria2.remove_all(True)
get_client().torrents_delete(torrent_hashes="all", delete_files=True)
try:
shutil.rmtree(DOWNLOAD_DIR)
except FileNotFoundError:
Expand Down
3 changes: 3 additions & 0 deletions bot/helper/mirror_utils/download_utils/mega_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def add_download(mega_link: str, path: str, listener):
if smsg:
msg1 = "File/Folder is already available in Drive.\nHere are the search results:"
sendMarkup(msg1, listener.bot, listener.update, button)
executor.continue_event.set()
return
if MEGA_LIMIT is not None or TAR_UNZIP_LIMIT is not None:
limit = None
Expand All @@ -193,10 +194,12 @@ def add_download(mega_link: str, path: str, listener):
if 'G' in limit[1] or 'g' in limit[1]:
if api.getSize(node) > limitint * 1024**3:
sendMessage(msg3, listener.bot, listener.update)
executor.continue_event.set()
return
elif 'T' in limit[1] or 't' in limit[1]:
if api.getSize(node) > limitint * 1024**4:
sendMessage(msg3, listener.bot, listener.update)
executor.continue_event.set()
return
with download_dict_lock:
download_dict[listener.uid] = MegaDownloadStatus(mega_listener, listener)
Expand Down
Loading

0 comments on commit 0bfba52

Please sign in to comment.