Skip to content

Commit

Permalink
Merge 1e308f3 into 72049af
Browse files Browse the repository at this point in the history
  • Loading branch information
belikor committed Sep 4, 2021
2 parents 72049af + 1e308f3 commit 09925d5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lbry/extras/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def get_argument_parser():
group_parser = sub.add_parser(group_name, group_name=group_name, help=api['groups'][group_name])
groups[group_name] = group_parser.add_subparsers(metavar='COMMAND')

nicer_order = ['stop', 'get', 'publish', 'resolve']
nicer_order = ['stop', 'get', 'getch', 'publish', 'resolve']
for command_name in sorted(api['commands']):
if command_name not in nicer_order:
nicer_order.append(command_name)
Expand Down
92 changes: 88 additions & 4 deletions lbry/extras/daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,14 +1079,15 @@ async def jsonrpc_resolve(self, urls: typing.Union[str, list], wallet_id=None, *
@requires(WALLET_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT, BLOB_COMPONENT, DATABASE_COMPONENT,
FILE_MANAGER_COMPONENT)
async def jsonrpc_get(
self, uri, file_name=None, download_directory=None, timeout=None, save_file=None, wallet_id=None):
self, uri, file_name=None, download_directory=None, timeout=None, save_file=None, wallet_id=None,
claim_id=False, download_repost=False):
"""
Download stream from a LBRY name.
Usage:
get <uri> [<file_name> | --file_name=<file_name>]
[<download_directory> | --download_directory=<download_directory>] [<timeout> | --timeout=<timeout>]
[--save_file=<save_file>] [--wallet_id=<wallet_id>]
[--save_file=<save_file>] [--wallet_id=<wallet_id>] [--claim_id] [--download_repost]
Options:
Expand All @@ -1096,12 +1097,32 @@ async def jsonrpc_get(
--timeout=<timeout> : (int) download timeout in number of seconds
--save_file=<save_file> : (bool) save the file to the downloads directory
--wallet_id=<wallet_id> : (str) wallet to check for claim purchase receipts
--claim_id : (bool) treat <uri> as a claim_id, that is, download by claim_id
instead of canonical URL
--download_repost : (bool) resolve the claim, and if it is a repost, download the original claim
Returns: {File}
"""
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
if download_directory and not os.path.isdir(download_directory):
return {"error": f"specified download directory \"{download_directory}\" does not exist"}
return {"error": f'specified download directory "{download_directory}" does not exist'}

if claim_id or download_repost:
if claim_id:
out = await self.jsonrpc_claim_search(claim_id=uri, wallet_id=wallet_id)
if out["total_items"] < 1:
return {"error":
f'No item found with specified claim_id "{uri}"'}

txo = out["items"][-1]
uri = txo.meta["canonical_url"]
else:
out = await self.jsonrpc_resolve(uri, wallet_id=wallet_id)
txo = out[uri]

if download_repost and txo.reposted_claim:
uri = txo.reposted_claim.meta["canonical_url"]

wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
try:
stream = await self.file_manager.download_from_uri(
uri, self.exchange_rate_manager, timeout, file_name, download_directory,
Expand All @@ -1115,6 +1136,69 @@ async def jsonrpc_get(
return {"error": str(e)}
return stream

@requires(WALLET_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT, BLOB_COMPONENT, DATABASE_COMPONENT,
FILE_MANAGER_COMPONENT)
async def jsonrpc_getch(self,
channel, number=1,
download_directory=None, timeout=None, save_file=False, wallet_id=None,
download_repost=False):
"""
Download the latest claims from a LBRY channel.
Usage:
getch <channel> [<number> | --number=<number>]
[<download_directory> | --download_directory=<download_directory>] [<timeout> | --timeout=<timeout>]
[--save_file] [--wallet_id=<wallet_id>] [--download_repost]
Options:
--channel=<channel> : (str) channel from which content will be downloaded
--number=<number> : (int) number of latest claims to download from the channel
(default 1)
--download_directory=<download_directory> : (str) full path to the directory to download into
--timeout=<timeout> : (int) download timeout in number of seconds
--save_file : (bool) save the file to the downloads directory
--wallet_id=<wallet_id> : (str) wallet to check for claim purchase receipts
--download_repost : (bool) if a claim is a repost, download the original claim
Returns: {File}
"""
if not channel.startswith("@"):
channel = "@" + channel

# If the claim is a livestream it will not have 'source'
# and won't be able to be downloaded, thus we want `has_source=True`
out = await self.jsonrpc_claim_search(channel=channel,
page_size=number,
order_by="release_time",
has_source=True,
wallet_id=wallet_id)
if out["total_items"] < 1:
return {"error":
f'No items found with specified channel "{channel}"'}

txos = out["items"]

streams = []
for txo in txos:
uri = txo.meta["canonical_url"]

# file_name=None because we don't want to rename all streams
# claim_id=False because we are sure these are URLs not IDs
stream = await self.jsonrpc_get(uri, file_name=None,
download_directory=download_directory,
timeout=timeout, save_file=save_file,
wallet_id=wallet_id,
claim_id=False,
download_repost=download_repost)
if isinstance(stream, dict) and "error" in stream:
typ = txo.claim.claim_type
stream = {"error": stream["error"] + f" ({typ})" + " " + uri}
streams.append(stream)

return {"items": streams,
"total_items": len(streams)}

SETTINGS_DOC = """
Settings management.
"""
Expand Down

0 comments on commit 09925d5

Please sign in to comment.