Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Release 4.0.0

See merge request namibsun/python/xdcc-dl!5
  • Loading branch information
namboy94 committed Oct 13, 2019
2 parents c49fdfc + 59b5942 commit fedca96
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 333 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
V 4.0.0:
- Went over to completely rely on python's built-in logging
- Throttle value is now passed as parameter
- Logging to files now active by default
V 3.3.0:
- Added download speed progress
- Fixed slow download speeds
Expand Down
52 changes: 15 additions & 37 deletions bin/xdcc-browse
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,54 @@ You should have received a copy of the GNU General Public License
along with xdcc-dl. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import logging
import argparse
from typing import List
from requests.exceptions import ConnectionError
from puffotter.init import cli_start, argparse_add_verbosity
from puffotter.prompt import selection_prompt
from xdcc_dl import sentry_dsn
from xdcc_dl.xdcc.exceptions import DownloadIncomplete
from xdcc_dl.helper import set_throttle_value, set_logging_level, prepare_packs
from xdcc_dl.helper import prepare_packs, add_xdcc_argparse_arguments
from xdcc_dl.entities import XDCCPack
from xdcc_dl.logging import Logger
from xdcc_dl.xdcc import download_packs
from xdcc_dl.pack_search.SearchEngine import SearchEngineType


def main(args: argparse.Namespace):
def main(args: argparse.Namespace, logger: logging.Logger):
"""
Conducts a XDCC pack search with the option to immediately download any
found packs
:param args: The command line arguments
:param logger: The logger to use
:return: None
"""
try:
set_logging_level(args.quiet, args.verbose, args.debug, args.silent)
set_throttle_value(args.throttle)

search_engine = SearchEngineType.resolve(args.search_engine)

results = search_engine.search(args.search_term)
for i, result in enumerate(results):
Logger().print("{}: {}".format(i + 1, result))

selection = input("Enter the packs to download: ")
parsepacks = XDCCPack.from_xdcc_message(
"/msg A xdcc send #{}".format(selection)
)

packs = []
for pack in parsepacks: # type: XDCCPack
packs.append(results[pack.packnumber - 1])
# noinspection PyTypeChecker
packs = selection_prompt(results) # type: List[XDCCPack]
prepare_packs(packs, args.out)

for pack in packs:
Logger().info("Downloading pack {}".format(pack))
logger.info("Downloading pack {}".format(pack))

download_packs(
packs,
timeout=args.timeout,
fallback_channel=args.fallback_channel
fallback_channel=args.fallback_channel,
throttle=args.throttle
)

except ConnectionError:
print("Connection Error, could not conduct search")
except DownloadIncomplete:
Logger().warning("Download incomplete.")
logger.warning("Download incomplete.")
raise KeyboardInterrupt()
except ValueError:
print("Invalid throttle value {}".format(args.throttle))


