Skip to content

Commit a4fc6da

Browse files
committedOct 2, 2021
mendeley integration into master
2 parents 39ff757 + a0f2433 commit a4fc6da

File tree

8 files changed

+48
-50
lines changed

8 files changed

+48
-50
lines changed
 

‎app/.idea/emns.iml

-19
This file was deleted.

‎app/.idea/misc.xml

-7
This file was deleted.

‎app/.idea/modules.xml

-8
This file was deleted.

‎app/.idea/vcs.xml

-7
This file was deleted.

‎app/app/main.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
from app.config import Config
77

8+
89
app = Flask(__name__)
10+
socketio = SocketIO(app)
11+
socketio.init_app(app, cors_allowed_origins="*")
912
API_KEY = os.environ["API_KEY"]
1013
ROOT_URL = os.environ.get("ROOT_URL", "https://scholar.miage.dev")
1114
SHLINK_API_KEY = os.environ.get("SHLINK_API_KEY", "")
15+
MENDELEY_CLIENT_ID = os.environ.get("MENDELEY_CLIENT_ID", "")
16+
MENDELEY_SECRET = os.environ.get("MENDELEY_SECRET", "")
1217

1318
SCPUS_BACKEND = f'https://api.elsevier.com/content/search/scopus?start=%d&count=%d&query=%s&apiKey={API_KEY}'
14-
db = None
1519
app.config.from_object(Config())
1620
db = SQLAlchemy(app)
1721

@@ -20,8 +24,6 @@
2024
db.create_all()
2125
db.session.commit()
2226

23-
print(f"Using API_KEY={API_KEY}")
24-
2527
print("launching FLASK")
2628
# app.run(host="0.0.0.0")
2729
socketio = SocketIO(app)

‎app/app/rest.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
from app.main import app, db
1+
from app.main import app, db, MENDELEY_CLIENT_ID, MENDELEY_SECRET
22
from app.model import ScpusFeed, ScpusRequest
33
from app.business import count_results_for_query, get_results_for_query, update_feed, generate_rss
4-
from flask import abort, Response, render_template, request
5-
4+
from flask import abort, Response, render_template, request, session, redirect
5+
from mendeley import Mendeley
6+
from mendeley.session import MendeleySession
67
import pickle
78

9+
mendeley = Mendeley(MENDELEY_CLIENT_ID, MENDELEY_SECRET, redirect_uri="http://localhost:5000/oauth")
10+
811

912
@app.route("/robots.txt")
1013
def block_robots():
@@ -60,10 +63,36 @@ def get_feed(id):
6063
return Response(rss, mimetype='application/rss+xml')
6164

6265

66+
@app.route("/mendeleyLogout")
67+
def mendeleyLogout():
68+
session.clear()
69+
return redirect('/')
70+
71+
72+
@app.route('/oauth')
73+
def auth_return():
74+
auth = mendeley.start_authorization_code_flow(state=session['state'])
75+
mendeley_session = auth.authenticate(request.url)
76+
77+
session.clear()
78+
session['token'] = mendeley_session.token
79+
80+
return redirect('/')
81+
82+
83+
def get_session_from_cookies():
84+
return MendeleySession(mendeley, session['token'])
85+
86+
6387
@app.route('/')
6488
@app.route('/home')
6589
def home():
66-
return render_template('index.html')
90+
if 'token' in session:
91+
return render_template('index.html', token=session['token'])
92+
else:
93+
auth = mendeley.start_authorization_code_flow()
94+
session['state'] = auth.state
95+
return render_template('index.html', login_url=auth.get_login_url())
6796

6897

6998
@app.route('/history', methods=["GET"])

‎app/app/templates/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ <h2>Sources</h2>
274274
</div>
275275
<div style="width: 90%; margin: auto;">
276276

277+
{% if login_url %}
278+
<input type="button" class="btn btn-primary" onclick="window.location='{{ login_url }}'" value="Connect to Mendeley" />
279+
{% else %}
280+
<input type="button" class="btn btn-error" onclick="window.location='{{ "/mendeleyLogout" }}'" value="Disconnect from Mendeley" />
281+
{% endif %}
282+
277283
<form action="query" method="POST">
278284
<p>Query:</p>
279285
<p>example: TITLE-ABS-KEY(blockchain) AND TITLE-ABS-KEY(INDUSTRY 4.0) AND (TITLE-ABS-KEY(Security) OR

‎requirements.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
Flask==1.1.2
2-
requests==2.25.1
2+
requests==2.5.1
3+
requests-oauthlib==0.4.2
4+
oauthlib==0.7.2
35
Flask-SocketIO==5.0.1
46
mysqlclient==2.0.3
57
Flask-SQLAlchemy==2.5.1
68
feedgen==0.9.0
79
dateparser==1.0.0
810
pytz==2020.5
9-
shlink==0.1.0
11+
shlink==0.1.0

0 commit comments

Comments
 (0)
Please sign in to comment.