mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-11 01:02:10 +03:00
run refresh_netsets provider fetches concurrently with asyncio.gather
This commit is contained in:
parent
6810c0f753
commit
52c0647e77
1 changed files with 35 additions and 12 deletions
|
|
@ -75,26 +75,49 @@ async def refresh_netsets(
|
||||||
cache_dir: Path,
|
cache_dir: Path,
|
||||||
) -> NetsetIndex:
|
) -> NetsetIndex:
|
||||||
directory = Path(cache_dir) / "firehol"
|
directory = Path(cache_dir) / "firehol"
|
||||||
directory.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
def setup_dir() -> None:
|
||||||
|
directory.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
await asyncio.to_thread(setup_dir)
|
||||||
index = NetsetIndex()
|
index = NetsetIndex()
|
||||||
for url in providers.firehol_netsets:
|
|
||||||
|
async def process_one(url: str) -> tuple[str, list[str] | None]:
|
||||||
name = url.rsplit("/", 1)[-1].removesuffix(".netset")
|
name = url.rsplit("/", 1)[-1].removesuffix(".netset")
|
||||||
path = directory / f"{name}.netset"
|
path = directory / f"{name}.netset"
|
||||||
fresh = (
|
|
||||||
path.exists()
|
def check_fresh() -> bool:
|
||||||
and (time.time() - os.path.getmtime(path)) < providers.firehol_refresh_hours * 3600
|
return (
|
||||||
)
|
path.exists()
|
||||||
|
and (time.time() - os.path.getmtime(path)) < providers.firehol_refresh_hours * 3600
|
||||||
|
)
|
||||||
|
|
||||||
|
fresh = await asyncio.to_thread(check_fresh)
|
||||||
if not fresh:
|
if not fresh:
|
||||||
try:
|
try:
|
||||||
response = await client.get(url)
|
response = await client.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
tmp = path.with_suffix(".netset.tmp")
|
|
||||||
tmp.write_text(response.text, encoding="utf-8")
|
def write_file() -> None:
|
||||||
os.replace(tmp, path)
|
tmp = path.with_suffix(".netset.tmp")
|
||||||
|
tmp.write_text(response.text, encoding="utf-8")
|
||||||
|
os.replace(tmp, path)
|
||||||
|
|
||||||
|
await asyncio.to_thread(write_file)
|
||||||
except httpx.HTTPError:
|
except httpx.HTTPError:
|
||||||
if not path.exists():
|
if not await asyncio.to_thread(path.exists):
|
||||||
continue
|
return name, None
|
||||||
index.add(name, parse_netset(path.read_text(encoding="utf-8")))
|
|
||||||
|
def read_and_parse() -> list[str]:
|
||||||
|
return parse_netset(path.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
|
parsed = await asyncio.to_thread(read_and_parse)
|
||||||
|
return name, parsed
|
||||||
|
|
||||||
|
results = await asyncio.gather(*(process_one(url) for url in providers.firehol_netsets))
|
||||||
|
for name, parsed in results:
|
||||||
|
if parsed is not None:
|
||||||
|
index.add(name, parsed)
|
||||||
return index
|
return index
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue