R/oscn_lastcase.R

Defines functions oscn_lastcase

oscn_lastcase <- function(courts, casetypes, years) {
  
  ctlist <- casetype_codes %>%
    filter(casetype %in% str_to_upper(casetypes))
  
  ctlist <- ctlist$casetype_id
  
  for (l in courts) {
    court_tmp <- l
    
    for (k in ctlist) {
      casetype_id <- k
      
      for (j in years) {
        year_tmp <- j
        
        if (year_tmp == year(Sys.Date())) {
          start_date <- Sys.Date()
        } else {
          start_date <- paste0(year_tmp, "-12-31") %>% 
            ymd()
        }
        
        for (i in start_date:ymd(paste0(year_tmp, "-01-01"))) {
          
          i <- as_date(i)
          
          d <- try(read_html(httr::GET(paste0("http://www.oscn.net/dockets/Results.aspx?db=",
                                              str_remove_all(court_tmp, " "),
                                              "&number=&lname=&fname=&mname=&DoBMin=&DoBMax=&partytype=&apct=&dcct=",
                                              casetype_id,
                                              "&FiledDateL=",
                                              month(i), "%2F", day(i), "%2F", year_tmp,
                                              "&FiledDateH=12%2F31%2F", year_tmp,
                                              "&ClosedDateL=&ClosedDateH=&iLC=&iLCType=&iYear=&iNumber=&citation="),
                                       config = httr::config(ssl_verifypeer = FALSE))))
          
          if (class(d) != "try-error") {
            
            message("Checked ", i)
            
            if (d %>% html_node("body") %>%
                html_text() %>%
                str_detect("Found No Records") == FALSE & 
                d %>% html_node("body") %>%
                html_text() %>%
                str_detect(paste0("-", year_tmp, "-")) == TRUE) {
              
              d <- d %>%
                html_node("table") %>%
                html_table()
              
              names(d) <- c("casenum", "file_date", "casename", "party")
              
              d <- d %>% 
                mutate(seq = str_extract(casenum, "\\d{1,5}(?=$|[[:alpha:]])") %>% 
                         as.numeric,
                       file_year = str_sub(casenum, 4, 7) %>% 
                         as.numeric) %>% 
                filter(file_year == year_tmp)
              
              casetype_tmp <- casetype_codes[casetype_id == casetype_codes$casetype_id, "casetype"] %>% 
                as.character()
              
              if (nrow(d) > 0) {
                
                if (exists("lastcase")) {
                  lastcase <<- bind_rows(tibble(court = str_to_upper(court_tmp),
                                                file_year = year_tmp,
                                                casetype = casetype_tmp,
                                                casetype_id = casetype_id,
                                                last_case = max(d$seq)),
                                         lastcase)
                } else {lastcase <<- tibble(court = str_to_upper(court_tmp),
                                            file_year = year_tmp,
                                            casetype = casetype_tmp,
                                            casetype_id = casetype_id,
                                            last_case = max(d$seq, na.rm = TRUE))
                }
                return(max(d$seq, na.rm = TRUE))
              }
            }
          }
        }
      }
    }
  }
}
openjusticeok/ojo documentation built on Feb. 2, 2021, 5:47 a.m.