-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclean.py
239 lines (201 loc) · 8.99 KB
/
clean.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
#!/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 clean downloaded content from the LBRY network."""
import os
import requests
import lbrytools.funcs as funcs
import lbrytools.search as srch
def lbrynet_del(claim_id=None, claim_name=None, what="blobs",
server="http://localhost:5279"):
"""Run the lbrynet file delete command on the given claim ID.
Parameters
----------
claim_id: str
A `'claim_id'` for a claim on the LBRY network.
It is a 40 character alphanumeric string.
claim_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'
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
-------
bool
It returns `True` if the files were deleted successfully.
It returns `False` if there is an error.
"""
if not funcs.server_exists(server=server):
return False
if not claim_id:
print("No input claim ID, using default value.")
claim_id = "70dfefa510ca6eee7023a2a927e34d385b5a18bd"
claim_name = "04-S"
cmd_name = ["lbrynet",
"file",
"delete",
"--claim_name=" + "'" + claim_name + "'"]
if "'" in claim_name:
cmd_name[3] = "--claim_name=" + '"' + claim_name + '"'
cmd_id = cmd_name[:]
cmd_id[3] = "--claim_id=" + claim_id
if what in "blobs":
print("Remove blobs")
elif what in "both":
print("Remove both, blobs and file")
cmd_id.append("--delete_from_download_dir")
cmd_name.append("--delete_from_download_dir")
print("Remove: " + " ".join(cmd_id))
print("Remove: " + " ".join(cmd_name))
msg = {"method": cmd_id[1] + "_" + cmd_id[2],
"params": {"claim_id": claim_id}}
if what in "both":
msg["params"]["delete_from_download_dir"] = True
output = requests.post(server, json=msg).json()
if "error" in output:
print(">>> No 'result' in the JSON-RPC server output")
return False
print("Blobs deleted")
return True
def delete_single(uri=None, cid=None, name=None, invalid=False,
what="media",
server="http://localhost:5279"):
"""Delete a single file, and optionally the downloaded blobs.
As long as the blobs are present, the content can be seeded
to the network, and the full file can be restored.
That is, while the blobs exist the file is not completely deleted.
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'
invalid: bool, optional
It defaults to `False`, in which case it will assume the claim
is still valid in the online database.
It will use `lbrynet claim search` to search `cid` or `name`.
If it is `True` it will assume the claim is no longer valid,
that is, that the claim has been removed from the online database
and only exists locally.
In this case, it will use `lbrynet file list` to resolve
`cid` or `name`.
This has no effect on `uri`, so if this input is used,
it will always try to resolve it from the online database.
what: str, optional
It defaults to `'media'`, in which case only the full media file
(mp4, mp3, mkv, etc.) is deleted.
If it is `'blobs'`, it will delete only the blobs.
If it is `'both'`, it will delete both the media file
and the blobs.
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
-------
bool
It returns `True` if the claim was deleted successfully.
If there is a problem or non existing claim, it will return `False`.
"""
if not (uri or cid or name):
print("Delete item by 'URI', 'claim_id', or 'name'.")
print(f"uri={uri}, cid={cid}, name={name}")
return False
if (not isinstance(what, str)
or what not in ("media", "blobs", "both")):
print(">>> Error: what can only be 'media', 'blobs', 'both'")
print(f"what={what}")
return False
# Searching online, with `offline=False`, will allow us to get the
# `canonical_url` and full channel's name, otherwise we can only obtain
# the `claim_name` and a simple channel name.
item = srch.search_item(uri=uri, cid=cid, name=name, offline=invalid,
server=server)
if not item:
return False
claim_id = item["claim_id"]
if invalid:
claim_name = item["claim_name"]
channel = item["channel_name"]
else:
claim_uri = item["canonical_url"]
claim_name = item["name"]
if ("signing_channel" in item
and "canonical_url" in item["signing_channel"]):
channel = item["signing_channel"]["canonical_url"]
channel = channel.split("lbry://")[1]
else:
channel = "@_Unknown_"
# Searching offline is necessary to get the download path,
# and blob information.
item = srch.search_item(cid=claim_id, offline=True,
server=server)
if not item:
print("No claim found locally, probably already deleted.")
return True
path = item["download_path"]
blobs = int(item["blobs_completed"])
blobs_full = int(item["blobs_in_stream"])
if invalid:
print(f"claim_name: {claim_name}")
else:
print(f"canonical_url: {claim_uri}")
print(f"claim_id: {claim_id}")
print(f"Blobs found: {blobs} of {blobs_full}")
if what in "media":
print(f"Remove media file: {path}")
if path:
os.remove(path)
print("Media file deleted")
else:
print("No media found locally, probably already deleted.")
return True
status = lbrynet_del(claim_id, claim_name=claim_name, what=what,
server=server)
return status
if __name__ == "__main__":
delete_single("1M") # assuming it was downloaded first