Skip to content

Commit

Permalink
Merge pull request #6 from chrbknudsen/main
Browse files Browse the repository at this point in the history
gammelt materiale bevaret
  • Loading branch information
chrbknudsen authored Sep 10, 2024
2 parents 711f43d + 73861d1 commit b11657e
Show file tree
Hide file tree
Showing 29 changed files with 1,547 additions and 0 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^skrammel$
19 changes: 19 additions & 0 deletions skrammel/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Package: KBAPI
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports:
dplyr,
magrittr,
rvest
674 changes: 674 additions & 0 deletions skrammel/LICENSE

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions skrammel/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(get_authors)
export(get_categories)
export(get_collections)
export(get_editions)
export(get_genres)
export(get_subcollections)
importFrom(magrittr,"%>%")
104 changes: 104 additions & 0 deletions skrammel/R/get_authors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#' Get authors
#'
#' Retrieves a list of available authors in Text Collections
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_authors()
#'
#' @export

get_authors <- function(){
test <- jsonlite::read_json("https://api.kb.dk/data/rest/api/text?q=cat_ssi:author+AND+type_ssi:work&wt=json&start=0&rows=75&defType=edismax",
simplifyVector = T)
test$response$docs$inverted_name_title_ssi
}

# det er ikke givet at det er den format der skal bruges til at søge.
# Så her er nogle alternativer
# test$response$docs[[2]]$work_title_tesim
# test$response$docs[[2]]$sort_title_ssi
# test$response$docs[[2]]$inverted_name_title_ssi



# Nedenstående skal placeres i et internt datasæt, og hentes som alternativ
# til at trække data fra apien. (for det tager tid!)
fall_back <- c("Palladius, Peder" ,
"Paludan-Müller, Frederik",
"Ploug, Carl",
"Møller, Poul Martin",
"Richardt, Christian",
"Saxo",
"Schack, Hans Egede",
"Schandorph, Sophus",
"Sibbern, Frederik Christian",
"Skjoldborg, Johan",
"Skram, Erik",
"Michaëlis, Sophus",
"Staffeldt, Schack",
"Steffens, Henrich",
"Sthen, Hans Christensen",
"Stub, Ambrosius",
"Stuckenberg, Viggo",
"Thomissøn, Hans",
"Topsøe, Vilhelm",
"Wessel, Johan Herman",
"Wied, Gustav",
"Winther, Christian",
"Worm, Jacob",
"Aakjær, Jeppe",
"Aarestrup, Emil",
"Ravnkilde, Adda",
"Arrebo, Anders",
"Bagger, Carl",
"Baggesen, Jens",
"Bang, Herman",
"Bergsøe, Vilhelm",
"Blicher, Steen Steensen",
"Bødtcher, Ludvig",
"Bording, Anders",
"Brahe, Tycho",
"Brandes, Georg",
"Brorson, Hans Adolph",
"Chievitz, Poul",
"Claussen, Sophus",
"Dalgas, Ernesto",
"Drachmann, Holger",
"Brandes, Edvard",
"Ewald, Johannes",
"Falster, Christian",
"Fibiger, Mathilde",
"Heiberg, Johanne Luise",
"Gjellerup, Karl",
"Goldschmidt, M. A.",
"Grundtvig, N. F. S.",
"Gyllembourg, Thomasine",
"Hauch, Carsten",
"Andersen, Hans Christian",
"Helie, Paulus",
"Hertz, Henrik",
"Hjortø, Knud",
"Holberg, Ludvig",
"Hostrup, Jens Christian",
"Rode, Helge",
"Ingemann, B. S.",
"Knudsen, Jakob",
"Jacobsen, Jørgen-Frantz",
"Heiberg, Johan Ludvig",
"Jacobsen, J. P.",
"Juel-Hansen, Erna",
"Kidde, Harald Henrik Sager",
"Kierkegaard, Søren",
"Kingo, Thomas",
"Kaalund, Hans Vilhelm",
"Larsen, Thøger",
"Larsen, Karl",
"Leonora Christina",
"Lyschander, Claus Christoffersen",
"Nansen, Peter",
"Oehlenschläger, Adam",
"Heiberg, Peter Andreas")
30 changes: 30 additions & 0 deletions skrammel/R/get_categories.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Get categories
#'
#' FIXME
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_categories()
#'
#' @export

get_categories <- function(){
base_url <- "https://api.kb.dk/data/text"

test <- rvest::read_html(base_url)

res <- test |>
rvest::html_element("#cat_ssi") %>%
rvest::html_elements("option") %>%
rvest::html_attrs() %>%
unlist()
unname(res[nchar(res)>0])
}

# den kan nok også med fordel have en "non-live" option
# og så kommer den ud som en named character vector.
# det skal der nok gøres noget ved.

32 changes: 32 additions & 0 deletions skrammel/R/get_collections.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Get collections
#'
#' FIXME
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_collections()
#'
#' @export

get_collections <- function(){
navn <- NULL
base_url <- "https://api.kb.dk/data/"

rvest::read_html(base_url) %>%
rvest::html_elements("body .main-nav a") %>%
purrr::map_dfr(~data.frame(
"navn" = rvest::html_attr(., "href"),
"beskrivelse" = rvest::html_text(.)
)) %>%
dplyr::mutate(link = paste0(base_url, navn))
}
# Hvis ovenstående fejler - returner denne:
# navn beskrivelse link
# 1 dsfl Aerial photographs https://api.kb.dk/data/dsfl
# 2 cop Digital collections https://api.kb.dk/data/cop
# 3 text Text collections https://api.kb.dk/data/text


18 changes: 18 additions & 0 deletions skrammel/R/get_cop.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
hent_have_data <- function(){
url <- "http://www5.kb.dk/images/billed/2010/okt/billeder/subject37894/da/?page="
data <- httr::GET(paste0(url,1), add_headers(Accept = "application/json")) %>%
content(type = "text", encoding = "UTF-8") %>%
fromJSON()
data <- data$response
sider <- data$pages$total_pages
res <- list()
res[[1]] <- data$docs %>% as.data.frame()
for(i in 2:sider){
res[[i]] <- httr::GET(paste0(url,i), add_headers(Accept = "application/json")) %>%
content(type = "text", encoding = "UTF-8") %>% fromJSON() %>%
.$response %>%
.$docs %>%
as.data.frame()
}
bind_rows(res)
}
19 changes: 19 additions & 0 deletions skrammel/R/get_editions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Get editions
#'
#' FIXME
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_editions()
#'
#' @export


get_editions <- function(){
jsonlite::read_json("https://api.kb.dk/data/rest/api/editions",
simplifyVector = T)
}

27 changes: 27 additions & 0 deletions skrammel/R/get_genres.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Get genres
#'
#' FIXME
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_genres()
#'
#' @export



get_genres <- function(){
base_url <- "https://api.kb.dk/data/text"

test <- rvest::read_html(base_url)

res <- test |>
rvest::html_element("#genre_ssi") |>
rvest::html_elements("option") |>
rvest::html_attrs() |>
unlist()
unname(res[nchar(res)>0])
}
24 changes: 24 additions & 0 deletions skrammel/R/get_subcollections.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Get subcollections
#'
#' FIXME
#'
#' @param live logical value indicating if list of authors should be retrieved live, or from internal data.
#'
#' @return None
#'
#' @examples
#' get_subcollections()
#'
#' @export

get_subcollections <- function(){
test <- jsonlite::read_json("https://api.kb.dk/data/rest/api/text?q=&rows=0&facet=on&facetfield=subcollection_ssi",
simplifyVector = F)
test <- test$facet_counts$facet_fields$subcollection_ssi
test <- test[seq(1, length(test), 2)]
unlist(test)


# Med mindre ovenstående fejler - ellers returner:
# c("adl", "gv", "tfs", "sks")
}
36 changes: 36 additions & 0 deletions skrammel/R/luftfotos.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Installer httr2, hvis det ikke allerede er installeret
# install.packages("httr2")

# Load httr2 pakken
library(httr2)

# Definer API-endpointet
api_url <- "http://www.kb.dk/cop/syndication/images/luftfo/2011/maj/luftfoto/subject203/"

# Opret en query string med parametrene
query_params <- list(
bbo = "10.395490670074423,55.22227193719089,10.296785378326376,55.18994697228769",
zoom = 14,
lat = 55.20611273543719,
lng = 10.346138024200382,
page = 1,
q_fritekst = "",
q_stednavn = "",
q_bygningsnavn = "",
q_person = "",
q_adresse = "",
notBefore = 1920,
notAfter = 1970,
category = "subject203",
itemType = "all",
thumbnailSize = "",
format = "xml"
)

# Send GET-anmodningen med parametre som query string
response <- request(api_url) %>%
req_url_query(!!!query_params) %>%
req_perform()

response

14 changes: 14 additions & 0 deletions skrammel/R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
4 changes: 4 additions & 0 deletions skrammel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# KB-API
Code to pull data from the Royal Library API


21 changes: 21 additions & 0 deletions skrammel/man/get_authors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions skrammel/man/get_categories.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b11657e

Please sign in to comment.