R/sbscrape_twitch.R

#'Scrape monthly Twitch statistics from socialblade.com
#'
#'@param url will only work with https://socialblade.com/.../monthly links
#'
#'@keywords twitch
#'@concept socialblade
#'@export
#'@examples
#'sbscrape_twitch("https://socialblade.com/twitch/user/enthusiast_gaming/monthly")

sbscrape_twitch<-function(url){

  # require(rvest,quietly = TRUE)
  # require(lubridate,quietly=TRUE)

  tryCatch(
    {
      pipeit<-function(url,code){
        read_html(url)%>%html_nodes(code)%>%html_text()
      }

      fl<-pipeit(url,
                 "#socialblade-user-content div div div:nth-child(2) div:nth-child(2)")
      fl<-gsub("[A-Z]","",fl)
      fl<-trimws(fl[grepl("[0-9]",fl)])

      vv<-pipeit(url,
                 "#socialblade-user-content div div div:nth-child(3) div:nth-child(2)")
      dates<-c()
      for(i in 1:30){
        dates[i]<-Sys.Date()-i+1
      }
      dates<-rev(as_date(dates))
      fl<-as.numeric(sapply(as.character(fl),
                            function(x) gsub(",","",x)))
      vv<-as.numeric(sapply(as.character(vv),
                            function(x) gsub(",","",x)))

      data.frame(Date=dates,"Culmative Followers"=fl,"Culmulative Views"=vv)
    },
    error=function(error_message){
      message("Error in sbscrape_twitch(): Please enter valid url. ")
      message("sbscrape will only work with https://socialblade.com/twitch/user.../monthly links")
    })
}

Try the sbscrapeR package in your browser

Any scripts or data that you put into this service are public.

sbscrapeR documentation built on June 4, 2019, 5:03 p.m.