-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_rss.R
132 lines (127 loc) · 4.47 KB
/
write_rss.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' Writes RSS Feeds
#'
#' @param docket_tbl incoming docket tbl
#' @param file file name to be printed.
#' @param xmlver XML file version
#' @param rssver RSS file version
#' @param title Title of RSS feed
#' @param link URL
#' @param description Description of RSS feed
#' @param language Language
#' @param copyright Copyrite
#' @param pubDate Publication Date
#' @param lastBuildDate Last updated date
#' @param docs website date
#' @param generator how the file was written
#' @param managingEditor whose fault is this?
#' @param webMaster who put this on the web?
#' @param maxitem how long is the RSS feed?
#' @param doc_ML The document ML number used to format a URL vector name.
#' @param doc_title The document title vector name.
#' @param doc_author The document author vector name.
#' @param doc_description The document description vector name.
#' @param doc_date The document date vector name.
#' @param ... Other items
#'
#' @source https://github.com/cran/animation/blob/8ef6f898875373fa95ba9a55d405a6f2bb741474/R/write.rss.R
#'
#' @return an RSS file
#' @export
write_rss <- function(
docket_tbl = "rss.csv",
file = "dockets.xml",
xmlver = "1.0",
rssver = "2.0",
title = "New Docket Files",
link = "https://mrdenman-nuclear.github.io/nrcadams/",
description = "NRC ADAMS Docket Update",
language = "en-us",
copyright = "Copyright 2022, Matthew Denman",
pubDate = Sys.time(),
lastBuildDate = Sys.time(),
docs = "https://mrdenman-nuclear.github.io/nrcadams/",
generator = "Function write_rss() in R package nrcadams",
managingEditor = "mrdenman@gmail.com",
webMaster = "mrdenman@gmail.com",
maxitem = 30,
doc_ML = `ML Number`,
doc_title = Title,
doc_author = Affiliation,
doc_description = Type,
doc_date = `Publish Date`,
...) {
docket_tbl = docket_tbl |>
dplyr::rename(
title = {{doc_title}},
description = {{doc_description}},
author = {{doc_author}},
pubDate = {{doc_date}}
) |>
nrcadams::format_ML_link({{doc_ML}}) |>
dplyr::select(title, link, description, author, pubDate) |>
dplyr::mutate(author = author |> stringr::str_replace("&", "and")) |>
dplyr::arrange(dplyr::desc(pubDate)) |>
dplyr::filter(dplyr::row_number() <= maxitem)
lcl = Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
pubDate = format(pubDate, "%a, %d %b %Y %H:%M:%S GMT")
lastBuildDate = format(lastBuildDate, "%a, %d %b %Y %H:%M:%S GMT")
cat(
"<?xml version", "=\"", xmlver, "\"?>\n", "<rss version=\"",
rssver, "\">\n", "\t", "<channel>\n", "\t\t", "<title>",
title, "</title>\n", "\t\t", "<link>", link, "</link>\n",
"\t\t", "<description>", description, "</description>\n",
"\t\t", "<language>", language, "</language>\n", "\t\t",
"<pubDate>", pubDate, "</pubDate>\n", "\t\t", "<lastBuildDate>",
lastBuildDate, "</lastBuildDate>\n", "\t\t", "<docs>",
docs, "</docs>\n", "\t\t", "<generator>", generator,
"</generator>\n", "\t\t", "<managingEditor>", managingEditor,
"</managingEditor>\n", "\t\t", "<webMaster>", webMaster,
"</webMaster>\n", file = file,
sep = ""
)
extra = list(...)
if (length(extra)) {
tag1 = paste("\t\t<", names(extra), ">", sep = "")
tag2 = paste("</", names(extra), ">", sep = "")
cat(paste(tag1, extra, tag2, sep = "", collapse = "\n"),
"\n", file = file, append = TRUE)
}
if(nrow(docket_tbl) > 0.5) {
tag1 = paste("<", colnames(docket_tbl), ">", sep = "")
tag2 = paste("</", colnames(docket_tbl), ">", sep = "")
cat(
paste(
"\t\t<item>",
apply(docket_tbl, 1, function(xx) paste(
"\t\t\t",
paste(tag1, xx, tag2, sep = "", collapse = "\n\t\t\t"), sep = "")
),
"\t\t</item>",
sep = "\n",
collapse = "\n"
),
file = file, append = TRUE
)
cat("\n\t", file = file, append = TRUE)
}
cat("</channel>", file = file, append = TRUE)
cat("\n</rss>", file = file, append = TRUE)
Sys.setlocale("LC_TIME", lcl)
cat("RSS feed created at:", file, "\n")
}
#' Make URL from ML number
#'
#' @param results_tbl tibble containing search data
#' @param ML_vector The vector name for the document ML number
#'
#' @return tibble with new link variable containing an ML URL
#' @export
format_ML_link = function(results_tbl, ML_vector = `ML Number`) {
results_tbl |>
dplyr::mutate(
link = stringr::str_c(
"https://adamswebsearch2.nrc.gov/webSearch2/main.jsp?AccessionNumber=", {{ML_vector}}
)
)
}