mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-26 00:13:57 +01:00
Compare commits
No commits in common. "60a89146da827674f7e3558183e573bed4726e9f" and "15f98d8da84da58a1caa2652a61b8decbd1d1056" have entirely different histories.
60a89146da
...
15f98d8da8
17
api/core.py
17
api/core.py
|
@ -12,14 +12,7 @@ from dotenv import load_dotenv
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
router = fastapi.APIRouter(tags=['core'])
|
router = fastapi.APIRouter(tags=['core'])
|
||||||
|
|
||||||
|
|
||||||
async def check_core_auth(request):
|
async def check_core_auth(request):
|
||||||
"""
|
|
||||||
|
|
||||||
### Checks the request's auth
|
|
||||||
Auth is taken from environment variable `CORE_API_KEY`
|
|
||||||
|
|
||||||
"""
|
|
||||||
received_auth = request.headers.get('Authorization')
|
received_auth = request.headers.get('Authorization')
|
||||||
|
|
||||||
if received_auth != os.getenv('CORE_API_KEY'):
|
if received_auth != os.getenv('CORE_API_KEY'):
|
||||||
|
@ -27,12 +20,14 @@ async def check_core_auth(request):
|
||||||
|
|
||||||
@router.get('/users')
|
@router.get('/users')
|
||||||
async def get_users(discord_id: int, incoming_request: fastapi.Request):
|
async def get_users(discord_id: int, incoming_request: fastapi.Request):
|
||||||
auth = await check_core_auth(incoming_request)
|
auth_error = await check_core_auth(incoming_request)
|
||||||
if auth:
|
|
||||||
|
if auth_error:
|
||||||
return auth_error
|
return auth_error
|
||||||
|
|
||||||
# Get user by discord ID
|
user = await users.by_discord_id(discord_id)
|
||||||
if not await users.by_discord_id(discord_id):
|
|
||||||
|
if not user:
|
||||||
return fastapi.Response(status_code=404, content='User not found.')
|
return fastapi.Response(status_code=404, content='User not found.')
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
|
@ -10,13 +10,12 @@ import load_balancing
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
async def is_policy_violated(inp: Union[str, list]) -> bool:
|
async def is_policy_violated(inp: Union[str, list]) -> bool:
|
||||||
"""
|
"""Check if a message violates the moderation policy.
|
||||||
### Check if a message violates the moderation policy.
|
You can either pass a list of messages consisting of dicts with "role" and "content", as used in the API parameter,
|
||||||
You can either pass a list of messages consisting of dicts with "role" and "content", as used in the API parameter,
|
or just a simple string.
|
||||||
or just a simple string.
|
|
||||||
|
Returns True if the message violates the policy, False otherwise.
|
||||||
Returns True if the message violates the policy, False otherwise.
|
"""
|
||||||
"""
|
|
||||||
|
|
||||||
text = inp
|
text = inp
|
||||||
|
|
||||||
|
|
|
@ -15,14 +15,11 @@ load_dotenv()
|
||||||
USE_PROXY_LIST = os.getenv('USE_PROXY_LIST', 'False').lower() == 'true'
|
USE_PROXY_LIST = os.getenv('USE_PROXY_LIST', 'False').lower() == 'true'
|
||||||
|
|
||||||
class Proxy:
|
class Proxy:
|
||||||
"""
|
"""Represents a proxy. The type can be either http, https, socks4 or socks5.
|
||||||
### Represents a proxy.
|
You can also pass a url, which will be parsed into the other attributes.
|
||||||
The type can be either http, https, socks4 or socks5.
|
URL format:
|
||||||
You can also pass a url, which will be parsed into the other attributes.
|
|
||||||
|
|
||||||
URL format:
|
|
||||||
[type]://[username:password@]host:port
|
[type]://[username:password@]host:port
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
url: str=None,
|
url: str=None,
|
||||||
|
@ -76,27 +73,28 @@ class Proxy:
|
||||||
proxy_type=proxy_types[self.proxy_type],
|
proxy_type=proxy_types[self.proxy_type],
|
||||||
host=self.ip_address,
|
host=self.ip_address,
|
||||||
port=self.port,
|
port=self.port,
|
||||||
rdns=False,
|
rdns=False, # remote DNS
|
||||||
username=self.username,
|
username=self.username,
|
||||||
password=self.password
|
password=self.password
|
||||||
)
|
)
|
||||||
|
|
||||||
## Load proxies from their files
|
# load proxies from files
|
||||||
|
|
||||||
proxies_in_files = []
|
proxies_in_files = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for proxy_type in ['http', 'socks4', 'socks5']:
|
for proxy_type in ['http', 'socks4', 'socks5']:
|
||||||
with open(f'secret/proxies/{proxy_type}.txt') as f:
|
with open(f'secret/proxies/{proxy_type}.txt') as f:
|
||||||
for line in f:
|
for line in f.readlines():
|
||||||
clean_line = line.split('#', 1)[0].strip()
|
if line.strip() and not line.strip().startswith('#'):
|
||||||
if clean_line:
|
if '#' in line:
|
||||||
proxies_in_files.append(f'{proxy_type}://{clean_line}')
|
line = line.split('#')[0]
|
||||||
|
|
||||||
|
proxies_in_files.append(f'{proxy_type}://{line.strip()}')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
## Manages the proxy list
|
# Proxy lists support
|
||||||
|
|
||||||
class ProxyLists:
|
class ProxyLists:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -106,11 +104,8 @@ class ProxyLists:
|
||||||
self.connector = aiohttp_socks.ChainProxyConnector.from_urls(proxies_in_files)
|
self.connector = aiohttp_socks.ChainProxyConnector.from_urls(proxies_in_files)
|
||||||
|
|
||||||
def get_proxy() -> Proxy:
|
def get_proxy() -> Proxy:
|
||||||
"""
|
"""Returns a Proxy object. The proxy is either from the proxy list or from the environment variables.
|
||||||
### Returns a Proxy object
|
"""
|
||||||
The proxy is either from the proxy list or from the environment variables.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if USE_PROXY_LIST:
|
if USE_PROXY_LIST:
|
||||||
return ProxyLists().get_random
|
return ProxyLists().get_random
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue