-
Notifications
You must be signed in to change notification settings - Fork 28
/
mendeley-example.py
106 lines (66 loc) · 2.44 KB
/
mendeley-example.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask, redirect, render_template, request, session
import yaml
from mendeley import Mendeley
from mendeley.session import MendeleySession
with open('config.yml') as f:
config = yaml.load(f)
REDIRECT_URI = 'http://localhost:5000/oauth'
app = Flask(__name__)
app.debug = True
app.secret_key = config['clientSecret']
mendeley = Mendeley(config['clientId'], config['clientSecret'], REDIRECT_URI)
@app.route('/')
def home():
if 'token' in session:
return redirect('/listDocuments')
auth = mendeley.start_authorization_code_flow()
session['state'] = auth.state
return render_template('home.html', login_url=(auth.get_login_url()))
@app.route('/oauth')
def auth_return():
auth = mendeley.start_authorization_code_flow(state=session['state'])
mendeley_session = auth.authenticate(request.url)
session.clear()
session['token'] = mendeley_session.token
return redirect('/listDocuments')
@app.route('/listDocuments')
def list_documents():
if 'token' not in session:
return redirect('/')
mendeley_session = get_session_from_cookies()
name = mendeley_session.profiles.me.display_name
docs = mendeley_session.documents.list(view='client').items
return render_template('library.html', name=name, docs=docs)
@app.route('/document')
def get_document():
if 'token' not in session:
return redirect('/')
mendeley_session = get_session_from_cookies()
document_id = request.args.get('document_id')
doc = mendeley_session.documents.get(document_id)
return render_template('metadata.html', doc=doc)
@app.route('/metadataLookup')
def metadata_lookup():
if 'token' not in session:
return redirect('/')
mendeley_session = get_session_from_cookies()
doi = request.args.get('doi')
doc = mendeley_session.catalog.by_identifier(doi=doi)
return render_template('metadata.html', doc=doc)
@app.route('/download')
def download():
if 'token' not in session:
return redirect('/')
mendeley_session = get_session_from_cookies()
document_id = request.args.get('document_id')
doc = mendeley_session.documents.get(document_id)
doc_file = doc.files.list().items[0]
return redirect(doc_file.download_url)
@app.route('/logout')
def logout():
session.pop('token', None)
return redirect('/')
def get_session_from_cookies():
return MendeleySession(mendeley, session['token'])
if __name__ == '__main__':
app.run()