Skip to content

Commit

Permalink
Add Random Panda GIFs support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zingzy committed Jan 19, 2024
1 parent 41c94fc commit 28a7a65
Show file tree
Hide file tree
Showing 67 changed files with 45 additions and 3 deletions.
Binary file added gifs/1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/10.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/11.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/12.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/13.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/14.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/15.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/16.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/17.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/18.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/19.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/20.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/21.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/22.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/23.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/24.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/25.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/26.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/27.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/28.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/29.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/30.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/31.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/32.gif
Binary file added gifs/33.gif
Binary file added gifs/34.gif
Binary file added gifs/35.gif
Binary file added gifs/36.gif
Binary file added gifs/37.gif
Binary file added gifs/38.gif
Binary file added gifs/39.gif
Binary file added gifs/4.gif
Binary file added gifs/40.gif
Binary file added gifs/41.gif
Binary file added gifs/42.gif
Binary file added gifs/43.gif
Binary file added gifs/44.gif
Binary file added gifs/45.gif
Binary file added gifs/46.gif
Binary file added gifs/47.gif
Binary file added gifs/48.gif
Binary file added gifs/49.gif
Binary file added gifs/5.gif
Binary file added gifs/50.gif
Binary file added gifs/51.gif
Binary file added gifs/52.gif
Binary file added gifs/53.gif
Binary file added gifs/54.gif
Binary file added gifs/55.gif
Binary file added gifs/56.gif
Binary file added gifs/57.gif
Binary file added gifs/58.gif
Binary file added gifs/59.gif
Binary file added gifs/6.gif
Binary file added gifs/60.gif
Binary file added gifs/61.gif
Binary file added gifs/62.gif
Binary file added gifs/63.gif
Binary file added gifs/64.gif
Binary file added gifs/65.gif
Binary file added gifs/66.gif
Binary file added gifs/7.gif
Binary file added gifs/8.gif
Binary file added gifs/9.gif
48 changes: 45 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

panda_pics = os.listdir("images")

panda_gifs = os.listdir("gifs")


def create_cache():
return TTLCache(maxsize=100, ttl=3600)
Expand All @@ -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)}"
Expand All @@ -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)}",
}
)

Expand All @@ -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}"
Expand All @@ -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)
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit 28a7a65

Please sign in to comment.