R/wos_search.R

Defines functions wos_search

Documented in wos_search

#' Run a search query to the Web of Science Web Services Lite API
#'
#' @param sid session id generated by a previous call to \code{\link{wos_authenticate}}
#' @param query query text
#' @param api the API type. Only "lite" is supported for now
#' @param editions search collection editions to query against
#' @param url url of WoS service (to be used with a proxy)
#'
#' @return a list with two elements : the number of results, and the generated query id.
#'
#' @seealso \code{\link{wos_authenticate}}, \code{\link{wos_retrieve}}
#'
#' @export
#' @import RCurl
#' @import xml2


wos_search <- function(sid, query = "",
                       api = "lite",
                       editions = if (api == "lite") c("SCI", "ISTP", "ISSHP", "IC"),
                       url = "http://search.webofknowledge.com") {

  if (api == "lite") {

    ## Editions tags
    editions_str <- paste0("<editions><collection>WOS</collection><edition>",
                           editions,
                           "</edition></editions>",
                           collapse = "\n")

    ## SOAP request
    body <- paste0('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:woksearchlite="http://woksearchlite.v3.wokmws.thomsonreuters.com">
    <soapenv:Header/>
    <soapenv:Body>
    <woksearchlite:search>
    <queryParameters>
    <databaseId>WOS</databaseId>
    <userQuery>', query, '</userQuery>',
    editions_str,
    '<queryLanguage>en</queryLanguage>
    </queryParameters>
    <retrieveParameters>
    <firstRecord>1</firstRecord>
    <count>100</count>
    </retrieveParameters>
    </woksearchlite:search>
    </soapenv:Body>
    </soapenv:Envelope>')

    url <- paste0(url, "/esti/wokmws/ws/WokSearchLite")
  }

  if (api == "premium") {

    body <- paste0('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:woksearchlite="http://woksearch.v3.wokmws.thomsonreuters.com">
    <soapenv:Header/>
    <soapenv:Body>
    <woksearch:search>
    <queryParameters>
    <databaseId>WOS</databaseId>
    <userQuery>', query, '</userQuery>
    <editions>
    <collection>WOS</collection>
    <edition>SCI</edition>
    </editions>
    <queryLanguage>en</queryLanguage>
    </queryParameters>
    <retrieveParameters>
    <firstRecord>1</firstRecord>
    <count>100</count>
    </retrieveParameters>
    </woksearch:search>
    </soapenv:Body>
    </soapenv:Envelope>')

    url <- "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch"
  }


  headers <- c(
    Accept = "multipart/*",
    'Content-Type' = "text/xml; charset=utf-8",
    'Cookie' = paste0("SID=", sid),
    SOAPAction = ""
  )

  h <- RCurl::basicTextGatherer()
  RCurl::curlPerform(
    url = url,
    httpheader = headers,
    postfields = body,
    writefunction = h$update
  )

  resp <- xml2::read_xml(h$value())

  err <- xml2::xml_find_first(resp, xpath = ".//faultstring")
  if (length(err) > 0) {
    stop("Error : ", xml2::xml_text(err))
  }

  results <- as.numeric(xml_text(xml_find_first(resp, xpath = "//return/recordsFound")))
  query_id <- xml_text(xml_find_first(resp, xpath = "//return/queryId"))

  cat(paste0(results, " records found"))

  return(list(sid = sid, results = results, id = query_id))

}
juba/rwos documentation built on Oct. 17, 2020, 7:48 p.m.