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

Feature: Cli download commands aleph & ipfs #165

Closed
wants to merge 11 commits into from
38 changes: 37 additions & 1 deletion src/aleph_client/commands/files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import logging
from pathlib import Path
from typing import Optional

import typer
from aleph.sdk import AuthenticatedAlephHttpClient

from aleph.sdk import AuthenticatedAlephHttpClient, AlephClient

from aleph.sdk.account import _load_account
from aleph.sdk.conf import settings as sdk_settings
from aleph.sdk.types import AccountFromPrivateKey, StorageEnum
Expand Down Expand Up @@ -99,3 +102,36 @@ async def upload(
)
logger.debug("Upload finished")
typer.echo(f"{result.json(indent=4)}")


@app.command()
def download(
item_hash: str = typer.Argument(..., help="IPFS hash to pin on aleph.im"),
use_ipfs: Optional[bool] = typer.Option(
default=False, help="Download on IPFS client (100MB + file)"
),
path: Optional[str] = typer.Option(None, help="Path of the file to download"),
debug: bool = False,
):
"""Download a file on aleph.im."""

setup_logging(debug)

# If no path given then path == item_hash
if path is None:
path = item_hash

with AlephClient(api_server=sdk_settings.API_HOST) as client:
Copy link
Member

Choose a reason for hiding this comment

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

This is the wrong import, try using async with AlephHttpClient

try:
typer.echo("Downloading file ...")
file_path = Path(path)
with file_path.open(mode="wb") as f:
if not use_ipfs:
output_buffer = client.download_file(item_hash)
else:
output_buffer = client.download_file_ipfs(item_hash)
f.write(output_buffer)
typer.secho(f"File: '{path}' downloaded", fg=typer.colors.GREEN)

except Exception as e:
typer.secho(f"Error downloading file: {str(e)}", fg=typer.colors.RED)
3 changes: 3 additions & 0 deletions src/aleph_client/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.lexers import JsonLexer
from typer import echo
from datetime import datetime
from pathlib import Path
Copy link
Member

Choose a reason for hiding this comment

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

unneeded import



def colorful_json(obj: str):
Expand Down Expand Up @@ -122,3 +124,4 @@ def str_to_datetime(date: Optional[str]) -> Optional[datetime]:
except ValueError:
pass
return datetime.fromisoformat(date)

Copy link
Member

Choose a reason for hiding this comment

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

you can revert this file

Empty file removed test.py
Empty file.
2 changes: 1 addition & 1 deletion tests/unit/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
from pathlib import Path
from tempfile import NamedTemporaryFile

from aleph.sdk.chains.ethereum import ETHAccount
from typer.testing import CliRunner

from aleph_client.__main__ import app
import pytest

runner = CliRunner()

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test_get_message_type_value():
assert get_message_type_value(AggregateMessage) == MessageType.aggregate
assert get_message_type_value(StoreMessage) == MessageType.store
assert get_message_type_value(ProgramMessage) == MessageType.program
assert get_message_type_value(ForgetMessage) == MessageType.forget
assert get_message_type_value(ForgetMessage) == MessageType.forget
Copy link
Member

Choose a reason for hiding this comment

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

seems also unneeded? Maybe it's due to auto formatting

Loading