ruff reformat

This commit is contained in:
boyska 2024-12-07 20:11:38 +01:00
parent 6acfd2fc83
commit abe521927e
3 changed files with 42 additions and 35 deletions

View file

@ -104,7 +104,7 @@ class ArchiveBackend:
def get_todo(args) -> list[dict]:
resp = requests.get(args.private_url + '/api/todo/archive.org').json()
resp = requests.get(args.private_url + "/api/todo/archive.org").json()
return resp["files"]
@ -120,12 +120,12 @@ def loop(args):
def process(file, args):
backend = ArchiveBackend(
args.config['auth']['accesskey'],
args.config['auth']['secretkey'],
)
args.config["auth"]["accesskey"],
args.config["auth"]["secretkey"],
)
print(file)
real_file = Path(CONFIG['general']['files']) / file['filepath']
real_file = Path(CONFIG["general"]["files"]) / file["filepath"]
print(real_file)
if not real_file.exists():
LOG.warn("File not found: %s", real_file)
@ -135,30 +135,38 @@ def process(file, args):
url = backend.copy(real_file)
archived = database.Archived(
original_id=file['id'],
link=url,
archive='archive.org',
archive_time=int(datetime.datetime.now().strftime('%s')),
# all this is possibly wrong: we should look at the real metadata
sha256=file['sha256'],
size=file['size'],
format=file['mime'],
)
original_id=file["id"],
link=url,
archive="archive.org",
archive_time=int(datetime.datetime.now().strftime("%s")),
# all this is possibly wrong: we should look at the real metadata
sha256=file["sha256"],
size=file["size"],
format=file["mime"],
)
return archived
def main():
p = ArgumentParser()
p.add_argument('--private-url', default='http://127.0.0.1:8000',
help='URL of the private web server providing the /api/todo endpoint')
p.add_argument('--period', default=30, type=int)
p.add_argument('--config', type=lambda fname: toml.load(open(fname)), required=True,
help='config specific to the archiveorg uploader. Not to be confused with CARICARI_CONFIG')
p.add_argument(
"--private-url",
default="http://127.0.0.1:8000",
help="URL of the private web server providing the /api/todo endpoint",
)
p.add_argument("--period", default=30, type=int)
p.add_argument(
"--config",
type=lambda fname: toml.load(open(fname)),
required=True,
help="config specific to the archiveorg uploader. Not to be confused with CARICARI_CONFIG",
)
args = p.parse_args()
while True:
loop(args)
time.sleep(args.period)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View file

@ -30,9 +30,11 @@ engine = create_engine(CONFIG["general"]["db"])
database.metadata.create_all(engine)
session_pool = sessionmaker(bind=engine)
app.mount("/static",
StaticFiles(directory=str(Path(__file__).parent / "static")),
name="static")
app.mount(
"/static",
StaticFiles(directory=str(Path(__file__).parent / "static")),
name="static",
)
@app.get("/")
@ -61,10 +63,8 @@ def normalize_filename(filename: str):
>>> normalize_filename("Hey, do you want to hear my story?" * 10)
'HeydoyouwanttohearmystoryHeydo'
"""
return "".join(
c for c in filename
if c.isalnum() or c in '-_'
)[:40]
return "".join(c for c in filename if c.isalnum() or c in "-_")[:40]
@app.post("/upload")
async def upload(
@ -150,9 +150,8 @@ def list(request: Request):
if "application/json" in request.headers.get("accept", ""):
return data
return templates.TemplateResponse(
name="list.html", request=request, context=data
)
return templates.TemplateResponse(name="list.html", request=request, context=data)
@app.get("/api/todo/{archive}")
def todo(request: Request, archive: str):

View file

@ -24,14 +24,15 @@ session_pool = sessionmaker(bind=engine)
def home():
return "public archive"
def get_format(path: str, request: Request) -> str:
# XXX: add some logic involving Accept, user-agent, etc.
return path.split('.')[-1]
return path.split(".")[-1]
@app.get("/dl/{path:path}")
async def get_file(request: Request, path: str, fmt: str = 'auto'):
if fmt == 'auto':
async def get_file(request: Request, path: str, fmt: str = "auto"):
if fmt == "auto":
fmt = get_format(path, request)
with session_pool() as conn:
original = (
@ -56,8 +57,7 @@ async def get_file(request: Request, path: str, fmt: str = 'auto'):
return 404
# look for the "best" archived version
right_format = [arc for arc in original.archived
if arc.format == fmt]
right_format = [arc for arc in original.archived if arc.format == fmt]
if right_format:
return RedirectResponse(right_format[0].link)
return RedirectResponse(original.archived[0].link)