Mendeley
Mendeleyという論文管理ツールがあるのですが、各論文に対していろいろな情報がデータベースとして保存されています。
APIも提供されているので、Rを使って論文に対する情報を抽出できるかやってみたいと思います。
Mendeley APIのアクセストークン用の情報を手に入れる。
Mendeleyの開発者ポータルにログイン
http://dev.mendeley.com/ から開発者ポータルにログインしてください。
MendeleyからアプリケーションIDとSecretを手に入れる
アプリケーションの管理画面に入れるので、適当にアプリケーション名を入れてアプリケーションIDとSecretを生成してください。
コード
アクセストークン生成用の情報と論文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"))