Skip to content

Instantly share code, notes, and snippets.

@japborst
Created February 10, 2017 09:29
Show Gist options
  • Save japborst/e510d2ae7f00d09708b80f6f5430de68 to your computer and use it in GitHub Desktop.
Save japborst/e510d2ae7f00d09708b80f6f5430de68 to your computer and use it in GitHub Desktop.
Python function that downloads all Mendeley entries as bibtex
import requests
from mendeley import Mendeley
def get_mendeley_bibtex(login, api_id, api_secret):
# login: tuple containing username and password
# api_id: Mendeley API id
# api_secret: Mendeley API secret
# Temporarily disable InsecurePlatformWarnings from urllib3
requests.packages.urllib3.disable_warnings()
# Set-up a Mendeley client
print "Logging in to Mendeley"
mendeley = Mendeley(str(api_id), client_secret=api_secret, redirect_uri="http://mendeley.com")
# Authorize user for Mendeley
auth = mendeley.start_authorization_code_flow()
login_url = auth.get_login_url()
r = requests.post(login_url, data={"username": login[0], "password": login[1]}, timeout=10)
auth_response = r.history[2].url
session = auth.authenticate(auth_response)
# Retrieve Mendeley documents in bibtex format
print "Downloading bib file"
url = "https://api.mendeley.com/documents"
# Max limit is 500
params = {
"view": "bib",
"limit": 500
}
access_token = session.token['access_token']
headers = {
"Authorization": "Bearer %s" % access_token,
"Accept": "application/x-bibtex"
}
r = requests.get(url, headers=headers, params=params, timeout=10)
if r.status_code != 200:
raise IOError("Shit, something broke downloading the bibfile")
return r.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment