1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
This commit is contained in:
Steffo 2019-11-06 15:54:08 +01:00
parent 7936d5b291
commit c0030e3643
14 changed files with 131 additions and 14 deletions

View file

@ -1,18 +1,14 @@
import discord import discord
import sentry_sdk import sentry_sdk
import logging as _logging import logging as _logging
import asyncio
from .generic import GenericBot from .generic import GenericBot
from ..utils import * from ..utils import *
from ..error import * from ..error import *
from ..audio import * from ..audio import *
from ..commands import * from ..commands import *
log = _logging.getLogger(__name__)
# TODO: Load the opus library log = _logging.getLogger(__name__)
if not discord.opus.is_loaded():
log.error("Opus is not loaded. Weird behaviour might emerge.")
class MusicData: class MusicData:

View file

@ -1,10 +1,10 @@
# Imports go here! # Imports go here!
from .version import VersionStar from .api_royalnet_version import ApiRoyalnetVersionStar
# Enter the PageStars of your Pack here! # Enter the PageStars of your Pack here!
available_page_stars = [ available_page_stars = [
VersionStar, ApiRoyalnetVersionStar,
] ]
# Enter the ExceptionStars of your Pack here! # Enter the ExceptionStars of your Pack here!

View file

@ -4,10 +4,10 @@ from starlette.responses import *
from royalnet.web import PageStar from royalnet.web import PageStar
class VersionStar(PageStar): class ApiRoyalnetVersionStar(PageStar):
path = "/api/royalnet/version" path = "/api/royalnet/version"
async def page(self, request: Request, **kwargs) -> JSONResponse: async def page(self, request: Request) -> JSONResponse:
return JSONResponse({ return JSONResponse({
"version": { "version": {
"semantic": royalnet.version.semantic "semantic": royalnet.version.semantic

View file

@ -28,6 +28,14 @@ class User:
def avatar(self): def avatar(self):
return Column(LargeBinary) return Column(LargeBinary)
def json(self):
return {
"uid": self.uid,
"username": self.username,
"role": self.role,
"avatar": self.avatar
}
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__qualname__} {self.username}>" return f"<{self.__class__.__qualname__} {self.username}>"

View file

@ -1,9 +1,15 @@
# Imports go here! # 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! # Enter the PageStars of your Pack here!
available_page_stars = [ available_page_stars = [
ApiUserListStar,
ApiUserGetStar,
ApiDiarioListStar,
ApiDiarioGetStar,
] ]
# Enter the ExceptionStars of your Pack here! # Enter the ExceptionStars of your Pack here!

View 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())

View 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)

View 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())

View 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])

View file

@ -1,4 +1,5 @@
import re import re
import datetime
from sqlalchemy import Column, \ from sqlalchemy import Column, \
Integer, \ Integer, \
Text, \ Text, \
@ -38,7 +39,7 @@ class Diario:
return Column(Text) return Column(Text)
@declared_attr @declared_attr
def timestamp(self): def timestamp(self) -> datetime.datetime:
return Column(DateTime, nullable=False) return Column(DateTime, nullable=False)
@declared_attr @declared_attr
@ -57,6 +58,19 @@ class Diario:
def quoted_account(self): def quoted_account(self):
return relationship("User", foreign_keys=self.quoted_account_id, backref="diario_quoted") 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): def __repr__(self):
return f"<Diario diario_id={self.diario_id}" \ return f"<Diario diario_id={self.diario_id}" \
f" creator_id={self.creator_id}" \ f" creator_id={self.creator_id}" \

View file

@ -1,4 +1,4 @@
semantic = "5.0a87" semantic = "5.0a88"
if __name__ == "__main__": if __name__ == "__main__":
print(semantic) print(semantic)

View file

@ -1,9 +1,11 @@
from .constellation import Constellation from .constellation import Constellation
from .star import Star, PageStar, ExceptionStar from .star import Star, PageStar, ExceptionStar
from .error import error
__all__ = [ __all__ = [
"Constellation", "Constellation",
"Star", "Star",
"PageStar", "PageStar",
"ExceptionStar", "ExceptionStar",
"error",
] ]

7
royalnet/web/error.py Normal file
View file

@ -0,0 +1,7 @@
from starlette.responses import JSONResponse
def error(code: int, description: str) -> JSONResponse:
return JSONResponse({
"error": description
}, status_code=code)

View file

@ -11,7 +11,7 @@ class Star:
def __init__(self, constellation: "Constellation"): def __init__(self, constellation: "Constellation"):
self.constellation: "Constellation" = constellation self.constellation: "Constellation" = constellation
async def page(self, request: Request, **kwargs) -> Response: async def page(self, request: Request) -> Response:
raise NotImplementedError() raise NotImplementedError()
@property @property