File size: 771 Bytes
9e35bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from fastapi import FastAPI, HTTPException
import urllib.request
from urllib.error import HTTPError

app = FastAPI()

BASE_URL = 'https://doi.org/'

@app.get("/get_bibtex/")
async def get_bibtex(doi: str):
    url = BASE_URL + doi
    req = urllib.request.Request(url)
    req.add_header('Accept', 'application/x-bibtex')

    try:
        with urllib.request.urlopen(req) as f:
            bibtex = f.read().decode()
        return {"bibtex": bibtex}
    except HTTPError as e:
        if e.code == 404:
            raise HTTPException(status_code=404, detail="DOI not found")
        else:
            raise HTTPException(status_code=503, detail="Service unavailable")

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", host="0.0.0.0", port=80)