This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
46 lines (41 loc) · 1.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from email import message
from typing import Optional
from fastapi import FastAPI, Query, status
from pydantic import BaseModel
from scraper import scrape_notifications
from fastapi.responses import JSONResponse
tags = [
{
"name": "Get Notifications",
"description": "Get a list of notifications.",
}
]
app = FastAPI(
title="KTU API",
description="**KTU API** get the list of notifications available in **KTU's** official website and return it in JSON format 🚀",
version="0.0.1",
contact={
"name": "Akash R Chandran",
"url": "https://links.akashrchandran.in/",
"email": "[email protected]",
},
license_info={
"name": "MIT License",
"url": "https://spdx.org/licenses/MIT.html",
},
openapi_tags=tags
)
class Error_Message(BaseModel):
error: bool
message: str
class Valid_Message(BaseModel):
error: bool
limit: int
data: dict
@app.get("/get", tags=["Get Notifications"], responses={200: {"model": Valid_Message}, 500: {"model": Error_Message}})
async def get_notifications(limit: Optional[int] = Query(10, description="Limit the number of notifications from site using this parameter")):
response = scrape_notifications(limit=limit)
if isinstance(response, list):
return JSONResponse({'error': False, 'limit': limit, "data": response})
else:
return JSONResponse({'error': True, 'message': str(response)}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)