Files
uni-ba-socialagenda/issuecomp-functions.R
T

113 lines
3.0 KiB
R
Raw Normal View History

2014-11-29 13:52:09 +01:00
require(stringr)
2014-11-29 21:41:28 +01:00
# Replace characters messing up JSON validation (\,\n,^)
correctJSON <- function(string) {
string <- gsub('\\\\\\\\\\"(\\w)', '\\1' , string)
string <- gsub('\\\\\\\\\\" ', ' ', string)
2014-12-05 13:21:53 +01:00
string <- gsub("\\\\{2,}", "", string)
2014-12-01 17:41:33 +01:00
string <- str_replace_all(string, pattern = "[^[:print:]]", replacement = " ")
2014-12-05 12:28:51 +01:00
string <- str_replace_all(string, pattern = "&..;", replacement = " ")
2014-11-29 21:41:28 +01:00
string <- str_replace_all(string, pattern = perl('\\\\(?![tn"])'), replacement = " ")
2014-11-30 18:58:47 +01:00
return(string)
}
2014-11-30 03:41:23 +01:00
insertRow <- function(existingDF, newrow, r) {
r <- as.numeric(nrow(existingDF)) + 1
existingDF <- rbind(existingDF,newrow)
existingDF <- existingDF[order(c(1:(nrow(existingDF)-1),r-0.5)),]
row.names(existingDF) <- 1:nrow(existingDF)
return(existingDF)
2014-12-01 23:17:55 +01:00
}
2015-01-12 14:59:51 +01:00
convertLogical0 <- function(var) {
if(is.integer(var) && length(var) == 0) {
var <- 0
}
return(var)
}
smartPatternMatch <- function(string, pattern, chars) {
if(chars < 5) {
found <- agrep(pattern, string, max.distance = list(all = 0), ignore.case = TRUE)
}
if(chars > 7) {
found <- agrep(pattern, string, max.distance = list(all = 2), ignore.case = TRUE)
}
else {
found <- agrep(pattern, string, max.distance = list(all = 1), ignore.case = TRUE)
}
found <- convertLogical0(found)
return(found)
}
## ERROR HANDLING
# Check for empty API returns (0 or 1 or 2)
errorEmptyAPI <- function(df) {
status <- length(df)
if(status == 0) {
if(error < 3) {
cat("[WARNING] Empty API result. Trying again.\n")
returncode <- 1
}
else {
cat("[WARNING] 3x empty API result. Aborting now.\n")
returncode <- 2
}
}
else {
returncode <- 0
}
return(returncode)
}
# Check if API output contains error fields (0 or 2)
errorErrorColumn <- function(df) {
status <- "error" %in% names(df)
if(status) {
cat("[WARNING] Error in API request:", df$error[1],"\n")
returncode <- 2
}
else {
returncode <- 0
}
return(returncode)
}
# Check if error codes exist (i.e. 34 or 88)
errorCheckCode <- function(df) {
status <- "errors" %in% names(df)
if(status) {
cat("[WARNING] Error in API request:", df$errors[1,1],"\n")
code <- df$errors[1,2]
}
else {
code <- 0
}
return(code)
}
# Handle code 88: rate limit exceeded (wait time)
errorCode88 <- function() {
rate_api_url <- "https://api.twitter.com/1.1/application/rate_limit_status.json"
rate_query <- c(resources="statuses")
resettime <- fromJSON(twitter_api_call(rate_api_url, rate_query, api_params))
resettime <- resettime$resources$statuses$`/statuses/user_timeline`$reset
curtime <- as.numeric(as.POSIXct(Sys.time()))
wait <- round(resettime - curtime + 10)
cat("[INFO] Rate limit is exceeded. Now waiting",wait,"seconds.\n")
return(wait)
}
# Handle code 34: Page does not exist (1 or 2)
errorCode34 <- function() {
if(error > 2) {
cat("[WARNING] 3x Not existing page. Aborting now.\n")
returncode <- 2
}
else {
returncode <- 1
}
return(returncode)
}