| |
|
|
| library(tidyverse) |
| library(xml2) |
| library(lubridate) |
| library(optparse) |
| library(glue) |
|
|
| |
| option_list <- list( |
| make_option(c("-i", "--input"), type = "character", default = NULL, |
| help = "Directory containing ArXiv XML files", metavar = "character"), |
| make_option(c("-o", "--output"), type = "character", default = NULL, |
| help = "Output CSV file path", metavar = "character") |
| ) |
|
|
| |
| opt_parser <- OptionParser(option_list = option_list) |
| opt <- parse_args(opt_parser) |
|
|
| |
| if (is.null(opt$input)) { |
| stop("Error: Input directory (-i, --input) is required") |
| } |
|
|
| if (is.null(opt$output)) { |
| stop("Error: Output file (-o, --output) is required") |
| } |
|
|
| |
| xml_files <- list.files(path = opt$input, pattern = "\\.xml$", full.names = TRUE) |
|
|
| |
| extract_metadata <- function(file_path) { |
| |
| doc <- read_xml(file_path) |
|
|
| |
| file_id <- tools::file_path_sans_ext(basename(file_path)) |
|
|
| |
| ns <- c(atom = "http://www.w3.org/2005/Atom") |
|
|
| |
| entries <- xml_find_all(doc, "//atom:entry", ns) |
|
|
| |
| map_dfr(entries, function(entry) { |
| |
| authors <- xml_find_all(entry, ".//atom:author/atom:name", ns) %>% |
| map_chr(~ xml_text(.)) %>% |
| list() |
|
|
| |
| entry_id <- xml_text(xml_find_first(entry, ".//atom:id", ns)) |
| version_number <- as.numeric(str_extract(entry_id, "v(\\d+)$", group = 1)) |
|
|
| |
| updated_date <- xml_text(xml_find_first(entry, ".//atom:updated", ns)) |
| published_date <- xml_text(xml_find_first(entry, ".//atom:published", ns)) |
|
|
| |
| updated_date <- as.Date(ymd_hms(updated_date)) |
| published_date <- as.Date(ymd_hms(published_date)) |
|
|
| |
| tibble( |
| id = file_id, |
| href = glue("https://arxiv.org/abs/{file_id}"), |
| updated_at = updated_date, |
| published_at = published_date, |
| count_versions = version_number |
| ) |
| }) |
| } |
|
|
| |
| results <- map_dfr(xml_files, extract_metadata) |
|
|
| |
| print(results) |
|
|
| |
| write_csv(results, opt$output) |
|
|