Last active
August 28, 2016 03:06
-
-
Save harveyl888/9722b39233089614a8f7 to your computer and use it in GitHub Desktop.
Monitor fantasy premierleague player list for updates during transfer window. Send notification via pushbullet when a new player is added.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## transferWatch | |
## | |
## Monitor fantasy premierleague player list for updates during transfer window. | |
## Send a pushbullet notification when the player list has changed. | |
## Run as a cron job to send updates at regular intervals | |
## | |
## crontab: | |
## 0 * * * * Rscript $HOME/codes/transferWatch/transferWatch.R | |
## run every hour | |
## | |
#library(XML) | |
library(rvest) | |
library(xml2) | |
library(dplyr) | |
library(RPushbullet) | |
library(methods) | |
dataFolder <- Sys.getenv('HOME') | |
l.devices <- RPushbullet::pbGetDevices() | |
devices <- unlist(lapply(l.devices[['devices']], function(x) x[['nickname']])) | |
# Scrape site and pull back tables of players | |
url <- 'https://fantasy.premierleague.com/player-list/' | |
pos <- c('GLK', 'DEF', 'MID', 'FWD') | |
#tabs <- XML::readHTMLTable(url, as.data.frame = TRUE, stringsAsFactors = FALSE) | |
tabs <- url %>% xml2::read_html(url) %>% html_nodes('.ism-table') %>% html_table() | |
for (i in seq_along(tabs)) { | |
tabs[[i]]$Pos <- pos[(i+1) %/% 2] | |
} | |
new.tables <- dplyr::bind_rows(tabs) | |
filename <- paste0(dataFolder, '/players.csv') | |
if (file.exists(filename)) { | |
# Read in old data | |
old.tables <- read.csv(filename, stringsAsFactors = F, encoding = 'UTF-8') | |
# Compare tables | |
df.new <- dplyr::setdiff(new.tables[, c(1,2,5)], old.tables[, c(1,2,5)]) | |
if (nrow(df.new) > 0) { | |
newPlayers <- paste0(apply(df.new, 1, function(x) paste0(x, collapse = ', ')), collapse='\n') | |
RPushbullet::pbPost('note', 'Transfer News', body = newPlayers, recipients = devices) | |
} | |
} | |
# Write new data to file | |
write.csv(new.tables, filename, row.names=F) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment