-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.py
486 lines (409 loc) · 17.4 KB
/
search.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python3
# --------------------------------------------------------------------------- #
# The MIT License (MIT) #
# #
# Copyright (c) 2021 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> #
# #
# 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 help with searching claims in the LBRY network."""
import concurrent.futures as fts
import requests
import lbrytools.funcs as funcs
def check_repost(item, repost=True):
"""Check if the item is a repost, and return the original item.
A claim that is just the repost of another cannot be downloaded directly,
so we replace the input item with the original source item.
Parameters
----------
item: dict
A dictionary with the information on an item, obtained
from `lbrynet resolve` or `lbrynet claim search`.
repost: bool, optional
It defaults to `True`, in which case the returned `item`
will be the reposted claim, that is,
the value of `item['reposted_claim']`.
If it's `False` it will return the original input `item`.
Returns
-------
dict
The original `item` dictionary if it is not a repost
or if it's a repost but `repost=False`.
Otherwise, it will return `item['reposted_claim']`.
"""
if "reposted_claim" in item:
old_uri = item["canonical_url"]
uri = item["reposted_claim"]["canonical_url"]
print("This is a repost.")
print(f"canonical_url: {old_uri}")
print(f"reposted_claim: {uri}")
print()
if repost:
item = item["reposted_claim"]
return item
def search_item_uri(uri=None, repost=True,
print_error=True,
server="http://localhost:5279"):
"""Find a single item in the LBRY network, resolving the URI.
Parameters
----------
uri: str
A unified resource identifier (URI) to a claim on the LBRY network.
It can be full or partial.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
uri = '@MyChannel#3/some-video-name#2'
uri = 'some-video-name'
The URI is also called the `'canonical_url'` of the claim.
repost: bool, optional
It defaults to `True`, in which case it will check if the claim
is a repost, and if it is, it will return the original claim.
If it is `False`, it won't check for a repost, it will simply return
the found claim.
print_error: bool, optional
It defaults to `True`, in which case it will print the error message
that `lbrynet resolve` returns.
By setting this value to `False` no messages will be printed;
this is useful inside other functions when we want to limit
the terminal output.
server: str, optional
It defaults to `'http://localhost:5279'`.
This is the address of the `lbrynet` daemon, which should be running
in your computer before using any `lbrynet` command.
Normally, there is no need to change this parameter from its default
value.
Returns
-------
dict
Returns the dictionary that represents the `claim`
that was found matching the URI.
False
If the dictionary has the `'error'` key, it will print the contents
of this key, and return `False`.
"""
if not funcs.server_exists(server=server):
return False
if not uri or not isinstance(uri, str):
m = ["Search by URI, full or partial.",
"lbry://@MyChannel#3/some-video-name#2",
" @MyChannel#3/some-video-name#2",
" some-video-name"]
print("\n".join(m))
print(f"uri={uri}")
return False
cmd = ["lbrynet",
"resolve",
uri]
msg = {"method": cmd[1],
"params": {"urls": uri}}
output = requests.post(server, json=msg).json()
if "error" in output:
print(">>> No 'result' in the JSON-RPC server output")
return False
item = output["result"][uri]
if "error" in item:
if print_error:
error = item["error"]
if "name" in error:
name_err = error["name"]
text_err = error["text"]
print(f">>> Error: {name_err}, {text_err}")
else:
print(f">>> Error: {error}")
print(">>> Check that the URI is correct, "
"or that the claim hasn't been removed from the network.")
return False
# The found item may be a repost so we check it,
# and return the original source item.
item = check_repost(item, repost=repost)
return item
def search_item_cid(cid=None, name=None,
repost=True, offline=False,
print_error=True,
server="http://localhost:5279"):
"""Find a single item in the LBRY network, resolving the claim id or name.
If both `cid` and `name` are given, `cid` is used.
Parameters
----------
cid: str
A `'claim_id'` for a claim on the LBRY network.
It is a 40 character alphanumeric string.
name: str, optional
A name of a claim on the LBRY network.
It is normally the last part of a full URI.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
name = 'some-video-name'
repost: bool, optional
It defaults to `True`, in which case it will check if the claim
is a repost, and if it is, it will return the original claim.
If it is `False`, it won't check for a repost, it will simply return
the found claim.
offline: bool, optional
It defaults to `False`, in which case it will use
`lbrynet claim search` to search `cid` or `name` in the online
database.
If it is `True` it will use `lbrynet file list` to search
`cid` or `name` in the offline database, 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.
When `offline=True`, `repost=True` has no effect because reposts
must be resolved online.
print_error: bool, optional
It defaults to `True`, in which case it will print an error message
if the claim is not found.
By setting this value to `False` no messages will be printed;
this is helpful if this function is used inside other functions,
and we want to limit the terminal output.
server: str, optional
It defaults to `'http://localhost:5279'`.
This is the address of the `lbrynet` daemon, which should be running
in your computer before using any `lbrynet` command.
Normally, there is no need to change this parameter from its default
value.
Returns
-------
dict
Returns the dictionary that represents the `claim`
that was found matching the `name` or `cid`.
False
If the dictionary seems to have no items found, it will print
an error message and return `False`.
"""
if not funcs.server_exists(server=server):
return False
if (name and (not isinstance(name, str)
or "#" in name or ":" in name or "@" in name)
or cid and (not isinstance(cid, str)
or "#" in cid or ":" in cid or "@" in cid)
or not (name or cid)):
m = ["Search by 'name' or 'claim_id' only.",
"lbry://@MyChannel#3/some-video-name#2",
" ^-------------^",
" name"]
print("\n".join(m))
print(f"cid={cid}")
print(f"name={name}")
return False
if offline:
cmd = ["lbrynet",
"file",
"list",
"--claim_name='{}'".format(name)]
if cid:
cmd[3] = "--claim_id=" + cid
msg = {"method": cmd[1] + "_" + cmd[2],
"params": {"claim_name": name}}
else:
cmd = ["lbrynet",
"claim",
"search",
"--name={}".format(name)]
if cid:
cmd[3] = "--claim_ids=" + cid
msg = {"method": cmd[1] + "_" + cmd[2],
"params": {"name": name}}
if cid:
msg["params"] = {"claim_id": cid}
output = requests.post(server, json=msg).json()
if "error" in output:
print(">>> No 'result' in the JSON-RPC server output")
return False
data = output["result"]
if "blocked" in data and data["blocked"]["total"] > 0:
chs = data["blocked"]["channels"]
blks = []
for blocking in chs:
blk = blocking["channel"]["canonical_url"].split("lbry://")[1]
blks.append(blk)
ch = " ; ".join(blks)
print(">>> Claim blocked by hub.")
print(f">>> Blocking channel: {ch}")
return False
if data["total_items"] < 1:
if print_error:
if cid:
print(">>> No item found.")
print(">>> Check that the claim ID is correct, "
"or that the claim hasn't been removed from "
"the network.")
elif name:
print(">>> No item found.")
print(">>> Check that the name is correct, "
"or that the claim hasn't been removed from "
"the network.")
return False
# The list of items may include various reposts;
# usually the last item is the oldest and thus the original.
item = data["items"][-1]
# The found item may be a repost so we check it,
# and return the original source item.
item = check_repost(item, repost=repost)
return item
def search_item(uri=None, cid=None, name=None,
repost=True, offline=False,
print_error=True,
server="http://localhost:5279"):
"""Find a single item in the LBRY network resolving URI, claim id, or name.
If all inputs are provided, `uri` is used.
If only `cid` and `name` are given, `cid` is used.
Parameters
----------
uri: str
A unified resource identifier (URI) to a claim on the LBRY network.
It can be full or partial.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
uri = '@MyChannel#3/some-video-name#2'
uri = 'some-video-name'
The URI is also called the `'canonical_url'` of the claim.
cid: str, optional
A `'claim_id'` for a claim on the LBRY network.
It is a 40 character alphanumeric string.
name: str, optional
A name of a claim on the LBRY network.
It is normally the last part of a full URI.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
name = 'some-video-name'
repost: bool, optional
It defaults to `True`, in which case it will check if the claim
is a repost, and if it is, it will return the original claim.
If it is `False`, it won't check for a repost, it will simply return
the found claim.
offline: bool, optional
It defaults to `False`, in which case it will use
`lbrynet claim search` to search `cid` or `name` in the online
database.
If it is `True` it will use `lbrynet file list` to search
`cid` or `name` in the offline database, 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.
When `offline=True`, `repost=True` has no effect because reposts
must be resolved online. In this case, if `uri` is provided,
and `name` is not, `uri` will be used as the value of `name`.
print_error: bool, optional
It defaults to `True`, in which case it will print the error message
that `lbrynet resolve` or `lbrynet claim search` returns.
If it is `False` no error messages will be printed;
this is useful inside other functions when we want to limit
the terminal output.
server: str, optional
It defaults to `'http://localhost:5279'`.
This is the address of the `lbrynet` daemon, which should be running
in your computer before using any `lbrynet` command.
Normally, there is no need to change this parameter from its default
value.
Returns
-------
dict
The dictionary that represents the `claim` that was found
matching `uri`, `cid` or `name`.
False
If there is a problem or no claim was found, it will return `False`.
"""
if not funcs.server_exists(server=server):
return False
if not (uri or cid or name):
print("Search by 'URI', 'claim_id' or 'name'.")
print(f"uri={uri}")
print(f"cid={cid}")
print(f"name={name}")
return False
if offline:
if uri and not name:
name = uri
item = search_item_cid(cid=cid, name=name,
repost=repost, offline=offline,
print_error=print_error,
server=server)
else:
if uri:
item = search_item_uri(uri=uri,
repost=repost,
print_error=print_error,
server=server)
else:
item = search_item_cid(cid=cid, name=name,
repost=repost, offline=offline,
print_error=print_error,
server=server)
if not item and print_error:
print(f">>> uri={uri}")
print(f">>> cid={cid}")
print(f">>> name={name}")
return item
def search_th(claim,
server="http://localhost:5279"):
"""Method to resolve a claim using threads."""
result = search_item(uri=claim, repost=True,
print_error=False,
server=server)
if not result:
result = search_item(cid=claim, repost=True,
print_error=False,
server=server)
return {"original": claim,
"resolved": result}
def resolve_claims(claims, threads=32,
server="http://localhost:5279"):
"""Resolve a list of claims, whether claim IDs or URLs are given.
First it tries resolving the item by URL, and if that fails it tries
by claim ID.
Returns
-------
list of dict
It returns a list of dictionaries, one for each claim
in the input list. Each dictionary has two keys:
- 'original': original input URL or claim ID (40-digit alphanumeric)
- 'resolved': the resolved information of the claim, if it was found,
or the value `False` if it was not found.
False
If there is a problem or non existing `file`,
it will return `False`.
"""
if not funcs.server_exists(server=server):
return False
resolved = []
n_claims = len(claims)
servers = (server for n in range(n_claims))
if threads:
with fts.ThreadPoolExecutor(max_workers=threads) as executor:
# The input must be iterables
results = executor.map(search_th,
claims, servers)
resolved = list(results) # generator to list
else:
for claim in claims:
res = search_th(claim, server=server)
resolved.append(res)
return resolved
if __name__ == "__main__":
s = search_item(uri="dsnt-exist")
print()
s = search_item(uri="LUKAS-LION---1984#b")
print()
s = search_item(cid="b7c7082fd52a5b932b6f08c83645ac808b6ba801")
print()
s = search_item(name="LUKAS-LION---1984")
print()
s = search_item(cid="wwwzyx")