File: news.R

package info (click to toggle)
r-cran-cli 3.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: ansic: 16,412; cpp: 37; sh: 13; makefile: 2
file content (122 lines) | stat: -rwxr-xr-x 3,012 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env Rscript

setup_app <- function() {
  theme <- list(
    "url" = list(color = "blue"),
    ".pkg" = list(color = "orange"),
    "it" = list("margin-bottom" = 1))
  start_app(theme = theme, output = "stdout")
}

load_packages <- function() {
  tryCatch({
    library(cli)
    library(httr)
    library(jsonlite)
    library(prettyunits)
    library(glue)
    library(parsedate)
    library(docopt) },
    error = function(e) {
      cli_alert_danger(
            "The {.pkg glue}, {.pkg httr}, {.pkg jsonlite}, {.pkg prettyunits},",
            " {.pkg parsedate} and {.pkg docopt} packages are needed!")
      q(save = "no", status = 1)
    })
}

news <- function(n = 10, day = FALSE, week = FALSE, since = NULL,
                 reverse = FALSE) {

  load_packages()
  setup_app()
  
  result <- if (day)
    news_day()
  else if (week)
    news_week()
  else if (!is.null(since))
    news_since(since)
  else
    news_n(as.numeric(n))

  if (reverse) result <- rev(result)

  format_results(result)
  invisible()
}

news_day <- function() {
  date <- format_iso_8601(Sys.time() - as.difftime(1, units="days"))
  ep <- glue("/-/pkgreleases?descending=true&endkey=%22{date}%22")
  do_query(ep)
}

news_week <- function() {
  date <- format_iso_8601(Sys.time() - as.difftime(7, units="days"))
  ep <- glue("/-/pkgreleases?descending=true&endkey=%22{date}%22")
  do_query(ep)
}

news_since <- function(since) {
  date <- format_iso_8601(parse_date(since))
  ep <- glue("/-/pkgreleases?descending=true&endkey=%22{date}%22")
  do_query(ep)
}

news_n <- function(n) {
  ep <- glue("/-/pkgreleases?limit={n}&descending=true")
  do_query(ep)
}

do_query <- function(ep) {
  base <- "https://crandb.r-pkg.org"
  url <- glue("{base}{ep}")
  response <- GET(url)
  stop_for_status(response)
  fromJSON(content(response, as = "text"), simplifyVector = FALSE)
}

format_results <- function(results) {
  cli_div(theme = list(ul = list("list-style-type" = "")))
  cli_ol()
  lapply(results, format_result)
}

parse_arguments <- function() {

  "Usage:
  news.R [-r | --reverse] [-n num ]
  news.R [-r | --reverse] --day | --week | --since date
  news.R [-h | --help]

Options:
  -n num        Show the last 'n' releases [default: 10].
  --day         Show releases in the last 24 hours
  --week        Show relaases in the last 7 * 24 hours
  --since date  Show releases since 'date'
  -r --reverse  Reverse the order, show older on top
  -h --help     Print this help message

New package releases on CRAN
" -> doc

  docopt(doc)
}

format_result <- function(result) {
  pkg <- result$package
  ago <- vague_dt(Sys.time() - parse_iso_8601(result$date))
  url <- paste0("https://r-pkg.org/pkg/", pkg$Package)
  cli_li()
  cli_text("{.pkg {pkg$Package}} {pkg$Version} --
           {ago} by {.emph {pkg$Maintainer}}")
  cli_text("{pkg$Title}")
  cli_text("{.url {url}}")
}

if (is.null(sys.calls())) {
  load_packages()
  opts <- parse_arguments()
  news(opts$n, opts$day, opts$week, opts$since, opts$reverse)
}