small fix in api

This commit is contained in:
Mateusz Gruszczyński
2026-03-04 10:33:48 +01:00
parent 721ad44960
commit 013a73e02d
3 changed files with 25 additions and 34 deletions

38
api.py
View File

@@ -439,30 +439,17 @@ def generate_raw_cidr():
}
)
if handler.needs_update():
handler.check_and_update()
update_progress(f'Loading data for {len(countries)} countries...', 0, 100)
update_progress('Loading networks...', 10, 100)
country_networks = {}
cache_sources = {}
total_countries = len(countries)
for idx, country in enumerate(countries, 1):
base_progress = int((idx - 1) / total_countries * 80)
update_progress(f'[{idx}/{total_countries}] Loading {country}...', base_progress, 100)
networks, source = get_country_networks_cached(country, use_cache=use_cache)
if networks:
country_networks[country] = networks
for i, country in enumerate(countries):
update_progress(f'Processing {country}...', 10 + (i * 60 // max(1, len(countries))), 100)
nets, source = get_country_networks_cached(country, use_cache=use_cache)
if nets:
country_networks[country] = nets
cache_sources[country] = source
next_progress = int(idx / total_countries * 80)
update_progress(
f'[{idx}/{total_countries}] {country}: {len(networks):,} networks ({source})',
next_progress,
100
)
if not country_networks:
clear_progress()
@@ -470,12 +457,7 @@ def generate_raw_cidr():
update_progress('Generating file...', 85, 100)
if 'txt' in app_type or 'cidr' in app_type or 'newline' in app_type:
config_text = ConfigGenerator.generate_raw_cidr(country_networks, aggregate=aggregate, redis_ips=None)
filename = f"blocklist_{'_'.join(sorted(countries))}.txt"
mimetype = 'text/plain'
elif 'json' in app_type:
if 'json' in app_type:
all_networks = []
for nets in country_networks.values():
all_networks.extend(nets)
@@ -507,8 +489,10 @@ def generate_raw_cidr():
mimetype = 'text/csv'
else:
clear_progress()
return jsonify({'success': False, 'error': f'Unknown format: {app_type}'}), 400
# TXT / CIDR / newline (default)
config_text = ConfigGenerator.generate_raw_cidr(country_networks, aggregate=aggregate, redis_ips=None)
filename = f"blocklist_{'_'.join(sorted(countries))}.txt"
mimetype = 'text/plain'
total_networks = sum(len(nets) for nets in country_networks.values())
stats = {

View File

@@ -42,15 +42,16 @@ function cacheHeaderSummary(headers) {
async function readBodyAuto(response) {
const ct = (response.headers.get('content-type') || '').toLowerCase();
const raw = await response.text();
const isJson = ct.includes('application/json');
if (isJson) {
try {
return { kind: 'json', data: await response.json(), contentType: ct };
return { kind: 'json', data: JSON.parse(raw), contentType: ct, raw };
} catch {
return { kind: 'text', data: await response.text(), contentType: ct };
return { kind: 'text', data: raw, contentType: ct, raw };
}
}
return { kind: 'text', data: await response.text(), contentType: ct };
return { kind: 'text', data: raw, contentType: ct, raw };
}
function safeParseJsonFromTextarea(textareaId) {
@@ -255,7 +256,7 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) {
const url = baseUrl + '/api/' + endpoint;
fetchAsBlob(url, 'POST', body)
.then(async ({ blob, headersText, cacheSummary, contentType, contentDisposition }) => {
.then(async ({ blob, headersText, cacheSummary, contentType }) => {
const ext = guessExtensionFromContentType(contentType);
const filename = `${fileBaseName}.${ext}`;
@@ -278,7 +279,6 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) {
});
}
function previewTextFromGenerate(textareaId) {
const { body: out } = showResponse('response-generate-download');
@@ -291,7 +291,7 @@ function previewTextFromGenerate(textareaId) {
}
fetchAsBlob(baseUrl + '/api/generate', 'POST', body)
.then(async ({ blob, headersText, cacheSummary, contentType }) => {
.then(async ({ blob, headersText, cacheSummary }) => {
let text = '';
try { text = await blob.text(); } catch { text = '(binary content)'; }

View File

@@ -465,10 +465,11 @@
<tr><td><code class="text-success">raw-cidr_csv</code></td><td><code>text/csv</code></td><td>CSV export</td></tr>
<tr><td><code class="text-success">raw-cidr_json</code></td><td><code>application/json</code></td><td><code>{countries, networks, count, aggregated}</code></td></tr>
<tr><td><code class="text-success">raw-cidr_json</code> + <code>as_js=true</code></td><td><code>application/javascript</code></td><td><code>const &lt;js_var&gt; = {...};</code></td></tr>
<tr><td><code class="text-success">raw-json</code></td><td><code>application/json</code></td><td>alias (same schema as <code>raw-cidr_json</code>)</td></tr>
</tbody>
</table>
<h6 class="fw-bold mt-3">Example JSON Schema (raw-cidr_json)</h6>
<h6 class="fw-bold mt-3">Example JSON Schema (raw-cidr_json / raw-json)</h6>
<pre><code>{
"countries": ["SG"],
"networks": ["1.2.3.0/24", "..."],
@@ -523,6 +524,12 @@
-d '{"countries":["PL"],"app_type":"raw-cidr_json","aggregate":true,"use_cache":true}' \
-o blocklist.json</code></pre>
<p class="mb-2 mt-3"><strong>JSON (alias raw-json):</strong></p>
<pre><code class="language-bash">curl -X POST <span id="curlUrl-raw-json-alias"></span>/api/generate/raw \
-H "Content-Type: application/json" \
-d '{"countries":["CN"],"aggregate":true,"use_cache":true,"app_type":"raw-json"}' \
-o blocklist.cn.json</code></pre>
<p class="mb-2 mt-3"><strong>JS wrapped:</strong></p>
<pre><code class="language-bash">curl -X POST <span id="curlUrl-raw-js"></span>/api/generate/raw \
-H "Content-Type: application/json" \