Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
import urllib.request
|
| 3 |
+
from urllib.error import HTTPError
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
BASE_URL = 'https://doi.org/'
|
| 8 |
+
|
| 9 |
+
@app.get("/get_bibtex/")
|
| 10 |
+
async def get_bibtex(doi: str):
|
| 11 |
+
url = BASE_URL + doi
|
| 12 |
+
req = urllib.request.Request(url)
|
| 13 |
+
req.add_header('Accept', 'application/x-bibtex')
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
with urllib.request.urlopen(req) as f:
|
| 17 |
+
bibtex = f.read().decode()
|
| 18 |
+
return {"bibtex": bibtex}
|
| 19 |
+
except HTTPError as e:
|
| 20 |
+
if e.code == 404:
|
| 21 |
+
raise HTTPException(status_code=404, detail="DOI not found")
|
| 22 |
+
else:
|
| 23 |
+
raise HTTPException(status_code=503, detail="Service unavailable")
|
| 24 |
+
|
| 25 |
+
if __name__ == "__main__":
|
| 26 |
+
import uvicorn
|
| 27 |
+
|
| 28 |
+
uvicorn.run("main:app", host="0.0.0.0", port=80)
|