better error handling and logic error fixing

This commit is contained in:
2014-12-06 16:40:29 +01:00
parent 0e89203f3c
commit c296884028
2 changed files with 99 additions and 56 deletions
+26 -56
View File
@@ -51,7 +51,8 @@ max_count <- "200"
keep <- c("created_at", "id_str", "text", "retweet_count") keep <- c("created_at", "id_str", "text", "retweet_count")
tweets_full <- data.frame(user=character(), name=character(), created_at=character(), id_str=character(), text=character(), retweet_count=character()) tweets_full <- data.frame(user=character(), name=character(), created_at=character(), id_str=character(), text=character(), retweet_count=character())
tweets_complete <- tweets_full tweets_complete <- tweets_full
for(a in 1:nrow(acc_df)) {
for(a in 94:nrow(acc_df)) {
user <- as.character(acc_df$screenname[a]) user <- as.character(acc_df$screenname[a])
name <- as.character(acc_df$name[a]) name <- as.character(acc_df$name[a])
max_id <- "999999999999999999" max_id <- "999999999999999999"
@@ -66,63 +67,32 @@ for(a in 1:nrow(acc_df)) {
# At first, work with an temporary tweet-DB # At first, work with an temporary tweet-DB
current <- twitter_api_call(api_url, query, api_params) current <- twitter_api_call(api_url, query, api_params)
rm(tweets_temp)
tweets_temp <- fromJSON(correctJSON(current)) tweets_temp <- fromJSON(correctJSON(current))
## START ERROR HANDLING ## ## START ERROR HANDLING ##
# Empty API output
status <- errorEmptyAPI(tweets_temp)
if(status == 1) { Sys.sleep(3);error <- error + 1;next}
if(status == 2) {break}
# Check for empty API returns # Contains "error" column
status <- length(tweets_temp) status <- errorErrorColumn(tweets_temp)
if(status == 0) { if(status == 1) { Sys.sleep(3);error <- error + 1;next}
if(error > 2) { if(status == 2) {break}
cat("[WARNING] 3x empty API result. Aborting now.\n")
break # Check if error code exists
} code <- errorCheckCode(tweets_temp) # 0 if no error
cat("[WARNING] Empty API result. Trying again.\n") if(code == 34) { # page does not exist
rm(tweets_temp) status <- errorCode34
error <- error + 1 if(status == 1) { Sys.sleep(3);error <- error + 1;next}
Sys.sleep(3) if(status == 2) {break}
}
if(code == 88) { # rate limit exceeded
wait <- errorCode88()
Sys.sleep(wait)
next next
} }
# Check if API output contains error fields
status <- "error" %in% names(tweets_temp)
if(status) {
cat("[WARNING] Error in API request:", tweets_temp$error[1],"\n")
rm(tweets_temp)
break
}
# Check for other errors, mostly rate limits
status <- "errors" %in% names(tweets_temp)
if(status) {
cat("[WARNING] Error in API request:", tweets_temp$errors[1,1],"\n")
status <- tweets_temp$errors[1,2]
# "Rate limit exceeded"
if(status == 88) {
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")
Sys.sleep(wait)
}
# "Sorry, that page does not exist"
if(status == 34) {
if(error > 2) {
cat("[WARNING] 3x Not existing page. Aborting now.\n")
break
}
error <- error + 1
}
rm(tweets_temp)
Sys.sleep(3)
next
}
## END ERROR HANDLING ## ## END ERROR HANDLING ##
@@ -173,7 +143,7 @@ for(a in 1:nrow(acc_df)) {
rm(old) rm(old)
tweets_full <- insertRow(tweets_full, tweets_temp) tweets_full <- insertRow(tweets_full, tweets_temp)
rm(tweets_temp) #rm(tweets_temp)
break # End loop because 2013 is reached break # End loop because 2013 is reached
} }
@@ -184,7 +154,7 @@ for(a in 1:nrow(acc_df)) {
loop <- loop + 1 # just for stats loop <- loop + 1 # just for stats
tweets_full <- insertRow(tweets_full, tweets_temp) tweets_full <- insertRow(tweets_full, tweets_temp)
rm(tweets_temp) #rm(tweets_temp)
} }
} # /repeat } # /repeat
+73
View File
@@ -2,6 +2,8 @@ require(stringr)
# Replace characters messing up JSON validation (\,\n,^) # Replace characters messing up JSON validation (\,\n,^)
correctJSON <- function(string) { correctJSON <- function(string) {
string <- gsub('\\\\\\\\\\"(\\w)', '\\1' , string)
string <- gsub('\\\\\\\\\\" ', ' ', string)
string <- gsub("\\\\{2,}", "", string) string <- gsub("\\\\{2,}", "", string)
string <- str_replace_all(string, pattern = "[^[:print:]]", replacement = " ") string <- str_replace_all(string, pattern = "[^[:print:]]", replacement = " ")
string <- str_replace_all(string, pattern = "&..;", replacement = " ") string <- str_replace_all(string, pattern = "&..;", replacement = " ")
@@ -16,3 +18,74 @@ insertRow <- function(existingDF, newrow, r) {
row.names(existingDF) <- 1:nrow(existingDF) row.names(existingDF) <- 1:nrow(existingDF)
return(existingDF) return(existingDF)
} }
## 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)
}