This repository has been archived on 2021-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
pubspace/pubspace.py

69 lines
1.7 KiB
Python
Raw Normal View History

2021-06-16 09:07:52 +00:00
import logging
from os import environ
2021-06-16 13:26:04 +00:00
import owncloud
2021-06-16 09:07:52 +00:00
from fastapi import FastAPI, Request
from mastodon import Mastodon, StreamListener
MASTODON_ACCESS_TOKEN = environ.get("MASTODON_ACCESS_TOKEN")
MASTODON_API_BASE_URL = environ.get("MASTODON_API_BASE_URL")
2021-06-16 13:26:04 +00:00
NEXTCLOUD_API_BASE_URL = environ.get("NEXTCLOUD_API_BASE_URL")
NEXTCLOUD_USER = environ.get("NEXTCLOUD_USER")
NEXTCLOUD_APP_PASSWORD = environ.get("NEXTCLOUD_APP_PASSWORD")
2021-06-16 09:07:52 +00:00
APP_LOG_LEVEL = environ.get("APP_LOG_LEVEL")
if APP_LOG_LEVEL == "info":
APP_LOG_LEVEL = logging.INFO
elif APP_LOG_LEVEL == "debug":
APP_LOG_LEVEL = logging.DEBUG
else:
APP_LOG_LEVEL = logging.INFO
mastodon = Mastodon(
access_token=MASTODON_ACCESS_TOKEN,
api_base_url=f"https://{MASTODON_API_BASE_URL}",
)
2021-06-16 13:26:04 +00:00
nextcloud = owncloud.Client(f"https://{NEXTCLOUD_API_BASE_URL}")
2021-06-16 09:07:52 +00:00
log = logging.getLogger("uvicorn")
log.setLevel(APP_LOG_LEVEL)
app = FastAPI()
app.state.log = log
class PubspaceListener(StreamListener):
def on_update(self, status):
log.info(status)
mastodon.stream_hashtag("pubspace", PubspaceListener(), run_async=True)
2021-06-16 13:26:04 +00:00
nextcloud.login(NEXTCLOUD_USER, NEXTCLOUD_APP_PASSWORD)
def create_share(fpath):
if not nextcloud.is_shared(fpath):
info = nextcloud.share_file_with_link(fpath)
return info.get_link()
return None
2021-06-16 09:07:52 +00:00
@app.get("/")
async def home(request: Request):
2021-06-16 09:32:14 +00:00
try:
2021-06-16 13:26:04 +00:00
payload = await request.json()
file = payload["file"]
link = create_share(file)
if link:
app.state.log(f"Shared {file} on {link}")
else:
app.state.log(f"{file} has already been shared!")
2021-06-16 09:32:14 +00:00
except Exception:
return {}
2021-06-16 09:17:32 +00:00
@app.get("/healthz")
async def healthz(request: Request):
return {"detail": "ALL ENGINES FIRING"}