File size: 2,705 Bytes
9e35bf3
 
 
9e61002
58ce433
9e61002
9e35bf3
 
 
 
 
58ce433
e585fe2
58ce433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e585fe2
9e61002
695cdfe
9e61002
 
695cdfe
 
f941793
 
695cdfe
 
9e61002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e35bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5370d2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from fastapi import FastAPI, HTTPException
import urllib.request
from urllib.error import HTTPError
from habanero import Crossref
from fastapi.responses import HTMLResponse


app = FastAPI()

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

@app.get("/", response_class=HTMLResponse)
def read_root():
    html_content = """
    <html>
        <head>
            <title>Citation Helper</title>
        </head>
        <body>
            <h1>Hello!</h1>
            <p>As Citation Helper, I specialize in generating citations for academic papers. You can provide me with the DOI (Digital Object Identifier) or the title of an academic paper, and I will create a citation for it.</p>
            <p>Here's how you can use my service:</p>
            <ul>
                <li><b>Provide a DOI or Title:</b> Share the DOI or the title of the paper you need a citation for.</li>
                <li><b>Choose a Citation Style:</b> After I generate the citation in APA format, you can request it in another style like Chicago, MLA, or Harvard if needed.</li>
                <li><b>Receive Your Citation:</b> I'll provide you with the correctly formatted citation.</li>
            </ul>
            <p>Feel free to start by giving me a DOI or a paper title!</p>
        </body>
    </html>
    """
    return HTMLResponse(content=html_content)

@app.get("/title2ref/")
async def title2doi(title:str):
    cr = Crossref()

    result = cr.works(query = title)
    for i, item in enumerate(result['message']['items']):
        if item['title'][0] == title:
            doi = result['message']['items'][i]['DOI']
            break
    
    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")
            
@app.get("/doi2ref/")
async def doi2ref(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=7860)