if __name__ == "__main__":
Expand All @@ -80,24 +75,7 @@ if __name__ == "__main__":
default=SearchEngineType.HORRIBLESUBS.name.lower(),
choices=SearchEngineType.choices(True),
help="The Search Engine to use")
parser.add_argument("-s", "--server",
default="irc.rizon.net",
help="Specifies the IRC Server. "
"Defaults to irc.rizon.net")
parser.add_argument("-o", "--out",
help="Specifies the target file. "
"Defaults to the pack's file name. "
"When downloading multiple packs, index "
"numbers will be appended to the filename")
parser.add_argument("-t", "--throttle",
help="Limits the download speed of xdcc-dl. "
"Append K,M or G for more convenient units")
parser.add_argument("--timeout", default=120, type=int,
help="Sets a timeout for starting the download")
parser.add_argument("--silent", action="store_true",
help="Disables all print output")
parser.add_argument("--fallback-channel",
help="Fallback channel in case ")
add_xdcc_argparse_arguments(parser)
argparse_add_verbosity(parser)
cli_start(
main, parser,
Expand Down
36 changes: 10 additions & 26 deletions bin/xdcc-dl
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@ along with xdcc-dl. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import os
import logging
import argparse
from puffotter.init import cli_start, argparse_add_verbosity
from xdcc_dl import sentry_dsn
from xdcc_dl.xdcc import download_packs
from xdcc_dl.helper import set_throttle_value, set_logging_level, prepare_packs
from xdcc_dl.helper import prepare_packs, add_xdcc_argparse_arguments
from xdcc_dl.entities import XDCCPack
from xdcc_dl.logging import Logger
from xdcc_dl.xdcc.exceptions import DownloadIncomplete


def main(args: argparse.Namespace):
def main(args: argparse.Namespace, logger: logging.Logger):
"""
Starts the main method of the program
:param args: The command line arguments
:param logger: The logger to use
:return: None
"""
try:
set_throttle_value(args.throttle)
set_logging_level(args.quiet, args.verbose, args.debug, args.silent)

packs = XDCCPack.from_xdcc_message(
args.message, os.getcwd(), args.server
)
Expand All @@ -47,12 +45,15 @@ def main(args: argparse.Namespace):
download_packs(
packs,
timeout=args.timeout,
fallback_channel=args.fallback_channel
fallback_channel=args.fallback_channel,
throttle=args.throttle
)

except DownloadIncomplete:
Logger().warning("Download incomplete.")
logger.warning("Download incomplete.")
raise KeyboardInterrupt()
except ValueError:
print("Invalid throttle value {}".format(args.throttle))


if __name__ == "__main__":
Expand All @@ -61,25 +62,8 @@ if __name__ == "__main__":
help="An XDCC Message. Supports ranges (1-100), "
"ranges with steps (1-100;2) as well as "
"comma-separated packs: (1,2,3).")
parser.add_argument("-s", "--server",
default="irc.rizon.net",
help="Specifies the IRC Server. "
"Defaults to irc.rizon.net")
parser.add_argument("-o", "--out",
help="Specifies the target file. "
"Defaults to the pack's file name. "
"When downloading multiple packs, index "
"numbers will be appended to the filename")
parser.add_argument("-t", "--throttle",
help="Limits the download speed of xdcc-dl. "
"Append K,M or G for more convenient units")
parser.add_argument("--timeout", default=120, type=int,
help="Sets a timeout for starting the download")
parser.add_argument("--fallback-channel",
help="Fallback channel in case ")
add_xdcc_argparse_arguments(parser)
argparse_add_verbosity(parser)
parser.add_argument("--silent", action="store_true",
help="Disables all print output")
cli_start(
main, parser,
sentry_dsn=sentry_dsn,
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0
4.0.0
70 changes: 26 additions & 44 deletions xdcc_dl/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,9 @@
along with xdcc-dl. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import sys
import logging
from argparse import ArgumentParser
from typing import List, Optional
from xdcc_dl.logging import Logger
from xdcc_dl.entities.XDCCPack import XDCCPack
from xdcc_dl.xdcc.XDCCClient import XDCCClient
from puffotter.units import byte_string_to_byte_count


def set_throttle_value(throttle_string: str):
"""
Sets the throttle value of the XDCC Client globally based on a string in
the form <Bytes><|k|m|g> (kilo, mega, giga)
:param throttle_string: The string to parse
:return: None
"""
try:
if throttle_string is not None:
limit = byte_string_to_byte_count(throttle_string)
XDCCClient.download_limit = limit
except ValueError:
print("Invalid throttle value")
sys.exit(1)


def set_logging_level(quiet: bool, verbose: bool, debug: bool, silent: bool):
"""
Sets the logging level based on a combination of flags
If all flags are False, the logging level will be set to WARNING
:param quiet: If set to True, will set logging to ERROR
:param verbose: If set to True, will set logging to INFO
:param debug: If set to True, will set logging to DEBUG
:param silent: If set to True, will disable ALL printing
:return: None
"""
if silent:
Logger.logging_level = -1
elif quiet:
Logger.logging_level = logging.ERROR
elif verbose:
Logger.logging_level = logging.INFO
elif debug:
Logger.logging_level = logging.DEBUG
else:
Logger.logging_level = logging.WARNING


def prepare_packs(packs: List[XDCCPack], location: Optional[str]):
Expand All @@ -71,11 +29,35 @@ def prepare_packs(packs: List[XDCCPack], location: Optional[str]):
:param packs: The packs to prepare
:return: None
"""

if location is not None:
if len(packs) == 1:
packs[0].set_filename(location, True)
else:
# Generate unique names for each pack file
for i, pack in enumerate(packs):
pack.set_filename(location + "-" + str(i).zfill(3), True)


def add_xdcc_argparse_arguments(parser: ArgumentParser):
"""
Adds relevant command line arguments for an argument parser for xdcc-dl
:param parser: The parser to modify
:return: None
"""
parser.add_argument("-s", "--server",
default="irc.rizon.net",
help="Specifies the IRC Server. "
"Defaults to irc.rizon.net")
parser.add_argument("-o", "--out",
help="Specifies the target file. "
"Defaults to the pack's file name. "
"When downloading multiple packs, index "
"numbers will be appended to the filename")
parser.add_argument("-t", "--throttle", default=-1,
help="Limits the download speed of xdcc-dl. "
"Append K,M or G for more convenient units")
parser.add_argument("--timeout", default=120, type=int,
help="Sets a timeout for starting the download")
parser.add_argument("--fallback-channel",
help="Fallback channel in case a channel could not"
"be joined automatically using WHOIS commands")
Loading

0 comments on commit fedca96

Please sign in to comment.