Esempio n. 1
0
File: cli.py Progetto: ygnid/aria2p
def subcommand_add_metalinks(api: API,
                             metalink_files: List[str] = None,
                             from_file: str = None) -> int:
    """
    Add metalink subcommand.

    Arguments:
        api: The API instance to use.
        metalink_files: The paths to the metalink files.
        from_file: Path to the file to metalink files paths from.

    Returns:
        int: 0 if OK else 1.
    """
    ok = True

    if not metalink_files:
        metalink_files = []

    if from_file:
        try:
            metalink_files.extend(read_lines(from_file))
        except OSError:
            print(f"Cannot open file: {from_file}", file=sys.stderr)
            ok = False

    for metalink_file in metalink_files:
        new_downloads = api.add_metalink(metalink_file)
        for download in new_downloads:
            print(f"Created download {download.gid}")

    return 0 if ok else 1
Esempio n. 2
0
File: cli.py Progetto: ygnid/aria2p
def subcommand_add_torrents(api: API,
                            torrent_files: List[str] = None,
                            from_file: str = None) -> int:
    """
    Add torrent subcommand.

    Arguments:
        api: The API instance to use.
        torrent_files: The paths to the torrent files.
        from_file: Path to the file to read torrent files paths from.

    Returns:
        int: Always 0.
    """
    ok = True

    if not torrent_files:
        torrent_files = []

    if from_file:
        try:
            torrent_files.extend(read_lines(from_file))
        except OSError:
            print(f"Cannot open file: {from_file}", file=sys.stderr)
            ok = False

    for torrent_file in torrent_files:
        new_download = api.add_torrent(torrent_file)
        print(f"Created download {new_download.gid}")

    return 0 if ok else 1
Esempio n. 3
0
File: cli.py Progetto: ygnid/aria2p
def subcommand_add_magnets(api: API,
                           uris: List[str] = None,
                           from_file: str = None) -> int:
    """
    Add magnet subcommand.

    Arguments:
        api: The API instance to use.
        uris: The URIs of the magnets.
        from_file: Path to the file to read uris from.

    Returns:
        int: Always 0.
    """
    ok = True

    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

    for uri in uris:
        new_download = api.add_magnet(uri)
        print(f"Created download {new_download.gid}")

    return 0 if ok else 1
def add_metalinks(
    api: API,
    metalink_files: List[str] = None,
    from_file: str = None,
    options: dict = None,
    position: int = None,
) -> int:
    """
    Add metalink subcommand.

    Arguments:
        api: The API instance to use.
        metalink_files: The paths to the metalink files.
        from_file: Path to the file to metalink files paths from.
        options: String of aria2c options to add to download.
        position: Position to add new download in the queue.

    Returns:
        int: 0 if OK else 1.
    """
    ok = True

    if not metalink_files:
        metalink_files = []

    if from_file:
        try:
            metalink_files.extend(read_lines(from_file))
        except OSError:
            print(f"Cannot open file: {from_file}", file=sys.stderr)
            ok = False

    for metalink_file in metalink_files:
        new_downloads = api.add_metalink(metalink_file,
                                         options=options,
                                         position=position)
        for download in new_downloads:
            print(f"Created download {download.gid}")

    return 0 if ok else 1
Esempio n. 5
0
    def add(self,
            uri: str) -> List[Download]:  # noqa: WPS231 (not that complex)
        """
        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.

        Arguments:
            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 read_lines(path):
                    if line:
                        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
Esempio n. 6
0
def add_torrents(
    api: API,
    torrent_files: List[str] = None,
    from_file: str = None,
    options: dict = None,
    position: int = None,
) -> int:
    """
    Add torrent subcommand.

    Arguments:
        api: The API instance to use.
        torrent_files: The paths to the torrent files.
        from_file: Path to the file to read torrent files paths from.
        options: String of aria2c options to add to download.
        position: Position to add new download in the queue.

    Returns:
        int: Always 0.
    """
    ok = True

    if not torrent_files:
        torrent_files = []

    if from_file:
        try:
            torrent_files.extend(read_lines(from_file))
        except OSError:
            print(f"Cannot open file: {from_file}", file=sys.stderr)
            ok = False

    for torrent_file in torrent_files:
        new_download = api.add_torrent(torrent_file,
                                       options=options,
                                       position=position)
        print(f"Created download {new_download.gid}")

    return 0 if ok else 1