working userloop

This commit is contained in:
2014-12-01 18:38:58 +01:00
parent 1fc222f55c
commit 92179c6fb0
2 changed files with 67 additions and 67 deletions
+65 -56
View File
@@ -8,11 +8,11 @@ setwd("~/Dokumente/Uni/Aktuell/BA-Arbeit/uni-ba-issuecomp")
source("functions.R") source("functions.R")
# Set curl handle for friendly scraping # # Set curl handle for friendly scraping
handle <- getCurlHandle(httpheader = list(from = "max.mehl@uni.kn", # handle <- getCurlHandle(httpheader = list(from = "max.mehl@uni.kn",
'user-agent' = str_c(R.version$version.string) # 'user-agent' = str_c(R.version$version.string)
) # )
) # )
acc_url <- "http://www.bundestwitter.de/api/politiker" acc_url <- "http://www.bundestwitter.de/api/politiker"
#acc_json <- readLines("politiker.txt") #acc_json <- readLines("politiker.txt")
@@ -35,70 +35,79 @@ api_params <- c(
) )
api_url <- "https://api.twitter.com/1.1/statuses/user_timeline.json"; api_url <- "https://api.twitter.com/1.1/statuses/user_timeline.json";
user <- "GregorGysi"
max_count <- "200" max_count <- "200"
max_id <- "999999999999999999"
loop <- 1
keep <- c("created_at", "id_str", "text", "retweet_count") keep <- c("created_at", "id_str", "text", "retweet_count")
rm(tweets_full) rm(tweets_full)
last_id <- NULL
repeat {
# Define specific search query
query <- c(include_rts=1, exclude_replies="true", trim_user="true", include_entities="false",
screen_name=user,
count=max_count,
max_id=max_id);
# If a tweets_full DB already exists (after the first loop this should be the case) for(a in 1:nrow(acc_df)) {
if(exists("tweets_full")) { user <- as.character(acc_df$screenname[a])
current <- twitter_api_call(api_url, query, api_params) name <- as.character(acc_df$name[a])
tweets_temp <- fromJSON(correctJSON(current)) max_id <- "999999999999999999"
tweets_temp <- tweets_temp[keep] loop <- 1
tweets_full <- insertRow(tweets_full, tweets_temp) repeat {
rm(tweets_temp) # Define specific search query
} query <- c(include_rts=1, exclude_replies="true", trim_user="true", include_entities="false",
# First loop screen_name=user,
else { count=max_count,
current <- twitter_api_call(api_url, query, api_params) max_id=max_id);
tweets_full <- fromJSON(correctJSON(current))
tweets_full <- tweets_full[keep]
}
# Now sleep 3 second to dodge 300queries/15min limit # If a tweets_full DB already exists (after the first loop this should be the case)
cat("User:",user,"in loop:",loop,"- now waiting 3 secs...\n") if(exists("tweets_full")) {
Sys.sleep(3) current <- twitter_api_call(api_url, query, api_params)
tweets_temp <- fromJSON(correctJSON(current))
tweets_temp <- tweets_temp[keep]
tweets_temp <- cbind(user=user, name=name, tweets_temp)
tweets_full <- insertRow(tweets_full, tweets_temp)
rm(tweets_temp)
}
# First loop
else {
current <- twitter_api_call(api_url, query, api_params)
tweets_full <- fromJSON(correctJSON(current))
tweets_full <- tweets_full[keep]
tweets_full <- cbind(user=user, name=name, tweets_full)
}
# Is the last tweet in tweets_full from 2013? # Now sleep 3 second to dodge 300queries/15min limit
status <- str_detect(tweets_full$created_at[nrow(tweets_full)], "2013$") cat("User:",user,"in loop:",loop,"- now waiting 2 secs...\n")
# Last loop is reached. Now clear the data frame Sys.sleep(2)
if (status) {
# Delete all tweets from 2013 # Is the last tweet in tweets_full from 2013?
old <- 0 status <- str_detect(tweets_full$created_at[nrow(tweets_full)], "2013$")
for(r in 1:nrow(tweets_full)) { # Last loop is reached. Now clear the data frame
status <- str_detect(tweets_full$created_at[r], "2013$") if (status) {
if(is.na(status)) { status <- FALSE }
if(status) { # Delete all tweets from 2013
old <- old + 1 old <- 0
for(r in 1:nrow(tweets_full)) {
status <- str_detect(tweets_full$created_at[r], "2013$")
if(is.na(status)) { status <- FALSE }
if(status) {
old <- old + 1
}
} }
} if(old > 0) {
if(old > 0) { old <- old - 1
old <- old - 1 tweets_full <- head(tweets_full, -old)
tweets_full <- head(tweets_full, -old) }
} rm(old)
rm(old)
break # End loop because 2013 is reached break # End loop because 2013 is reached
}
# The last tweet is still from 2014, so we need another loop
else {
# Setting max_id to gather next 200 tweets
max_id <- tweets_full$id_str[nrow(tweets_full)]
loop <- loop + 1 # just for stats
}
} }
# The last tweet is still from 2014, so we need another loop # Every tweet from 2014 from user[r] is downloaded. Now next user in for-loop
else { cat("User:",user,"finished after",loop,"loops\n")
# Setting max_id to gather next 200 tweets
max_id <- tweets_full$id_str[nrow(tweets_full)]
loop <- loop + 1 # just for stats
}
} }
# --------------- # ---------------
-9
View File
@@ -10,15 +10,6 @@ correctJSON <- function(string) {
return(string) return(string)
} }
correctJSON2 <- function(string) {
#string <- sub(x=string, pattern = perl('\\\\(?![tn"])'), replacement = " ")
string <- gsub(x=string, pattern = "\n", replacement = " ")
string <- gsub(x=string, pattern = "\r", replacement = " ")
string <- gsub(x=string, pattern = "\\^", replacement = " ")
#\xed\xa0\xbd\xed\xb1\x8d\xed\xa0\xbd\xed\xb8\x8e\
return(string)
}
insertRow <- function(existingDF, newrow, r) { insertRow <- function(existingDF, newrow, r) {
r <- as.numeric(nrow(existingDF)) + 1 r <- as.numeric(nrow(existingDF)) + 1
existingDF <- rbind(existingDF,newrow) existingDF <- rbind(existingDF,newrow)