Imported RoyalRoad chapter checker, changed it to use FastAPI with metrics and uses their RSS feed instead of scraping with the FanFicFare module
This commit is contained in:
1 parent
4ebf3c1df8
commit
36a8767349
7 files changed
+263
No files matched your search
+80
@@ -0,0 +1,80 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException, Response, Request
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from db import get_db, create_connection_pool, close_connection_pool
|
||||
import logging
|
||||
from feed_handler import grab_latest_chapter_information
|
||||
from send_notification import send_notification
|
||||
from metrics_server import REQUEST_COUNTER
|
||||
import asyncio
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Starting application...")
|
||||
|
||||
logger.info("Creating MySQL connection pool...")
|
||||
create_connection_pool()
|
||||
|
||||
yield
|
||||
logger.info("Closing MySQL connection pool...")
|
||||
close_connection_pool()
|
||||
|
||||
api = FastAPI(
|
||||
title="Docker Repository Query",
|
||||
description="Queries Dockerhub and GHCR for new docker images",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
@api.middleware("http")
|
||||
async def prometheus_middleware(request: Request, call_next):
|
||||
status = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status = response.status_code
|
||||
except Exception:
|
||||
raise
|
||||
finally:
|
||||
REQUEST_COUNTER.labels(request.method, request.url.path, status).inc()
|
||||
return response
|
||||
|
||||
|
||||
@api.get("/health")
|
||||
def return_health():
|
||||
return Response(status_code=200)
|
||||
|
||||
@api.get("/royalroad")
|
||||
def get_chapters(
|
||||
request: Request,
|
||||
db = Depends(get_db)
|
||||
):
|
||||
try:
|
||||
logger.info("Checking for new Royalroad chapters")
|
||||
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT id,royalroadId,lastChapter FROM stories where active=1")
|
||||
for id,royalroadId,last_chapter_db in cursor.fetchall():
|
||||
chapter_number,chapter_link,title = grab_latest_chapter_information(royalroadId)
|
||||
if chapter_number > last_chapter_db:
|
||||
cursor.execute("UPDATE stories SET lastChapter = %s WHERE id = %s",
|
||||
(chapter_number, id))
|
||||
db.commit()
|
||||
send_notification(title,chapter_number,chapter_link)
|
||||
return {"status": "checked"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
async def start_servers():
|
||||
config_main = uvicorn.Config("main:api", host="0.0.0.0", port=5000, log_level="info")
|
||||
config_metrics = uvicorn.Config("metrics_server:metrics_api", host="0.0.0.0", port=9000, log_level="info")
|
||||
|
||||
server_main = uvicorn.Server(config_main)
|
||||
server_metrics = uvicorn.Server(config_metrics)
|
||||
|
||||
await asyncio.gather(server_main.serve(), server_metrics.serve())
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(start_servers())
|
||||
Reference in new issue
Block a user