mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
5.0a88
This commit is contained in:
parent
7936d5b291
commit
c0030e3643
14 changed files with 131 additions and 14 deletions
|
@ -1,18 +1,14 @@
|
|||
import discord
|
||||
import sentry_sdk
|
||||
import logging as _logging
|
||||
import asyncio
|
||||
from .generic import GenericBot
|
||||
from ..utils import *
|
||||
from ..error import *
|
||||
from ..audio import *
|
||||
from ..commands import *
|
||||
|
||||
log = _logging.getLogger(__name__)
|
||||
|
||||
# TODO: Load the opus library
|
||||
if not discord.opus.is_loaded():
|
||||
log.error("Opus is not loaded. Weird behaviour might emerge.")
|
||||
log = _logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MusicData:
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
# Imports go here!
|
||||
from .version import VersionStar
|
||||
from .api_royalnet_version import ApiRoyalnetVersionStar
|
||||
|
||||
|
||||
# Enter the PageStars of your Pack here!
|
||||
available_page_stars = [
|
||||
VersionStar,
|
||||
ApiRoyalnetVersionStar,
|
||||
]
|
||||
|
||||
# Enter the ExceptionStars of your Pack here!
|
||||
|
|
|
@ -4,10 +4,10 @@ from starlette.responses import *
|
|||
from royalnet.web import PageStar
|
||||
|
||||
|
||||
class VersionStar(PageStar):
|
||||
class ApiRoyalnetVersionStar(PageStar):
|
||||
path = "/api/royalnet/version"
|
||||
|
||||
async def page(self, request: Request, **kwargs) -> JSONResponse:
|
||||
async def page(self, request: Request) -> JSONResponse:
|
||||
return JSONResponse({
|
||||
"version": {
|
||||
"semantic": royalnet.version.semantic
|
|
@ -28,6 +28,14 @@ class User:
|
|||
def avatar(self):
|
||||
return Column(LargeBinary)
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"uid": self.uid,
|
||||
"username": self.username,
|
||||
"role": self.role,
|
||||
"avatar": self.avatar
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<{self.__class__.__qualname__} {self.username}>"
|
||||
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
# Imports go here!
|
||||
|
||||
from .api_user_list import ApiUserListStar
|
||||
from .api_user_get import ApiUserGetStar
|
||||
from .api_diario_list import ApiDiarioListStar
|
||||
from .api_diario_get import ApiDiarioGetStar
|
||||
|
||||
# Enter the PageStars of your Pack here!
|
||||
available_page_stars = [
|
||||
|
||||
ApiUserListStar,
|
||||
ApiUserGetStar,
|
||||
ApiDiarioListStar,
|
||||
ApiDiarioGetStar,
|
||||
]
|
||||
|
||||
# Enter the ExceptionStars of your Pack here!
|
||||
|
|
22
royalnet/packs/royal/stars/api_diario_get.py
Normal file
22
royalnet/packs/royal/stars/api_diario_get.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from starlette.requests import Request
|
||||
from starlette.responses import *
|
||||
from royalnet.web import *
|
||||
from royalnet.utils import *
|
||||
from ..tables import Diario
|
||||
|
||||
|
||||
class ApiDiarioGetStar(PageStar):
|
||||
path = "/api/diario/get/{diario_id}"
|
||||
tables = {Diario}
|
||||
|
||||
async def page(self, request: Request) -> JSONResponse:
|
||||
diario_id_str = request.path_params.get("diario_id", "")
|
||||
try:
|
||||
diario_id = int(diario_id_str)
|
||||
except (ValueError, TypeError):
|
||||
return error(400, "Invalid diario_id")
|
||||
async with self.alchemy.session_acm() as session:
|
||||
entry: Diario = await asyncify(session.query(self.alchemy.User).get, diario_id)
|
||||
if entry is None:
|
||||
return error(404, "No such user")
|
||||
return JSONResponse(entry.json())
|
25
royalnet/packs/royal/stars/api_diario_list.py
Normal file
25
royalnet/packs/royal/stars/api_diario_list.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
from starlette.requests import Request
|
||||
from starlette.responses import *
|
||||
from royalnet.web import *
|
||||
from royalnet.utils import *
|
||||
from ..tables import Diario
|
||||
|
||||
|
||||
class ApiDiarioListStar(PageStar):
|
||||
path = "/api/diario/list"
|
||||
tables = {Diario}
|
||||
|
||||
async def page(self, request: Request) -> JSONResponse:
|
||||
page_str = request.query_params.get("page", "0")
|
||||
try:
|
||||
page = int(page_str)
|
||||
except (ValueError, TypeError):
|
||||
return error(400, "Invalid offset")
|
||||
async with self.alchemy.session_acm() as session:
|
||||
if page < 0:
|
||||
page = -page-1
|
||||
entries: typing.List[Diario] = await asyncify(session.query(self.alchemy.Diario).order_by(self.alchemy.Diario.diario_id.desc()).limit(500).offset(page * 500).all)
|
||||
else:
|
||||
entries: typing.List[Diario] = await asyncify(session.query(self.alchemy.Diario).order_by(self.alchemy.Diario.diario_id).limit(500).offset(page * 500).all)
|
||||
response = [entry.json() for entry in entries]
|
||||
return JSONResponse(response)
|
22
royalnet/packs/royal/stars/api_user_get.py
Normal file
22
royalnet/packs/royal/stars/api_user_get.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from starlette.requests import Request
|
||||
from starlette.responses import *
|
||||
from royalnet.web import *
|
||||
from royalnet.utils import *
|
||||
from royalnet.packs.common.tables import User
|
||||
|
||||
|
||||
class ApiUserGetStar(PageStar):
|
||||
path = "/api/user/get/{uid_str}"
|
||||
tables = {User}
|
||||
|
||||
async def page(self, request: Request) -> JSONResponse:
|
||||
uid_str = request.path_params.get("uid_str", "")
|
||||
try:
|
||||
uid = int(uid_str)
|
||||
except (ValueError, TypeError):
|
||||
return error(400, "Invalid uid")
|
||||
async with self.alchemy.session_acm() as session:
|
||||
user: User = await asyncify(session.query(self.alchemy.User).get, uid)
|
||||
if user is None:
|
||||
return error(404, "No such user")
|
||||
return JSONResponse(user.json())
|
15
royalnet/packs/royal/stars/api_user_list.py
Normal file
15
royalnet/packs/royal/stars/api_user_list.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
from starlette.requests import Request
|
||||
from starlette.responses import *
|
||||
from royalnet.web import *
|
||||
from royalnet.utils import *
|
||||
from royalnet.packs.common.tables import User
|
||||
|
||||
|
||||
class ApiUserListStar(PageStar):
|
||||
path = "/api/user/list"
|
||||
tables = {User}
|
||||
|
||||
async def page(self, request: Request) -> JSONResponse:
|
||||
async with self.alchemy.session_acm() as session:
|
||||
users: typing.List[User] = await asyncify(session.query(self.alchemy.User).all)
|
||||
return JSONResponse([user.json() for user in users])
|
|
@ -1,4 +1,5 @@
|
|||
import re
|
||||
import datetime
|
||||
from sqlalchemy import Column, \
|
||||
Integer, \
|
||||
Text, \
|
||||
|
@ -38,7 +39,7 @@ class Diario:
|
|||
return Column(Text)
|
||||
|
||||
@declared_attr
|
||||
def timestamp(self):
|
||||
def timestamp(self) -> datetime.datetime:
|
||||
return Column(DateTime, nullable=False)
|
||||
|
||||
@declared_attr
|
||||
|
@ -57,6 +58,19 @@ class Diario:
|
|||
def quoted_account(self):
|
||||
return relationship("User", foreign_keys=self.quoted_account_id, backref="diario_quoted")
|
||||
|
||||
def json(self) -> dict:
|
||||
return {
|
||||
"diario_id": self.diario_id,
|
||||
"creator": self.creator.json() if self.creator else None,
|
||||
"quoted_account": self.quoted_account.json() if self.quoted_account else None,
|
||||
"quoted": self.quoted,
|
||||
"text": self.text,
|
||||
"context": self.context,
|
||||
"timestamp": self.timestamp.isoformat(),
|
||||
"media_url": self.media_url,
|
||||
"spoiler": self.spoiler
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Diario diario_id={self.diario_id}" \
|
||||
f" creator_id={self.creator_id}" \
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
semantic = "5.0a87"
|
||||
semantic = "5.0a88"
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(semantic)
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
from .constellation import Constellation
|
||||
from .star import Star, PageStar, ExceptionStar
|
||||
from .error import error
|
||||
|
||||
__all__ = [
|
||||
"Constellation",
|
||||
"Star",
|
||||
"PageStar",
|
||||
"ExceptionStar",
|
||||
"error",
|
||||
]
|
||||
|
|
7
royalnet/web/error.py
Normal file
7
royalnet/web/error.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from starlette.responses import JSONResponse
|
||||
|
||||
|
||||
def error(code: int, description: str) -> JSONResponse:
|
||||
return JSONResponse({
|
||||
"error": description
|
||||
}, status_code=code)
|
|
@ -11,7 +11,7 @@ class Star:
|
|||
def __init__(self, constellation: "Constellation"):
|
||||
self.constellation: "Constellation" = constellation
|
||||
|
||||
async def page(self, request: Request, **kwargs) -> Response:
|
||||
async def page(self, request: Request) -> Response:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
|
|
Loading…
Reference in a new issue