Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created May 20, 2022 01:38
Show Gist options
  • Save thoughtfulbloke/301f5bb3a18d3344ec4d38fd255ebd72 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/301f5bb3a18d3344ec4d38fd255ebd72 to your computer and use it in GitHub Desktop.
# source this file so it pauses for input,
# check the graphs
# add comments via the readline input
# post using rtweet
library(rvest)
library(dplyr)
library(ggplot2)
library(lubridate)
library(rtweet)
# Run after news, demographics, and cases updated.
######### Data updates
datename <- format(Sys.Date(), "%y_%m_%d")
urlloc <- "https://www.health.govt.nz/covid-19-novel-coronavirus/covid-19-data-and-statistics/covid-19-case-demographics"
download.file(url=urlloc,
destfile = paste0("../case_demografics_html_asof/",
datename, ".html"))
#### vaccine
if(wday(Sys.Date(), label = TRUE) %in% c("Mon", "Tue", "Wed", "Thu", "Fri")) {
datename <- format(Sys.Date(), "%y_%m_%d")
urlloc <-
"https://www.health.govt.nz/covid-19-novel-coronavirus/covid-19-data-and-statistics/covid-19-vaccine-data"
download.file(url = urlloc,
destfile = paste0("../vaccine_html_asof/",
datename, ".html"))
}
datename <- format(Sys.Date(), "_%Y-%m-%d.csv")
urlloc <- "https://github.com/minhealthnz/nz-covid-data/raw/main/cases/covid-cases.csv"
download.file(url=urlloc,
destfile = paste0("../daily_all_cases/covid-cases",
datename))
lnkloc <- paste0("https://www.health.govt.nz/news-media/news-items")
lnks <- lnkloc %>%
read_html() %>%
html_nodes("a") %>%
html_attr("href") %>%
grep(pattern="news-items.*[0123456789].*[0123456789].*[0123456789].*[0123456789].*", value=TRUE)
urlloc <- paste0("https://www.health.govt.nz", lnks[1])
datename <- format(Sys.Date(), "%y_%m_%d")
download.file(url=urlloc,
destfile = paste0("../news_item_html/",
datename, ".html"))
###########
source("daily_precentage_DHB_cases.R")
ggsave(filename="~/Desktop/graph_1.png",plot=graf,dpi=72,
units="in", bg="white", height = 5.556 * 1.6,
width=9.877* 1.6)
rm(list=ls())
source("daily_recentcast.R")
ggsave(filename="~/Desktop/graph_2.png",plot=graf,dpi=72,
units="in", bg="white", height = 5.556 * 1.6,
width=9.877* 1.6)
rm(list=ls())
source("daily_DHB_hospitalisation.R")
ggsave(filename="~/Desktop/graph_3.png",plot=graf,dpi=72,
units="in", bg="white", height = 5.556 * 1.6,
width=9.877* 1.6)
rm(list=ls())
# Mon International comparison
# Tue Elderly
# Wed Tertiary
# Thur Hospital Detail
# Fri Excess mortality
# Sat School aged
# Sun DHB cumulative
if(wday(Sys.Date(), label = TRUE) == "Mon"){
source("weekly_mon_int_comparison.R")
custom_text = "international comparisons. "
}
if(wday(Sys.Date(), label = TRUE) == "Tue"){
source("weekly_elderly.R")
custom_text = "elderly case status. "
}
if(wday(Sys.Date(), label = TRUE) == "Wed"){
source("weekly_tertiary.R")
custom_text = "tertiary age range case status. "
}
if(wday(Sys.Date(), label = TRUE) == "Thu"){
source("weekly_hospital_age.R")
custom_text = "case hospitalisation rate by age. "
}
if(wday(Sys.Date(), label = TRUE) == "Fri"){
source("weekly_NZexcess.R")
custom_text = "excess deaths YTD. "
}
if(wday(Sys.Date(), label = TRUE) == "Sat"){
source("weekly_schoolies.R")
custom_text = "school aged households. "
}
if(wday(Sys.Date(), label = TRUE) == "Sun"){
source("weekly_cumulative_DHB.R")
custom_text = "total DHB cases. "
}
ggsave(filename="~/Desktop/graph_4.png",plot=graf,dpi=72,
units="in", bg="white", height = 5.556 * 1.6,
width=9.877* 1.6)
# giving each tweet text based on date to make it easy to find
# the previous days tweet to reply to
today_text <- trimws(format.Date(Sys.Date(), "NZ Covid trends %e %b %Y. %A's graph 4 is "))
yesterday_text <- trimws(format.Date(Sys.Date() -days(1), "NZ Covid trends %e %b %Y. %A's graph 4 is"))
### pause to review graphs and add any comments to tweet
# via terminal input
addendum <- readline(prompt = "Thoughts: ")
tweetable_text <- paste0(today_text, " ",custom_text, addendum)
if(nchar(tweetable_text) > 254)(stop())
# find the last tweet in the thread to add today's as a reply
# on the basis of the date information at the start and author
previous_tweets <- search_tweets(paste0('from:thoughtfulnz "',yesterday_text,'"'))
thread_end <- previous_tweets$status_id[1]
post_tweet(status = tweetable_text,
media = c("~/Desktop/graph_1.png",
"~/Desktop/graph_2.png",
"~/Desktop/graph_3.png",
"~/Desktop/graph_4.png"),
in_reply_to_status_id = thread_end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment