-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetClaimIdsFromMessage.html
194 lines (176 loc) · 5.41 KB
/
getClaimIdsFromMessage.html
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
<html>
<head>
<style>
body {
text-align: center;
}
div {
margin: 10px 0px;
}
textarea {
width: 1000px;
height: 400px;
}
</style>
<script>
let default_lbrynet_url = "http://172.105.86.236:5279";
let check_urls = [
"lbry://",
"odysee.com/",
"lbry.tv/",
];
function doACall(url, method, params, callback) {
let xhr = new XMLHttpRequest();
url = `${url}?m=${method}`;
xhr.open("POST", url, true);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
try {
let response = JSON.parse(xhr.response);
if (!response.error) {
callback(response);
} else {
alert(response.error);
}
}
catch {
alert("Call to lbrynet failed. Response not JSON");
return;
}
} else if (xhr.readyState === 4) {
alert("Call to lbrynet failed. Status not 200");
}
};
xhr.send(JSON.stringify({
method: method,
params: params
}));
}
function clear_result_text_areas() {
let failed_text_area = document.querySelector("#failed_text_area");
failed_text_area.value = "";
let result_text_area = document.querySelector("#result_text_area");
result_text_area.value = "";
}
function get_message_text_area() {
let message_text_area = document.querySelector("#message_text_area");
return message_text_area;
}
function get_message() {
let message_text_area = get_message_text_area();
return message_text_area.value;
}
function set_message(message) {
let message_text_area = get_message_text_area();
message_text_area.value = message;
}
function get_lbrynet_server_url() {
let lbrynet_url_input = document.querySelector("#lbrynet_url_input");
return lbrynet_url_input.value ? lbrynet_url_input.value : default_lbrynet_url;
}
function remove_url_from_message(url) {
let message = get_message();
message = message.replace(url, "DETECTED");
set_message(message);
}
function convert_url_to_LBRY_url(url) {
for (let i = 0; i < check_urls.length; i++) {
let check_url = check_urls[i];
let url_rx = new RegExp(`${check_url}.+`);
let matched = url.match(url_rx);
if (matched) {
let lbry_url = matched[0].replace(check_url, "lbry://");
return lbry_url;
}
}
}
function try_to_get_urls_from_text(text, check_urls) {
let urls = [];
for (let i = 0; i < check_urls.length; i++) {
let check_url = check_urls[i];
let url_rx = new RegExp(`${check_url}[^\\s\\n\?]+`, "g");
let matched_urls = text.match(url_rx);
if (matched_urls) {
urls = urls.concat(matched_urls);
}
}
return urls;
}
function main() {
let lbrynet_url = get_lbrynet_server_url();
let lbrynet_url_btn = document.querySelector("#lbrynet_url_btn");
lbrynet_url_btn.onclick = () => lbrynet_url = get_lbrynet_server_url();
console.log(lbrynet_url);
let btn = document.querySelector("#get_claim_ids_btn");
btn.onclick = () => {
clear_result_text_areas();
// Get message
let message = get_message();
// Try to get all Odysee/LBRY urls from message, and store in lbry:// format
let lbry_urls = [];
let urls = try_to_get_urls_from_text(message, check_urls);
urls.forEach((url) => {
remove_url_from_message(url);
let lbry_url = convert_url_to_LBRY_url(url);
lbry_url = decodeURIComponent(lbry_url);
lbry_urls.push(lbry_url);
});
// Resolve all found urls, and get claim_ids
let claim_ids = [];
let unresolved_urls = [];
doACall(lbrynet_url, "resolve", {urls: lbry_urls}, (response) => {
let claims = response["result"];
for (let i = 0; i < lbry_urls.length; i++) {
let lbry_url = lbry_urls[i];
let claim = claims[lbry_url];
if (claim) {
if (!claim.error && claim.value_type === "stream") {
claim_ids.push(claim.claim_id);
} else {
let error_reason = claim.error ? claim.error.text
? claim.error.text
: claim.error
: "Not a stream claim";
unresolved_urls.push(`${lbry_url} - ${error_reason}`);
}
} else {
let error_reason = "Not found from response dict";
unresolved_urls.push(`${lbry_url} - ${error_reason}`);
}
}
// Parse claim_ids to sql query format
for (let i = 0; i < claim_ids.length; i++) {
claim_ids[i] = `'${claim_ids[i]}',`;
}
// Show results
let result_text_area = document.querySelector("#result_text_area");
result_text_area.value = claim_ids.join('\n').slice(0, -1); //Slice removes the last ','
let failed_text_area = document.querySelector("#failed_text_area");
failed_text_area.value = unresolved_urls.join('\n');
});
}
}
document.addEventListener("DOMContentLoaded", () => {main();});
</script>
</head>
<body>
<div>
<label>Lbrynet url:</label>
<input id="lbrynet_url_input" placeHolder="http://172.105.86.236:5279">
<button id="lbrynet_url_btn">Set</button>
</div>
<div class="textarea_div">
<p>Paste the message here ( Found urls will be replaced with word: 'DETECTED' )</p>
<textarea id="message_text_area"></textarea>
</div>
<button id="get_claim_ids_btn">Get claim ids</button>
<div class="textarea_div">
<p>Claim_ids</p>
<textarea id="result_text_area"></textarea>
</div>
<div class="textarea_div">
<p>Failed to resolve or not stream url</p>
<textarea id="failed_text_area"></textarea>
</div>
</body>
</html>