LoginSignup
3
1

More than 5 years have passed since last update.

RでMendeleyのAPIを叩いてみる~ある論文の読者層を見る~

Last updated at Posted at 2017-09-13

Mendeley

Mendeleyという論文管理ツールがあるのですが、各論文に対していろいろな情報がデータベースとして保存されています。
APIも提供されているので、Rを使って論文に対する情報を抽出できるかやってみたいと思います。

Mendeley APIのアクセストークン用の情報を手に入れる。

Mendeleyの開発者ポータルにログイン

http://dev.mendeley.com/ から開発者ポータルにログインしてください。

image.png

MendeleyからアプリケーションIDとSecretを手に入れる

アプリケーションの管理画面に入れるので、適当にアプリケーション名を入れてアプリケーションIDとSecretを生成してください。
image.png

コード

アクセストークン生成用の情報と論文Doi

client_id <- "アプリケーションID"
client_secret <- "アプリケーションSecret"
doi <- "10.1021/acs.accounts.6b00479"

パッケージ

require(httr)
require(rjson)
require(RCurl)
require(RColorBrewer)
require(optparse)

アクセストークン生成

先ほどの情報からアクセストークンを生成をします。
調べたところ以下の流れはMendeleyの認証のうち、Client credentials authorization flowに当たるようです。
このトークンでは、ユーザーごとの情報は抜き出せず、アノニマスな情報だけを抜き出せる権限になるようです。
ユーザー毎の情報を抜き出す認証方法(Authorization code flow)についてはまた今度出来たらと思います。良さげなサイトなどあれば教えてください。
書きました→RでMendeleyからOAuth認証後、自分のライブラリ情報を抜き出す

basic_auth_credentials <- RCurl::base64(paste(client_id, client_secret, sep = ":"))
access_token_rsp <- POST('https://api.mendeley.com/oauth/token',
                        add_headers(
                          'Authorization' = paste("Basic", basic_auth_credentials),
                          'Content-Type' = 'application/x-www-form-urlencoded'
                        ),
                        body = 'grant_type=client_credentials&scope=all'
)

access_token <- content(access_token_rsp)$access_token


Mendeley APIの作成

catalogue_url <- paste('https://api.mendeley.com/catalog?view=stats&doi=', curlEscape(doi), sep='')

doc_rsp <- GET(catalogue_url,
               add_headers(
                 'Authorization' = paste("Bearer", access_token),
                 'Content-Type' = 'application/vnd.mendeley-document.1+json'
               )
)

docs <- fromJSON(rawToChar(content(doc_rsp)))

返ってきたデータから必要な情報を抽出、プロット

df <- data.frame(t(data.frame(docs[[1]]$reader_count_by_academic_status)))
colnames(df) <- c('readers')
par(mar=c(5,18,1,1), las=2)
barplot(df$readers, names.arg=row.names(df), horiz=TRUE, col=brewer.pal(n=15, name="Reds"))

image.png

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1