Skip to content

Instantly share code, notes, and snippets.

@baptiste
Last active January 4, 2023 04:08
Show Gist options
  • Save baptiste/fd2836c0a8c3a2a62a6cc60c5759c51f to your computer and use it in GitHub Desktop.
Save baptiste/fd2836c0a8c3a2a62a6cc60c5759c51f to your computer and use it in GitHub Desktop.
library(minixml)
# devtools::install_github("coolbutuseless/minixml")
library(anytime)
generate_empty_feed <- function(file = 'index.xml'){
doc <- xml_elem("rss", version="2.0")
channel <- doc$add('channel')
channel$add('title', "Photography")
channel$add('link', "https://photo.bapt.xyz/")
channel$add('description', "My latest photo pages")
cat(as.character(doc), "\n", file= file)
}
import_feed <- function(file = 'index.xml'){
rl <- readLines('index.xml')
minixml::parse_xml_doc(paste(trimws(rl), collapse = '',sep=''))
}
new_entry <- function(feed,
album = 'kaikoura',
description = paste("photo album of", album),
title = paste("New album:", album),
link = paste0("https://p.bapt.xyz/", album),
guid = paste0("p.bapt.xyz/", album),
pubDate = rfc2822( anytime(Sys.time()) )){
channel <- feed$children[[1]]
item <- xml_elem('item')
item$add("title", title)
item$add('link', link)
item$add('guid', guid)
item$add('pubDate', pubDate)
item$add('description', description)
channel$append(item)
return(feed)
}
update_feed <- function(feed, file = 'index.xml'){
cat(as.character(feed),"\n", file= file)
}
# generate_empty_feed() # only first time
feed <- import_feed()
new_feed <- new_entry(feed, album = 'kaikoura')
new_feed
update_feed(new_feed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment