2022-10-26 13:06:04 +00:00
|
|
|
from functools import wraps
|
2022-12-22 00:01:21 +00:00
|
|
|
from os import getenv
|
2022-10-26 13:06:04 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from pynyaata.types import Color, RemoteFile
|
|
|
|
|
|
|
|
|
|
|
|
def duplicate(remotes: List[RemoteFile]) -> List[RemoteFile]:
|
|
|
|
processed_ids: List[int] = []
|
|
|
|
dedup_remotes: List[RemoteFile] = []
|
|
|
|
|
|
|
|
for remote in remotes:
|
|
|
|
if remote.id not in processed_ids:
|
|
|
|
dedup_remotes.append(remote)
|
|
|
|
|
|
|
|
processed_ids.append(remote.id)
|
|
|
|
|
|
|
|
return dedup_remotes
|
|
|
|
|
|
|
|
|
|
|
|
def inactive(remotes: List[RemoteFile]) -> List[RemoteFile]:
|
|
|
|
return list(
|
|
|
|
filter(
|
|
|
|
lambda remote: remote.seeds != 0 or remote.downloads != 0,
|
|
|
|
remotes,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def blacklist(remotes: List[RemoteFile]) -> List[RemoteFile]:
|
2023-01-06 21:01:11 +00:00
|
|
|
BLACKLIST_WORDS = getenv("BLACKLIST_WORDS", "")
|
|
|
|
|
|
|
|
if not BLACKLIST_WORDS:
|
|
|
|
return remotes
|
|
|
|
else:
|
|
|
|
return list(
|
|
|
|
filter(
|
|
|
|
lambda remote: not any(
|
2023-03-22 23:20:25 +00:00
|
|
|
word and word in remote.name.lower()
|
|
|
|
for word in BLACKLIST_WORDS.split(",")
|
2023-01-06 21:01:11 +00:00
|
|
|
),
|
|
|
|
remotes,
|
|
|
|
)
|
2022-10-26 13:06:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def danger(remotes: List[RemoteFile]) -> List[RemoteFile]:
|
|
|
|
return list(
|
|
|
|
filter(
|
|
|
|
lambda remote: remote.color != Color.DANGER,
|
|
|
|
remotes,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-22 23:20:25 +00:00
|
|
|
def trusted(remotes: List[RemoteFile]) -> List[RemoteFile]:
|
|
|
|
TRUSTED_WORDS = getenv("TRUSTED_WORDS", "")
|
|
|
|
|
|
|
|
for remote in remotes:
|
|
|
|
if any(
|
|
|
|
word and word in remote.name.lower() for word in TRUSTED_WORDS.split(",")
|
|
|
|
):
|
|
|
|
remote.color = Color.PRIMARY
|
|
|
|
|
|
|
|
return remotes
|
|
|
|
|
|
|
|
|
2022-10-26 13:06:04 +00:00
|
|
|
def filter_data(f):
|
|
|
|
@wraps(f)
|
2023-01-13 22:11:55 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
ret = f(*args, **kwargs)
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
ret = duplicate(ret)
|
|
|
|
ret = inactive(ret)
|
|
|
|
ret = blacklist(ret)
|
|
|
|
ret = danger(ret)
|
2023-03-22 23:20:25 +00:00
|
|
|
ret = trusted(ret)
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
return wrapper
|