add mastodon statuses proxy
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
from fastapi import Depends, FastAPI
|
from fastapi import Depends, FastAPI
|
||||||
from routers import embed, yle_rss_fi, yle_rss_en, yle_rss_sv, the_local, taloustaito,sur,hackernews,fuengirola
|
from routers import embed, yle_rss_fi, yle_rss_en, yle_rss_sv, the_local, taloustaito, sur, hackernews, fuengirola, \
|
||||||
|
statuses
|
||||||
|
|
||||||
from settings.defaults import get_settings
|
from settings.defaults import get_settings
|
||||||
|
|
||||||
app = FastAPI(title='Mastobot', description='Mastodon Feed Automation Service', version=get_settings().version)
|
app = FastAPI(title='Mastobot', description='Mastodon Feed Automation Service', version=get_settings().version)
|
||||||
|
|
||||||
app.include_router(embed.router, prefix="/embed", tags=["embed"])
|
app.include_router(embed.router, prefix="/embed", tags=["embed"])
|
||||||
|
app.include_router(statuses.router, prefix="/statuses", tags=["proxy"])
|
||||||
|
|
||||||
app.include_router(yle_rss_fi.router, prefix="/rss", tags=["rss"])
|
app.include_router(yle_rss_fi.router, prefix="/rss", tags=["rss"])
|
||||||
app.include_router(yle_rss_en.router, prefix="/rss", tags=["rss"])
|
app.include_router(yle_rss_en.router, prefix="/rss", tags=["rss"])
|
||||||
|
|||||||
44
app/routers/statuses.py
Normal file
44
app/routers/statuses.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import logging
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from fastapi import Depends, APIRouter, HTTPException, Request, Response, status
|
||||||
|
|
||||||
|
from settings.defaults import Settings, get_settings
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@router.get("/{account_id}", summary="Fetch Mastodon Statuses")
|
||||||
|
async def get_statuses(account_id: str, request:Request, settings: Annotated[Settings, Depends(get_settings)]):
|
||||||
|
|
||||||
|
if account_id not in settings.mastodon:
|
||||||
|
logger.error('Account %s not found', account_id)
|
||||||
|
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail="Account not found.")
|
||||||
|
|
||||||
|
mastodon_account = settings.mastodon[account_id]
|
||||||
|
mastodon_server = settings.mastodon_server
|
||||||
|
mastodon_get_statuses_url = f'{mastodon_server}/api/v1/accounts/{account_id}/statuses'
|
||||||
|
token = mastodon_account['token']
|
||||||
|
headers = {
|
||||||
|
'Authorization': f'Bearer {token}',
|
||||||
|
'Content-type': 'application/json',
|
||||||
|
'User-Agent': 'Serverless Feed'
|
||||||
|
}
|
||||||
|
query_params = dict(request.query_params)
|
||||||
|
response = requests.get(mastodon_get_statuses_url, headers=headers, params=query_params)
|
||||||
|
if response.status_code != 200:
|
||||||
|
logger.error('Failed to get statuses', response)
|
||||||
|
raise HTTPException(status_code=response.status_code, detail=response.text)
|
||||||
|
|
||||||
|
forwarded_headers = {
|
||||||
|
'Content-type': 'application/json',
|
||||||
|
'User-Agent': 'Serverless Feed'
|
||||||
|
}
|
||||||
|
|
||||||
|
if 'link' in response.headers:
|
||||||
|
forwarded_headers["Access-Control-Expose-Headers"] = 'link'
|
||||||
|
forwarded_headers['link'] = response.headers['link'].replace(mastodon_get_statuses_url, str(request.base_url) + 'statuses/' + account_id)
|
||||||
|
|
||||||
|
return Response(content=response.content, status_code=response.status_code, headers=forwarded_headers)
|
||||||
|
|
||||||
@@ -3,6 +3,7 @@ from functools import lru_cache
|
|||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
mastodon_server:str
|
mastodon_server:str
|
||||||
|
mastodon: dict[str, dict[str, object]]
|
||||||
openai_api_key:str
|
openai_api_key:str
|
||||||
aws_access_key_id: str
|
aws_access_key_id: str
|
||||||
aws_secret_access_key: str
|
aws_secret_access_key: str
|
||||||
|
|||||||
Reference in New Issue
Block a user