Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 1 | setGeneric("textMetadata", function(kco, ...) standardGeneric("textMetadata") ) |
| 2 | |
| 3 | #' Retrieve metadata for a text, identified by its sigle (id) |
| 4 | #' |
| 5 | #' @aliases textMetadata |
| 6 | #' |
| 7 | #' @description |
| 8 | #' Retrieves metadata for a text, identified by its sigle (id) using the corresponding KorAP API |
| 9 | #' (see [Kustvakt Wiki](https://github.com/KorAP/Kustvakt/wiki/Service:-Metadata-Retrieval)). |
| 10 | #' |
| 11 | #' |
| 12 | #' @param kco [KorAPConnection()] object (obtained e.g. from `new("KorAPConnection")`) |
| 13 | #' @param textSigle unique text id (concatenation of corpus, document and text ids, separated by `/`, e.g. ) or vector thereof |
| 14 | #' @param verbose logical. If `TRUE`, additional diagnostics are printed. Defaults to `kco@verbose`. |
| 15 | #' |
Marc Kupietz | 3687a8c | 2024-01-24 16:18:36 +0100 | [diff] [blame] | 16 | #' @return Tibble with columns for each metadata property. In case of errors, such as non-existing texts/sigles, the tibble will also contain a column called `errors`. |
| 17 | #' If there are metadata columns you cannot make sense of, please ignore them. The function simply returns all the metadata it gets from the server. |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 18 | #' |
| 19 | #' @importFrom urltools url_encode |
| 20 | #' @importFrom dplyr bind_rows relocate mutate |
| 21 | #' |
| 22 | #' @examples |
| 23 | #' \dontrun{ |
| 24 | #' new("KorAPConnection") %>% textMetadata(c("WUD17/A97/08542", "WUD17/B96/57558", "WUD17/A97/08541")) |
| 25 | #' } |
| 26 | #' |
| 27 | #' @export |
| 28 | setMethod("textMetadata", "KorAPConnection", |
| 29 | function(kco, textSigle, verbose = kco@verbose) { |
| 30 | if (length(textSigle) > 1) |
| 31 | do.call(bind_rows, Map(function(atomicSigle) |
| 32 | textMetadata(kco, atomicSigle), textSigle)) |
| 33 | else { |
| 34 | url <- |
| 35 | paste0(kco@apiUrl, 'corpus/', |
| 36 | URLencode(enc2utf8(textSigle), reserved = TRUE)) |
| 37 | log_info(verbose, "Getting metadata for ", textSigle, sep = "") |
| 38 | res <- apiCall(kco, url) |
| 39 | log_info(verbose, ifelse(is.null(res) || "errors" %in% names(res), " [error]\n", "\n")) |
| 40 | |
| 41 | if(is.null(res)) { |
| 42 | res <- tibble(errors="API request failed") |
| 43 | } else { |
| 44 | res <- lapply(res, function(x) paste0(x, collapse = "\\t")) # flatten list |
| 45 | res <- as_tibble(res) %>% |
| 46 | head(n=1) %>% |
| 47 | mutate( |
| 48 | requestUrl = url, |
| 49 | textSigle = textSigle, |
| 50 | webUIRequestUrl = paste0(kco@KorAPUrl, sprintf('?q=<base/s=t>&cq=textSigle+%%3D+"%s"', url_encode(enc2utf8(textSigle))))) %>% |
| 51 | relocate(textSigle) |
| 52 | } |
| 53 | res |
| 54 | } |
| 55 | }) |
| 56 | |
| 57 | |