Created
February 13, 2021 17:49
-
-
Save ryanscharf/b4a0fd5cc145c164e10ca3608ef3d57f to your computer and use it in GitHub Desktop.
Parsing change plate stock/availability from a few different providers
This file contains 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
library(tidyverse) | |
library(rvest) | |
library(jsonlite) | |
library(janitor) | |
library(lubridate) | |
library(httr) | |
library(htmlTable) | |
library(mailR) | |
# Luxiiojun --------------------------------------------------------------- | |
lux_url <- 'https://luxiaojunbarbell.com/collections/plates-bumpers/products/luxiaojun-lb-urethane-change-plates' | |
lux_content <- GET(lux_url) | |
lux_content <- content(lux_content, as = 'text') %>% xml2::read_html() %>% | |
html_nodes('script') %>% .[[64]] %>% html_text() | |
breakpoint1 <- 'window.SwymProductInfo.variants = window.SwymProductInfo.product.variants;\n' | |
lux_content <- str_split(string = lux_content, pattern = breakpoint1, simplify = T) %>% .[1,2] | |
lux_stock <- str_extract_all(lux_content, '(?<=stk: )[:digit:]') %>% set_names('stock') %>% bind_rows() | |
# lux_items <- str_extract_all(lux_content, '(?<=products[:punct:]{2}).+(?=pre-order_620x620)') %>% | |
# set_names('items') %>% bind_rows() | |
lux_items <- str_extract_all(lux_content, '(?<=variants: [:punct:]{2} \").+(?=\" : )') %>% | |
set_names('items') %>% bind_rows() %>% slice(1-nrow(.)) | |
lux_stock <- bind_cols(lux_stock, lux_items) %>% mutate(instock = case_when(stock>0 ~ 1, T ~ 0)) | |
# Vulcan ------------------------------------------------------------------ | |
vulcan_url <- 'https://www.vulcanstrength.com/Vulcan-Prime-Pound-Change-Plates-p/v-pcxx.htm' | |
# vulcan_url %>% xml2::read_html() %>% html_nodes('div') %>% html_nodes('vol-product__form') html_table(fill = T) | |
# vulcan_url %>% xml2::read_html() %>% | |
# html_node(css= '.Multi-Child_Background td') | |
vulcan_tables <- vulcan_url %>% xml2::read_html() %>% | |
html_nodes('table') %>% .[5] %>% html_text() | |
vulcan_items <- | |
str_extract_all(vulcan_tables, '(?<=Vulcan Prime Change Plate ).+(?= - [:alpha:])') %>% set_names('items') %>% bind_rows() | |
vulcan_status <- | |
str_extract_all(vulcan_tables, '(?<=Vulcan Prime Change Plate (Pairs|Set) - [:graph:]{1,3}lb - ).+') %>% | |
set_names('status') %>% bind_rows() | |
vulcan_purchasable <- str_split(vulcan_tables, 'V-PC', simplify = T) %>% .[-1] %>% as.tibble() %>% | |
set_names('strings') %>% | |
mutate( | |
purchasable = case_when( | |
str_detect(.$strings, 'UNAVAILABLE') == T ~ 0, | |
T ~ 1) | |
) %>% select(-strings) | |
vulcan_stock <- bind_cols(vulcan_items, vulcan_status, vulcan_purchasable) | |
# Sorinex ----------------------------------------------------------------- | |
sorinex_url <- 'https://www.sorinex.com/products/change-plates' | |
sorinex_items <- sorinex_url %>% xml2::read_html() %>% | |
html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "product-single__variants no-js", " " ))]') %>% | |
html_text() | |
sorinex_stock <- tibble( | |
bind_rows(set_names(str_extract_all(sorinex_items, '\\d+\\.*\\d*'), 'items')), | |
bind_rows(set_names(str_extract_all(sorinex_items, '(?<=- ).*(?=\n)'), 'status')) | |
) | |
# Titan ------------------------------------------------------------------- | |
titan_urls <- c( | |
titan_black_url = 'https://www.titan.fitness/strength/weight-plates/specialty-plates/lb-black-change-plates/CHBUMP_GROUP.html', | |
titan_color_url = 'https://www.titan.fitness/strength/weight-plates/specialty-plates/lb-color-change-plates/CCHBUMP_GROUP.html' | |
) | |
titan_items <- function(url){ | |
tmp <- url %>% xml2::read_html() %>% | |
html_nodes(xpath='//h3[@class="product-name"]') %>% | |
html_text() %>% str_extract('(?<=\n \n ).*(?=\n)') %>% | |
as.tibble() %>% | |
set_names('items') | |
tmp_status <- url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//span[@class="availability-msg"]') %>% | |
html_text() %>% | |
str_extract('(?<=\t\t\t).*(?=\\.)') %>% | |
as.tibble() %>% | |
set_names('status') | |
return_df <- bind_cols(tmp,tmp_status) | |
return(return_df) | |
} | |
titan_stock <- map_df(titan_urls, titan_items) | |
# Rogue ------------------------------------------------------------------- | |
rogue_url <- 'https://www.roguefitness.com/rogue-lb-change-plates' | |
rogue_items <- rogue_url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//div[(@class = "item-name")]') %>% html_text() %>% as.tibble() | |
set_names('items') | |
rogue_status <- rogue_url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//div[(@class = "bin-stock-availability")]') %>% html_text() %>% | |
.[-1] %>% | |
str_replace_all('\n|\\s\n', '') %>% | |
as.tibble() %>% set_names('status') | |
rogue_stock <- bind_cols(rogue_items, rogue_status) | |
# Rep --------------------------------------------------------------------- | |
rep_url <- 'https://www.repfitness.com/bars-plates/rep-lb-change-plates-2503' | |
rep_items <- rep_url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//strong[(@class = "product-item-name")]') %>% | |
html_text() %>% as.tibble() %>% set_names('items') | |
rep_status_general <- rep_url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//div[@title = "Availability"]') %>% | |
html_text() %>% str_replace_all('\n\\s+', '') | |
if(rep_status_general == 'Out of Stock'){ | |
rep_status <- list(status = 'Out of Stock') | |
} else{ | |
rep_status <- rep_url %>% xml2::read_html() %>% | |
html_nodes(xpath = '//td[@data-th = "Qty"]') %>% html_text() %>% | |
str_replace_all('\n\\s+', '') %>% as.tibble() %>% set_names('status') %>% | |
transmute(status = case_when(status == 'Out of stock' ~ 'Out of Stock', T ~ 'In Stock')) | |
} | |
rep_stock <- bind_cols(rep_items, rep_status) | |
# Eleiko ------------------------------------------------------------------ | |
ele_url <- 'https://shop.eleiko.com/en-us/equipment/discs/weightlifting/3001318-eleiko-rubber-coated-disc-1-25-lbs' | |
'//*[contains(concat( " ", @class, " " ), concat( " ", "product-detail", " " ))]' | |
# wrap up and send out ---------------------------------------------------- | |
change_stock <- | |
bind_rows( | |
lst( | |
lux_stock, | |
rep_stock, | |
rogue_stock, | |
sorinex_stock, | |
titan_stock, | |
vulcan_stock | |
), | |
.id = 'manufacturer' | |
) | |
body <- paste0("<html><head> | |
<style> | |
body{font-family:Calibri, sans-serif;} | |
table{border-left:1px solid #000000;border-top:1px solid #000000;} | |
table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:13px; font-weight:bold; margin: 0px; padding-left: 5px; padding-right: 5px; margin: 0px;} | |
table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:13px; font-weight:normal; margin: 0px; padding-left: 5px; padding-right: 5px; margin: 0px;} | |
</style> | |
</head><body><p> This is a test email. Ignore it.</p>", | |
htmlTable::htmlTable(change_stock, rnames = F), | |
"</body></html>") | |
mailR::send.mail(from = '', to = 'r', | |
body = body, | |
smtp = list(host.name = '', | |
port = , | |
user.name = '', | |
passwd = '', ssl = T), | |
authenticate = T, | |
send = T, | |
html = T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment