понедельник, 26 января 2015 г.

Integrate R with Pastebin

Introduction


Pastebin is a web application which allows anyone to store and share plain text snippets. It is often used for exchanging code snippets and for collaborative code review. I have used Pastebin in one my past project as intermediate online storage for textual information. In this article I would like to describe briefly how to access and use Pastebin functions from R scripts.


Pastebin API


Pastebin API is described in details here. It is accessible through HTTP POST / GET requests and exposes the following main functions:

  • Login to Pastebin and user key retrieval
  • Create a new paste
  • Listing pastes created by a user
  • Listing trending pastes
  • Deleting pastes created by a user
  • Getting user information
  • Getting paste text
In order to start using this API, you will need to register and get developer key.

Once you registered and got developer key, you will be able login to the site and get user key (which needs to be passed in other API functions). Please note that user password is passed in clear text during login. Perhaps this is OK for such kind of sites, but be careful when choosing password for your Pastebin account and don't user passwords which match to your other account.

Once you logged into the system, you will be able to post new pastes, list existing ones, remove pastes, and view user details.

Some functions are available for anonymous users. For example, listing trending pastes and getting public pastes.

R Code

Below are R functions which implements Pastebin operations described above. They use RCurl package to send POST and GET requests to Pastebin API.
 
pastebin_login = function(api_dev_key, api_user_name, api_user_password) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_login.php',
        api_dev_key = api_dev_key,
        api_user_name = api_user_name,
        api_user_password = api_user_password
    ))
 
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    return(response)
}
pastebin_paste = function(api_dev_key, api_user_key, api_paste_code, api_paste_name, api_paste_format, api_paste_private = c('0', '1', '2'), api_paste_expire_date = c('N','10M','1H','1D','1W','2W','1M')) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_post.php',
        api_option = 'paste',
        api_dev_key = api_dev_key,
        api_paste_code = api_paste_code,
        api_paste_private = api_paste_private,
        api_paste_name = api_paste_name,
        api_paste_expire_date = api_paste_expire_date,
        api_paste_format = api_paste_format,
        api_user_key = api_user_key
    ))
 
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    paste_key = gsub('http://pastebin.com/', '', response)
 
    return(paste_key)
}
 
pastebin_list = function(api_dev_key, api_user_key, api_results_limit = 50) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_post.php',
        api_option = 'list',
        api_dev_key = api_dev_key,
        api_results_limit = api_results_limit,
        api_user_key = api_user_key
    ))
     
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    return(response)
}
 
pastebin_delete = function(api_dev_key, api_user_key, api_paste_key) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_post.php',
        api_option = 'delete',
        api_dev_key = api_dev_key,
        api_paste_key = api_paste_key,
        api_user_key = api_user_key
    ))
     
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    return(response)
}
 
pastebin_trends = function(api_dev_key) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_post.php',
        api_option = 'trends',
        api_dev_key = api_dev_key
    ))
 
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    return(response)
}
 
pastebin_userdetails = function(api_dev_key, api_user_key) {
 
    require(RCurl)
 
    response = as.character(postForm(
        uri = 'http://pastebin.com/api/api_post.php',
        api_option = 'userdetails',
        api_dev_key = api_dev_key,
        api_user_key = api_user_key
    ))
 
    if (length(grep("Bad API request", response)) > 0) stop(response)
 
    return(response)
}
 
pastebin_get = function(paste_key) {
 
    require(RCurl)
 
    raw = getURL(paste('http://pastebin.com/raw.php?i=', paste_key, sep = ""))
 
    return(raw)
}  
 

Example

And finally below is the simple example to illustrate how to use the functions in practice.

Please note that you will have to specify your own developer key, user name, and password instead of placeholders.

Please also be careful when sending request to web app programmatically and try to avoid overloading it.
 
api_dev_key = '<dev key>'
 
api_user_key = pastebin_login(api_dev_key, '<user name>', '<password>')
 
paste_key = pastebin_paste(api_dev_key, api_user_key, 'test text', 'testname', 'text', '1', '10M')
 
pastebin_get(paste_key)
 
pastebin_list(api_dev_key, api_user_key)
 
pastebin_delete(api_dev_key, api_user_key, paste_key)

Комментариев нет:

Отправить комментарий