zliang commited on
Commit
9e61002
1 Parent(s): a5370d2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -2
main.py CHANGED
@@ -1,13 +1,35 @@
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')
 
1
  from fastapi import FastAPI, HTTPException
2
  import urllib.request
3
  from urllib.error import HTTPError
4
+ from habanero import Crossref
5
+
6
 
7
  app = FastAPI()
8
 
9
  BASE_URL = 'https://doi.org/'
10
 
11
+ @app.get("/title2ref/")
12
+ async def ttile2doi(title:str):
13
+ cr = Crossref()
14
+
15
+ result = cr.works(query = "title")
16
+ doi = result['message']['items'][0]['DOI']
17
+ url = BASE_URL + doi
18
+ req = urllib.request.Request(url)
19
+ req.add_header('Accept', 'application/x-bibtex')
20
+
21
+ try:
22
+ with urllib.request.urlopen(req) as f:
23
+ bibtex = f.read().decode()
24
+ return {"bibtex": bibtex}
25
+ except HTTPError as e:
26
+ if e.code == 404:
27
+ raise HTTPException(status_code=404, detail="DOI not found")
28
+ else:
29
+ raise HTTPException(status_code=503, detail="Service unavailable")
30
+
31
+ @app.get("/doi2ref/")
32
+ async def doi2ref(doi: str):
33
  url = BASE_URL + doi
34
  req = urllib.request.Request(url)
35
  req.add_header('Accept', 'application/x-bibtex')