diff --git a/gifs/1.gif b/gifs/1.gif new file mode 100644 index 0000000..33fc2e8 Binary files /dev/null and b/gifs/1.gif differ diff --git a/gifs/10.gif b/gifs/10.gif new file mode 100644 index 0000000..e1ab788 Binary files /dev/null and b/gifs/10.gif differ diff --git a/gifs/11.gif b/gifs/11.gif new file mode 100644 index 0000000..0d56192 Binary files /dev/null and b/gifs/11.gif differ diff --git a/gifs/12.gif b/gifs/12.gif new file mode 100644 index 0000000..2868e71 Binary files /dev/null and b/gifs/12.gif differ diff --git a/gifs/13.gif b/gifs/13.gif new file mode 100644 index 0000000..b092045 Binary files /dev/null and b/gifs/13.gif differ diff --git a/gifs/14.gif b/gifs/14.gif new file mode 100644 index 0000000..0067713 Binary files /dev/null and b/gifs/14.gif differ diff --git a/gifs/15.gif b/gifs/15.gif new file mode 100644 index 0000000..3703dc4 Binary files /dev/null and b/gifs/15.gif differ diff --git a/gifs/16.gif b/gifs/16.gif new file mode 100644 index 0000000..1a893cc Binary files /dev/null and b/gifs/16.gif differ diff --git a/gifs/17.gif b/gifs/17.gif new file mode 100644 index 0000000..03f209e Binary files /dev/null and b/gifs/17.gif differ diff --git a/gifs/18.gif b/gifs/18.gif new file mode 100644 index 0000000..6d4adf6 Binary files /dev/null and b/gifs/18.gif differ diff --git a/gifs/19.gif b/gifs/19.gif new file mode 100644 index 0000000..329e639 Binary files /dev/null and b/gifs/19.gif differ diff --git a/gifs/2.gif b/gifs/2.gif new file mode 100644 index 0000000..13b85d3 Binary files /dev/null and b/gifs/2.gif differ diff --git a/gifs/20.gif b/gifs/20.gif new file mode 100644 index 0000000..ab9ed8e Binary files /dev/null and b/gifs/20.gif differ diff --git a/gifs/21.gif b/gifs/21.gif new file mode 100644 index 0000000..7e7696d Binary files /dev/null and b/gifs/21.gif differ diff --git a/gifs/22.gif b/gifs/22.gif new file mode 100644 index 0000000..00440a8 Binary files /dev/null and b/gifs/22.gif differ diff --git a/gifs/23.gif b/gifs/23.gif new file mode 100644 index 0000000..1f72037 Binary files /dev/null and b/gifs/23.gif differ diff --git a/gifs/24.gif b/gifs/24.gif new file mode 100644 index 0000000..20709a6 Binary files /dev/null and b/gifs/24.gif differ diff --git a/gifs/25.gif b/gifs/25.gif new file mode 100644 index 0000000..66178ce Binary files /dev/null and b/gifs/25.gif differ diff --git a/gifs/26.gif b/gifs/26.gif new file mode 100644 index 0000000..53a1209 Binary files /dev/null and b/gifs/26.gif differ diff --git a/gifs/27.gif b/gifs/27.gif new file mode 100644 index 0000000..c2bd484 Binary files /dev/null and b/gifs/27.gif differ diff --git a/gifs/28.gif b/gifs/28.gif new file mode 100644 index 0000000..3ded267 Binary files /dev/null and b/gifs/28.gif differ diff --git a/gifs/29.gif b/gifs/29.gif new file mode 100644 index 0000000..5dd358a Binary files /dev/null and b/gifs/29.gif differ diff --git a/gifs/3.gif b/gifs/3.gif new file mode 100644 index 0000000..1161eac Binary files /dev/null and b/gifs/3.gif differ diff --git a/gifs/30.gif b/gifs/30.gif new file mode 100644 index 0000000..ffb7d94 Binary files /dev/null and b/gifs/30.gif differ diff --git a/gifs/31.gif b/gifs/31.gif new file mode 100644 index 0000000..aca17c6 Binary files /dev/null and b/gifs/31.gif differ diff --git a/gifs/32.gif b/gifs/32.gif new file mode 100644 index 0000000..e436875 Binary files /dev/null and b/gifs/32.gif differ diff --git a/gifs/33.gif b/gifs/33.gif new file mode 100644 index 0000000..82ff9cf Binary files /dev/null and b/gifs/33.gif differ diff --git a/gifs/34.gif b/gifs/34.gif new file mode 100644 index 0000000..e9050f4 Binary files /dev/null and b/gifs/34.gif differ diff --git a/gifs/35.gif b/gifs/35.gif new file mode 100644 index 0000000..9d4a663 Binary files /dev/null and b/gifs/35.gif differ diff --git a/gifs/36.gif b/gifs/36.gif new file mode 100644 index 0000000..b516acc Binary files /dev/null and b/gifs/36.gif differ diff --git a/gifs/37.gif b/gifs/37.gif new file mode 100644 index 0000000..48bb1b2 Binary files /dev/null and b/gifs/37.gif differ diff --git a/gifs/38.gif b/gifs/38.gif new file mode 100644 index 0000000..29ebeaa Binary files /dev/null and b/gifs/38.gif differ diff --git a/gifs/39.gif b/gifs/39.gif new file mode 100644 index 0000000..178385e Binary files /dev/null and b/gifs/39.gif differ diff --git a/gifs/4.gif b/gifs/4.gif new file mode 100644 index 0000000..d59b324 Binary files /dev/null and b/gifs/4.gif differ diff --git a/gifs/40.gif b/gifs/40.gif new file mode 100644 index 0000000..bd5bef6 Binary files /dev/null and b/gifs/40.gif differ diff --git a/gifs/41.gif b/gifs/41.gif new file mode 100644 index 0000000..d241892 Binary files /dev/null and b/gifs/41.gif differ diff --git a/gifs/42.gif b/gifs/42.gif new file mode 100644 index 0000000..dc11f1a Binary files /dev/null and b/gifs/42.gif differ diff --git a/gifs/43.gif b/gifs/43.gif new file mode 100644 index 0000000..ed501dd Binary files /dev/null and b/gifs/43.gif differ diff --git a/gifs/44.gif b/gifs/44.gif new file mode 100644 index 0000000..fb7e477 Binary files /dev/null and b/gifs/44.gif differ diff --git a/gifs/45.gif b/gifs/45.gif new file mode 100644 index 0000000..820dcc1 Binary files /dev/null and b/gifs/45.gif differ diff --git a/gifs/46.gif b/gifs/46.gif new file mode 100644 index 0000000..0adae0a Binary files /dev/null and b/gifs/46.gif differ diff --git a/gifs/47.gif b/gifs/47.gif new file mode 100644 index 0000000..7285ff7 Binary files /dev/null and b/gifs/47.gif differ diff --git a/gifs/48.gif b/gifs/48.gif new file mode 100644 index 0000000..d1f1bff Binary files /dev/null and b/gifs/48.gif differ diff --git a/gifs/49.gif b/gifs/49.gif new file mode 100644 index 0000000..a78f15a Binary files /dev/null and b/gifs/49.gif differ diff --git a/gifs/5.gif b/gifs/5.gif new file mode 100644 index 0000000..35a70bd Binary files /dev/null and b/gifs/5.gif differ diff --git a/gifs/50.gif b/gifs/50.gif new file mode 100644 index 0000000..ef6c02e Binary files /dev/null and b/gifs/50.gif differ diff --git a/gifs/51.gif b/gifs/51.gif new file mode 100644 index 0000000..056ea71 Binary files /dev/null and b/gifs/51.gif differ diff --git a/gifs/52.gif b/gifs/52.gif new file mode 100644 index 0000000..02b15f2 Binary files /dev/null and b/gifs/52.gif differ diff --git a/gifs/53.gif b/gifs/53.gif new file mode 100644 index 0000000..6a7896c Binary files /dev/null and b/gifs/53.gif differ diff --git a/gifs/54.gif b/gifs/54.gif new file mode 100644 index 0000000..97e92ee Binary files /dev/null and b/gifs/54.gif differ diff --git a/gifs/55.gif b/gifs/55.gif new file mode 100644 index 0000000..9aefdad Binary files /dev/null and b/gifs/55.gif differ diff --git a/gifs/56.gif b/gifs/56.gif new file mode 100644 index 0000000..d3bc487 Binary files /dev/null and b/gifs/56.gif differ diff --git a/gifs/57.gif b/gifs/57.gif new file mode 100644 index 0000000..bd2ab50 Binary files /dev/null and b/gifs/57.gif differ diff --git a/gifs/58.gif b/gifs/58.gif new file mode 100644 index 0000000..fd2626f Binary files /dev/null and b/gifs/58.gif differ diff --git a/gifs/59.gif b/gifs/59.gif new file mode 100644 index 0000000..61e0af6 Binary files /dev/null and b/gifs/59.gif differ diff --git a/gifs/6.gif b/gifs/6.gif new file mode 100644 index 0000000..6013521 Binary files /dev/null and b/gifs/6.gif differ diff --git a/gifs/60.gif b/gifs/60.gif new file mode 100644 index 0000000..f10067e Binary files /dev/null and b/gifs/60.gif differ diff --git a/gifs/61.gif b/gifs/61.gif new file mode 100644 index 0000000..78742de Binary files /dev/null and b/gifs/61.gif differ diff --git a/gifs/62.gif b/gifs/62.gif new file mode 100644 index 0000000..1433b8c Binary files /dev/null and b/gifs/62.gif differ diff --git a/gifs/63.gif b/gifs/63.gif new file mode 100644 index 0000000..f50d58e Binary files /dev/null and b/gifs/63.gif differ diff --git a/gifs/64.gif b/gifs/64.gif new file mode 100644 index 0000000..d37de51 Binary files /dev/null and b/gifs/64.gif differ diff --git a/gifs/65.gif b/gifs/65.gif new file mode 100644 index 0000000..49a7822 Binary files /dev/null and b/gifs/65.gif differ diff --git a/gifs/66.gif b/gifs/66.gif new file mode 100644 index 0000000..ff99104 Binary files /dev/null and b/gifs/66.gif differ diff --git a/gifs/7.gif b/gifs/7.gif new file mode 100644 index 0000000..9677922 Binary files /dev/null and b/gifs/7.gif differ diff --git a/gifs/8.gif b/gifs/8.gif new file mode 100644 index 0000000..23c96e3 Binary files /dev/null and b/gifs/8.gif differ diff --git a/gifs/9.gif b/gifs/9.gif new file mode 100644 index 0000000..2bd44b7 Binary files /dev/null and b/gifs/9.gif differ diff --git a/main.py b/main.py index 3a963fb..7363390 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,8 @@ panda_pics = os.listdir("images") +panda_gifs = os.listdir("gifs") + def create_cache(): return TTLCache(maxsize=100, ttl=3600) @@ -46,6 +48,12 @@ def get_random_pic(request: Request): ) +@app.get("/gif", tags=["gifs"]) +def get_random_gif(request: Request): + return JSONResponse( + content={"url": f"{request.base_url}g/{random.choice(panda_gifs)}"} + ) + @app.get("/raw_pic", tags=["pics"]) async def get_random_pic_raw(): random_pic_path = f"images/{random.choice(panda_pics)}" @@ -54,12 +62,21 @@ async def get_random_pic_raw(): return StreamingResponse(BytesIO(contents), media_type="image/jpeg") -@app.get("/both", tags=["facts", "pics"]) +@app.get("/raw_gif", tags=["gifs"]) +async def get_random_gif_raw(): + random_gif_path = f"gifs/{random.choice(panda_gifs)}" + with open(random_gif_path, "rb") as file: + contents = file.read() + return StreamingResponse(BytesIO(contents), media_type="image/gif") + + +@app.get("/all", tags=["facts", "pics", "gifs"]) def get_random_fact_and_pic(request: Request): return JSONResponse( content={ "fact": random.choice(panda_facts), "pic": f"{request.base_url}i/{random.choice(panda_pics)}", + "gif": f"{request.base_url}g/{random.choice(panda_gifs)}", } ) @@ -75,6 +92,12 @@ def get_all_pics(request: Request): return JSONResponse(content={"pics": pics}) +@app.get("/all-gifs", tags=["gifs"]) +def get_all_gifs(request: Request): + gifs = [f"{request.base_url}g/{i}" for i in panda_gifs] + return JSONResponse(content={"gifs": gifs}) + + @app.get("/i/{file_name}", tags=["pics"]) async def get_image(file_name: str, cache: TTLCache = Depends(create_cache)): file_path = f"images/{file_name}" @@ -89,16 +112,35 @@ async def get_image(file_name: str, cache: TTLCache = Depends(create_cache)): cache[file_name] = response return response + +@app.get("/g/{file_name}", tags=["gifs"]) +async def get_gif(file_name: str, cache: TTLCache = Depends(create_cache)): + file_path = f"gifs/{file_name}" + if not os.path.isfile(file_path): + raise HTTPException(status_code=404, detail="File not found") + + cached_response = cache.get(file_name) + if cached_response: + return cached_response + + response = FileResponse(file_path, media_type="image/gif") + cache[file_name] = response + return response + + @app.get('/health', tags=["health"]) def health(): return JSONResponse(content={"status": "ok"}) + @app.get("/stats", tags=["stats"]) def get_stats(): num_images = len(panda_pics) num_quotes = len(panda_facts) + num_gifs = len(panda_gifs) + + return JSONResponse(content={"num_images": num_images, "num_gifs": num_gifs, "num_quotes": num_quotes}) - return JSONResponse(content={"num_images": num_images, "num_quotes": num_quotes}) if __name__ == "__main__": - uvicorn.run(app, port=8000) \ No newline at end of file + uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file