Skip to content

Commit

Permalink
add contributor list
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Nov 9, 2024
1 parent b6c5c9e commit 3a87484
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,30 @@ async def get_channel_name(platform: str, id: str, token: Annotated[str, fastapi
except:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This channel does not exist."})


@api.get("/contribution/contributors", description="Get contributors that worked on Astroid.")
async def get_contributors():
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributors()

@api.get("/contribution/contributor", description="Get a contributor that worked on Astroid.")
async def get_contributor(name: str = None, id: int = None):
if name and not id:
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor_by_username(name)
elif id and not name:
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor(id)
elif id and name:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You can only provide either a name or an id."})
else:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You must provide a name or an id."})

@api.post("/contribution/addcontributor/{id}", description="Add a contributor that worked on Astroid.")
async def add_contributor(id: int, username: str = None, avatar: str = None, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
if token == Bot.config.MASTER_TOKEN:
return await astroidapi.surrealdb_handler.Contributions.Contributors.create_contributor(id, username, avatar)
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})


logging.info("[CORE] API started.")

uvicorn.run(api, host="localhost", port=9921)
15 changes: 15 additions & 0 deletions src/astroidapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ def __init__(self, message):
self.message = message
super().__init__(self.message)

class GetContributorError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class GetContributorsError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class CreateContributorError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class ReadHandlerError:

Expand Down
5 changes: 2 additions & 3 deletions src/astroidapi/get_channel_information.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from Bot import config
from astroidapi import beta_config
import aiohttp
import asyncio
import traceback
Expand All @@ -21,7 +20,7 @@ async def from_discord_id(cls, channel_id: int):
try:
async with aiohttp.ClientSession() as session:
headers = {
"Authorization": f"Bot {beta_config.DISCORD_TOKEN}"
"Authorization": f"Bot {config.DISCORD_TOKEN}"
}
async with session.get(f"https://discord.com/api/v9/channels/{channel_id}", headers=headers) as resp:
data = await resp.json()
Expand All @@ -46,7 +45,7 @@ async def from_guilded_id(cls, channel_id: str):
try:
async with aiohttp.ClientSession() as session:
headers = {
"Authorization": f"Bearer {beta_config.GUILDED_TOKEN}"
"Authorization": f"Bearer {config.GUILDED_TOKEN}"
}
async with session.get(f"https://www.guilded.gg/api/v1/channels/{channel_id}", headers=headers) as resp:
print(await resp.text())
Expand Down
63 changes: 62 additions & 1 deletion src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,65 @@ async def _checkendpointdatadeletionloop():
return True
except Exception as e:
raise errors.SurrealDBHandler.SuspensionHandlerError(e)



class Contributions:

class Contributors:

@classmethod
async def get_contributors(cls):
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.CONTRIBUTIONS_DATABASE)
return await db.select("contributors")
except Exception as e:
raise errors.SurrealDBHandler.GetContributorsError(e)

@classmethod
async def get_contributor(cls, contributor_id: int):
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.CONTRIBUTIONS_DATABASE)
return await db.select(f"contributors:`{contributor_id}`")
except Exception as e:
raise errors.SurrealDBHandler.GetContributorError(e)

@classmethod
async def get_contributor_by_username(cls, username: str):
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.CONTRIBUTIONS_DATABASE)
data = await db.select("contributors")
print(data)
for contributor in data:
if contributor["username"] == username:
return contributor
except Exception as e:
raise errors.SurrealDBHandler.GetContributorError(e)

@classmethod
async def create_contributor(cls, userid: int, username: str = None, avatar: str = None):
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.CONTRIBUTIONS_DATABASE)
contributors = await db.select("contributors")
for contributor in contributors:
if str(contributor["id"]).replace("contributors:⟨", "").replace("⟩", "")== str(userid):
await db.update(f"contributors:⟨{userid}⟩", {
"username": username,
"avatar": avatar
})
return await db.select(f"contributors:⟨{userid}⟩")
await db.create(f"contributors:⟨{userid}⟩`", {
"username": username,
"avatar": avatar,
"contributor_since": datetime.datetime.now().timestamp(),
})
return await db.select(f"contributions:⟨{userid}⟩")
except Exception as e:
raise errors.SurrealDBHandler.CreateContributorError(e)

0 comments on commit 3a87484

Please sign in to comment.