Skip to content

Instantly share code, notes, and snippets.

@GarrettMooney
Last active February 26, 2018 03:11
Show Gist options
  • Save GarrettMooney/85a0ca845a3fdee441f7b990ba77e40b to your computer and use it in GitHub Desktop.
Save GarrettMooney/85a0ca845a3fdee441f7b990ba77e40b to your computer and use it in GitHub Desktop.
script to get electricity data from pjm.com
library(purrr)
data_dir <- here::here()
base_url <- "http://www.pjm.com"
url <- sprintf("%s/pub/operations/hist-meter-load", base_url)
years <- 1993L:2018L
# check for robots.txt
robotstxt::get_robotstxt(base_url)
.get_data <- function(year) {
data_url <- sprintf("%s/%s-hourly-loads.xls", url, year)
file_name <- basename(data_url)
file_path <- file.path(data_dir, file_name)
if (!file.exists(file_path)) {
download.file(data_url, file_path)
Sys.sleep(sample(5, 1)) # be nice
}
}
get_data <- memoise::memoise(.get_data) # be nice and DRY
# test get_data on 1993
get_data(1993)
# get all years
walk(years, get_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment