Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fs search is fixed, closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Mar 21, 2014
1 parent b249b61 commit 3724b0a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 33 deletions.
87 changes: 58 additions & 29 deletions R/fs_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,42 @@
#' @param category Show only results matching this category
#' @param from_date Start time window for search. Date format is YYYY-MM-DD
#' @param to_date Ending time window for search. Date format is YYYY-MM-DD
#' @param mine Browse only articles owned by user. default is FALSE
#' @param mine Browse only articles owned by user. default is FALSE. Not functional. Use \code{\link{fs_browse}} instead.
#' @param public_only (for use with mine=TRUE only) browse only my public articles. default is FALSE
#' @param private_only (for use with mine=TRUE only) browse only my private articles. default is FALSE
#' @param drafts_only (for use with mine=TRUE only) browse only my draft articles. default is FALSE
#' @param session (optional) the authentication credentials from \code{\link{fs_auth}}. If not provided, will attempt to load from cache as long as figshare_auth has been run.
#' @param base the API access url
#' @param debug logical, enable debugging mode
#' @return output of PUT request (invisibly)
#' @seealso \code{\link{fs_auth}}
#' @seealso \\code{\link{fs_auth}} \code{\link{fs_browse}}
#' @references \url{http://api.figshare.com/docs/howto.html#q-search}
#' @import httr
#' @export
#' @examples \dontrun{
#' fs_search("Boettiger")
#' fs_search("Boettiger", category = "Ecology")
#' fs_search("Boettiger", author = "Carl")
#' fs_search("Boettiger", author = "Carl", from="2014-01-01")
#' fs_search("Boettiger", author = "Carl", from="2014-01-01",
#' category = "Evolutionary Biology")
#'
#' }
fs_search <-
function(query, author = NA, title = NA, description = NA, tag = NA, category = NA, from_date = NA, to_date = NA, mine = FALSE, public_only = FALSE, private_only = FALSE, drafts_only = FALSE, session = fs_get_auth(), base = "http://api.figshare.com/v1") {
function(query,
author = NA,
title = NA,
description = NA,
tag = NA,
category = NA,
from_date = NA,
to_date = NA,
mine = FALSE,
public_only = FALSE,
private_only = FALSE,
drafts_only = FALSE,
session = fs_get_auth(),
base = "http://api.figshare.com/v1",
debug = FALSE) {

# resolve name conflict?
the_title <- title
Expand All @@ -45,52 +65,61 @@ fs_search <-
method <- paste(method, "/search?search_for=", query, sep="")


if(!is.na(author))
if(!is.na(author)){ # NOT AN ID
method <- paste(method, "&has_author=", author, sep="")
}
if(!is.na(the_title))
method <- paste(method, "&has_title=", the_title, sep="")
if(!is.na(description))
method <- paste(method, "&has_description=", description, sep="")
if(!is.na(tag))
method <- paste(method, "&has_tag=", tag, sep="")
if(!is.na(category)){
if(!is.numeric(category))
category <- fs_cat_to_id(category)
# if(!is.numeric(category))
# category <- fs_cat_to_id(category)
method <- paste(method, "&has_category=", category, sep="")
}
if(!is.na(from_date))
method <- paste(method, "&from_date=", from_date, sep="")
if(!is.na(to_date))
method <- paste(method, "&to_date=", to_date, sep="")

request = paste(base, method, sep="/")
request <- paste(base, method, sep="/")
request <- build_url(parse_url(request)) # perform % encoding

out <- GET(request, config(token = session))
if(debug | out$status_code != 200)
out

else {

parsed <- fromJSON(content(out, "text"))
if(is.null(parsed$count))
parsed$count <- parsed$items_found
if(is.null(parsed$count))
parsed$count <- length(parsed$items)
out <- parsed$items
parsed <- fromJSON(content(out, "text"))
if(is.null(parsed$count))
parsed$count <- parsed$items_found
if(is.null(parsed$count))
parsed$count <- length(parsed$items)
out <- parsed$items



if(parsed$count > 10) {
total_pages <- ceiling(parsed$count / 10)
all <- lapply(1:total_pages, function(i){
method_ <- paste(method, "&page=", i, sep="")
request = paste(base, method_, sep="/")
out <- GET(request, config(token = session))
parsed <- content(out, as = "parsed")
parsed$items
})
out <- unlist(all, recursive = FALSE)
if(parsed$count > 10) {
total_pages <- ceiling(parsed$count / 10)
all <- lapply(1:total_pages, function(i){
method_ <- paste(method, "&page=", i, sep="")
request = paste(base, method_, sep="/")
request <- build_url(parse_url(request)) # perform % encoding
out <- GET(request, config(token = session))
parsed <- fromJSON(content(out, as = "text"))
parsed$items
})
out <- unlist(all, recursive = FALSE)
}
out <- lapply(out, function(o){
class(o) <- "fs_object"
o
})
out
}
out <- lapply(out, function(o){
class(o) <- "fs_object"
o
})
out
}

## response <- GET("http://api.figshare.com/v1/articles/search?search_for=Boettiger", config(token = fs_auth()))
Expand Down
16 changes: 12 additions & 4 deletions man/fs_search.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
fs_search(query, author = NA, title = NA, description = NA, tag = NA,
category = NA, from_date = NA, to_date = NA, mine = FALSE,
public_only = FALSE, private_only = FALSE, drafts_only = FALSE,
session = fs_get_auth(), base = "http://api.figshare.com/v1")
session = fs_get_auth(), base = "http://api.figshare.com/v1",
debug = FALSE)
}
\arguments{
\item{query}{the search query}
Expand All @@ -29,7 +30,8 @@ fs_search(query, author = NA, title = NA, description = NA, tag = NA,
is YYYY-MM-DD}

\item{mine}{Browse only articles owned by user. default
is FALSE}
is FALSE. Not functional. Use \code{\link{fs_browse}}
instead.}

\item{public_only}{(for use with mine=TRUE only) browse
only my public articles. default is FALSE}
Expand All @@ -46,6 +48,8 @@ fs_search(query, author = NA, title = NA, description = NA, tag = NA,
run.}

\item{base}{the API access url}

\item{debug}{logical, enable debugging mode}
}
\value{
output of PUT request (invisibly)
Expand All @@ -59,7 +63,11 @@ Full-text searches coming soon.
\examples{
\dontrun{
fs_search("Boettiger")
fs_search("Boettiger", category = "Ecology")
fs_search("Boettiger", author = "Carl")
fs_search("Boettiger", author = "Carl", from="2014-01-01")
fs_search("Boettiger", author = "Carl", from="2014-01-01",
category = "Evolutionary Biology")

}
}
\author{
Expand All @@ -69,6 +77,6 @@ Carl Boettiger \email{cboettig@gmail.com}
\url{http://api.figshare.com/docs/howto.html#q-search}
}
\seealso{
\code{\link{fs_auth}}
\\code{\link{fs_auth}} \code{\link{fs_browse}}
}

0 comments on commit 3724b0a

Please sign in to comment.