-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprint.py
295 lines (241 loc) · 10.1 KB
/
print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
# --------------------------------------------------------------------------- #
# The MIT License (MIT) #
# #
# Copyright (c) 2021 Eliud Cabrera Castillo <[email protected]> #
# #
# Permission is hereby granted, free of charge, to any person obtaining #
# a copy of this software and associated documentation files #
# (the "Software"), to deal in the Software without restriction, including #
# without limitation the rights to use, copy, modify, merge, publish, #
# distribute, sublicense, and/or sell copies of the Software, and to permit #
# persons to whom the Software is furnished to do so, subject to the #
# following conditions: #
# #
# The above copyright notice and this permission notice shall be included #
# in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# --------------------------------------------------------------------------- #
"""Functions to print information about the claims in the LBRY network."""
import time
import lbrytools.funcs as funcs
def print_info_pre_get(claim=None, offline=False,
print_text=True):
"""Get and print information about the item found in the LBRY network.
Parameters
----------
claim: dict
A dictionary with the information of an item, obtained
from `search_item`.
::
item = search_item(uri="some-video-name")
offline: bool, optional
It defaults to `False`, in which case it assumes the input `claim` was
resolved online through `lbrynet resolve` or `lbrynet claim search`.
If it is `True` it assumes the `claim` was resolved offline
through `lbrynet file list`, that is, in the downloaded content.
This is required for 'invalid' claims, which have been removed from
the online database but may still exist locally.
print_text: bool, optional
It defaults to `True`, in which case it will print
the summary of the claim to the terminal.
If it is `False` it will just return the summary text
but it won't be printed.
Returns
-------
str
A paragraph of text with information from the claim
that can be directly printed to the terminal or to another interface.
False
If there is a problem or no item, it will return `False`.
"""
if not claim:
print("Error: no item. Get one item with `search_item(uri)`")
return False
if offline:
value = claim["metadata"]
else:
value = claim["value"]
claimid = claim["claim_id"]
if offline:
address = 14 * "_"
else:
address = claim.get("address")
title = value.get("title", "(no title)")
if offline:
create_time = 14 * "_"
else:
create_time = claim["meta"].get("creation_timestamp", 0)
create_time = time.strftime(funcs.TFMT, time.gmtime(create_time))
rels_time = int(value.get("release_time", 0))
if not rels_time:
rels_time = 14 * "_"
else:
rels_time = time.strftime(funcs.TFMT, time.gmtime(rels_time))
if offline:
amount = 8 * "_"
else:
amount = claim["amount"]
if offline:
effective = 8 * "_"
else:
effective = claim["meta"].get("effective_amount")
if offline:
vtype = "stream"
else:
vtype = claim["value_type"]
stream_type = value.get("stream_type", 14 * "_")
mtype = 14 * "_"
if offline:
mtype = claim["mime_type"]
else:
if "source" in value and "media_type" in value["source"]:
mtype = value["source"]["media_type"]
seconds = 0
if "video" in value and "duration" in value["video"]:
seconds = value["video"]["duration"]
if "audio" in value and "duration" in value["audio"]:
seconds = value["audio"]["duration"]
sec = seconds % 60 # remainder
mi = seconds // 60 # integer part
duration = f"{mi} min {sec} s"
size = 0
if "source" in value and "size" in value["source"]:
size = float(value["source"]["size"])
size_mb = size / (1024**2)
if offline:
info = ["claim_name: " + claim["claim_name"]]
else:
info = ["canonical_url: " + claim["canonical_url"]]
info2 = [f"claim_id: {claimid}",
f"address: {address}",
f"title: {title}",
f"creation_timestamp: {create_time}",
f"release_time: {rels_time}",
f"amount: {amount}",
f"effective_amount: {effective}",
f"value_type: {vtype}",
f"stream_type: {stream_type}",
f"media_type: {mtype}",
f"duration: {duration}",
f"size: {size_mb:.4f} MB"]
info.extend(info2)
summary = "\n".join(info)
if print_text:
print(summary)
return summary
def print_info_post_get(info_get=None):
"""Print information about the downloaded item from lbrynet get.
Parameters
----------
info_get: dict
A dictionary with the information obtained from downloading
a claim.
::
info_get = lbrynet_get(get_cmd)
Returns
-------
bool
It returns `True` if the information was read and printed
without problems.
If there is a problem or no item, it will return `False`.
"""
if not info_get:
print("Error: no item information. "
"Get the information with `lbrynet_get(cmd)`")
return False
err = False
if "error" in info_get:
print(">>> Error: " + info_get["error"])
err = True
elif not info_get["blobs_in_stream"]:
# In certain cases the information does not have blobs in the stream
# nor download path. This may need to be checked if it causes
# an error that cannot be handled later.
# elif (not info_get["blobs_in_stream"]
# or not info_get["download_path"]):
# print(info_get)
print(">>> Error in downloading claim, "
f"blobs_in_stream={info_get['blobs_in_stream']}, "
f"download_path={info_get['download_path']}")
err = True
if not err:
print("blobs_completed: {}".format(info_get["blobs_completed"]))
print("blobs_in_stream: {}".format(info_get["blobs_in_stream"]))
print("download_path: {}".format(info_get["download_path"]))
print("completed: {}".format(info_get["completed"]))
else:
print(">>> Skip download.")
return True
def print_multi_list(multi_ch_info=None, sep=";"):
"""Print the summary of downloaded claims from multiple channels.
This is meant to be used with the returned list from
`ch_download_latest_multi`.
Parameters
----------
list of lists of dicts
A list of lists, where each internal list represents one channel,
and this internal list has as many dictionaries as downloaded claims.
The information in each dictionary represents the standard output
of the `lbrynet_get` command for each downloaded claim.
If the download fails, then the corresponding item in the list
may be `False`, in which case no claim information is printed.
sep: str, optional
It defaults to `;`. It is the separator character between
the data fields in the printed summary. Since the claim name
can have commas, a semicolon `;` is used by default.
Returns
-------
bool
It returns `True` if the information was read and printed
without problems.
If there is a problem or no list of items, it will return `False`.
"""
if not multi_ch_info or not isinstance(multi_ch_info, (list, tuple)):
print("Print information from a list of lists from multiple "
"channels obtained from `ch_download_latest_multi`.")
return False
if len(multi_ch_info) < 1:
print("Empty list.")
return False
# flat_list = [item for sublist in list_ch_info for item in sublist]
flat_list = []
for sublist in multi_ch_info:
if not sublist:
flat_list.append(None)
continue
for item in sublist:
if not item:
flat_list.append(None)
continue
flat_list.append(item)
n_items = len(flat_list)
print("Summary of downloads")
out = []
for it, item in enumerate(flat_list, start=1):
line = "{:2d}/{:2d}".format(it, n_items) + f"{sep} "
if not item:
line += "empty item. Failure establishing server connection?"
out.append(line)
continue
if "claim_id" in item:
line += "{}".format(item["claim_id"]) + f"{sep} "
line += "{:3d}/{:3d}".format(item["blobs_completed"],
item["blobs_in_stream"]) + f"{sep} "
line += '"{}"'.format(item["channel_name"])
line += f"{sep} "
line += '"{}"'.format(item["claim_name"])
out.append(line)
elif "error" in item:
out.append(line + '"{}"'.format(item["error"]))
else:
out.append(line + "not downloaded")
funcs.print_content(out, file=None, fdate=False)
return True