Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to search and mirror #231

Open
wants to merge 2 commits into
base: v5.0
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def bot_help(update, context):

/{BotCommands.UnzipMirrorCommand} [download_url][magnet_link] : starts mirroring and if downloaded file is any archive , extracts it to google drive

/{BotCommands.SearchMirrorCommand} [query_string] : search and mirror from torrent websites to google drive

/{BotCommands.TarMirrorCommand} [download_url][magnet_link]: start mirroring and upload the archived (.tar) version of the download

/{BotCommands.WatchCommand} [youtube-dl supported link]: Mirror through youtube-dl. Click /{BotCommands.WatchCommand} for more help.
Expand Down
19 changes: 19 additions & 0 deletions bot/helper/ext_utils/torrent_search_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import requests
from bot import LOGGER

def search_torrent_link(query):
LOGGER.info("Searching torrent on query")
query = query.replace("/", "")
try:
r = requests.get(
"https://torrent-paradise.ml/api/search?q=" + query)
torrents = r.json()
torrents = sorted(torrents, key=lambda i: i['s'], reverse=True)
torrent = torrents[0]
Copy link
Owner

@lzzy12 lzzy12 May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes only one torrent is returned by the api for the query but it can return multiple. Won't it make more sense if it shows the name of the torrents to the user and let the user decide what he/she wants to mirror instead of just assuming it to be the first one from the sorted list.. Maybe bot should reply with list of buttons showing the name of the torrents and clicking on it will trigger the mirror process

except:
return "not found"
LOGGER.info("Torrent Found")
reply = ""
reply += "magnet:?xt=urn:btih:" + torrent['id']
reply += "&tr=udp://tracker.coppersurfer.tk:6969/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://tracker.leechers-paradise.org:6969/announce"
return reply
1 change: 1 addition & 0 deletions bot/helper/telegram_helper/bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class _BotCommands:
def __init__(self):
self.StartCommand = 'start'
self.MirrorCommand = 'mirror'
self.SearchMirrorCommand = "searchmirror"
self.UnzipMirrorCommand = 'unzipmirror'
self.TarMirrorCommand = 'tarmirror'
self.CancelMirror = 'cancel'
Expand Down
31 changes: 24 additions & 7 deletions bot/modules/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock
from bot.helper.ext_utils import fs_utils, bot_utils
from bot.helper.ext_utils.bot_utils import setInterval
from bot.helper.ext_utils.torrent_search_utils import search_torrent_link
from bot.helper.ext_utils.exceptions import DirectDownloadLinkException, NotSupportedExtractionArchive
from bot.helper.mirror_utils.download_utils.aria2_download import AriaDownloadHelper
from bot.helper.mirror_utils.download_utils.direct_link_generator import direct_link_generator
Expand Down Expand Up @@ -170,12 +171,20 @@ def onUploadError(self, error):
else:
update_all_messages()

def _mirror(bot, update, isTar=False, extract=False):
message_args = update.message.text.split(' ')
try:
link = message_args[1]
except IndexError:
link = ''
def _mirror(bot, update, isTar=False, extract=False, search=False):
if search:
LOGGER.info(update.message.text)
query = update.message.text.replace(BotCommands.SearchMirrorCommand,"")
link = search_torrent_link(query)
LOGGER.info(link)
if link == "not found":
link = ''
else:
message_args = update.message.text.split(' ')
try:
link = message_args[1]
except IndexError:
link = ''
LOGGER.info(link)
link = link.strip()
reply_to = update.message.reply_to_message
Expand Down Expand Up @@ -203,7 +212,10 @@ def _mirror(bot, update, isTar=False, extract=False):
else:
tag = None
if not bot_utils.is_url(link) and not bot_utils.is_magnet(link):
sendMessage('No download source provided', bot, update)
if search:
sendMessage('Provided query did\'nt return any result, \n Try Again with right name or Torrent API may be down', bot , update)
else:
sendMessage('No download source provided', bot, update)
return

try:
Expand All @@ -229,13 +241,18 @@ def tar_mirror(update, context):
def unzip_mirror(update, context):
_mirror(context.bot,update, extract=True)

def search_mirror(update, context):
_mirror(context.bot, update, search=True)

mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
search_mirror_handler = CommandHandler(BotCommands.SearchMirrorCommand, search_mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
tar_mirror_handler = CommandHandler(BotCommands.TarMirrorCommand, tar_mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
unzip_mirror_handler = CommandHandler(BotCommands.UnzipMirrorCommand, unzip_mirror,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
dispatcher.add_handler(mirror_handler)
dispatcher.add_handler(tar_mirror_handler)
dispatcher.add_handler(unzip_mirror_handler)
dispatcher.add_handler(search_mirror_handler)