Skip to content

Commit

Permalink
refactor: Move add command logic into API
Browse files Browse the repository at this point in the history
In this refactor, we move the logic of the CLI `add` subcommand
into the API class, for better reusability.

We also deprecate the `-f` or `--from-file` argument,
and automatically treat every valid file-path that is not a torrent
nor a metalink as a source file (to read URIs from), recursively.
  • Loading branch information
pawamoy committed Sep 30, 2020
1 parent f24660b commit 6f36116
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
38 changes: 38 additions & 0 deletions src/aria2p/api.py
Expand Up @@ -43,6 +43,44 @@ def __init__(self, client=None) -> None:
def __repr__(self):
return f"API({self.client!r})"

def add(self, uri: str) -> List[Download]:
"""
Add a download (guess its type).
If the provided URI is in fact a file-path, and is neither a torrent or a metalink,
then we read its lines and try to add each line as a download, recursively.
Parameters:
uri: The URI or file-path to add.
Returns:
The created downloads.
"""
new_downloads = []
path = Path(uri)

# On Windows, path.exists() generates an OSError when path is an URI
# See https://github.com/pawamoy/aria2p/issues/41
try:
path_exists = path.exists()
except OSError:
path_exists = False

if path_exists:
if path.suffix == ".torrent":
new_downloads.append(self.add_torrent(path))
elif path.suffix == ".metalink":
new_downloads.extend(self.add_metalink(path))
else:
for line in path.read_text().split("\n"):
new_downloads.extend(self.add(line))
elif uri.startswith("magnet:?"):
new_downloads.append(self.add_magnet(uri))
else:
new_downloads.append(self.add_uris([uri]))

return new_downloads

def add_magnet(self, magnet_uri: str, options: Union[Options, dict] = None, position: int = None) -> Download:
"""
Add a download with a Magnet URI.
Expand Down
53 changes: 17 additions & 36 deletions src/aria2p/cli.py
Expand Up @@ -392,53 +392,34 @@ def subcommand_add(api: API, uris: List[str] = None, from_file: str = None) -> i
Add magnet subcommand.
Parameters:
api: the API instance to use.
uris: the URIs or file-paths to add.
from_file: path to the file to read uris from.
api: The API instance to use.
uris: The URIs or file-paths to add.
from_file: Path to the file to read uris from.
Deprecated: every URI that is a valid file-path
and is not a torrent or a metalink is now read as an input file.
Returns:
int: 0 if OK else 1.
"""
ok = True
uris = uris or []

if not uris:
uris = []
if from_file:
try:
uris.extend(read_lines(from_file))
except OSError:
print(f"Cannot open file: {from_file}", file=sys.stderr)
ok = False
logger.warning(
"Deprecation warning: every URI that is a valid file-path "
"and is not a torrent or a metalink is now read as an input file."
)

new_downloads = []
for uri in uris:
path = Path(uri)

# On Windows, path.exists() generates an OSError when path is an URI
# See https://github.com/pawamoy/aria2p/issues/41
try:
path_exists = path.exists()
except OSError:
path_exists = False

if path_exists:
if path.suffix == ".torrent":
new_downloads = [api.add_torrent(path)]
elif path.suffix == ".metalink":
new_downloads = api.add_metalink(path)
else:
print(f"Cannot determine type of file {path}", file=sys.stderr)
print(f" Known extensions are .torrent and .metalink", file=sys.stderr)
ok = False
continue
elif uri.startswith("magnet:?"):
new_downloads = [api.add_magnet(uri)]
else:
new_downloads = [api.add_uris([uri])]

new_downloads.extend(api.add(uri))

if new_downloads:
for new_download in new_downloads:
print(f"Created download {new_download.gid}")
return 0

return 0 if ok else 1
print("No new download was created", file=sys.stderr)
return 1


def subcommand_add_magnets(api: API, uris: List[str] = None, from_file: str = None) -> int:
Expand Down

0 comments on commit 6f36116

Please sign in to comment.