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
+21 -12
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,14 +35,16 @@ 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 { for(a in 1:nrow(acc_df)) {
user <- as.character(acc_df$screenname[a])
name <- as.character(acc_df$name[a])
max_id <- "999999999999999999"
loop <- 1
repeat {
# Define specific search query # Define specific search query
query <- c(include_rts=1, exclude_replies="true", trim_user="true", include_entities="false", query <- c(include_rts=1, exclude_replies="true", trim_user="true", include_entities="false",
screen_name=user, screen_name=user,
@@ -54,6 +56,7 @@ repeat {
current <- twitter_api_call(api_url, query, api_params) current <- twitter_api_call(api_url, query, api_params)
tweets_temp <- fromJSON(correctJSON(current)) tweets_temp <- fromJSON(correctJSON(current))
tweets_temp <- tweets_temp[keep] tweets_temp <- tweets_temp[keep]
tweets_temp <- cbind(user=user, name=name, tweets_temp)
tweets_full <- insertRow(tweets_full, tweets_temp) tweets_full <- insertRow(tweets_full, tweets_temp)
rm(tweets_temp) rm(tweets_temp)
} }
@@ -62,11 +65,12 @@ repeat {
current <- twitter_api_call(api_url, query, api_params) current <- twitter_api_call(api_url, query, api_params)
tweets_full <- fromJSON(correctJSON(current)) tweets_full <- fromJSON(correctJSON(current))
tweets_full <- tweets_full[keep] tweets_full <- tweets_full[keep]
tweets_full <- cbind(user=user, name=name, tweets_full)
} }
# Now sleep 3 second to dodge 300queries/15min limit # Now sleep 3 second to dodge 300queries/15min limit
cat("User:",user,"in loop:",loop,"- now waiting 3 secs...\n") cat("User:",user,"in loop:",loop,"- now waiting 2 secs...\n")
Sys.sleep(3) Sys.sleep(2)
# Is the last tweet in tweets_full from 2013? # Is the last tweet in tweets_full from 2013?
status <- str_detect(tweets_full$created_at[nrow(tweets_full)], "2013$") status <- str_detect(tweets_full$created_at[nrow(tweets_full)], "2013$")
@@ -97,8 +101,13 @@ repeat {
max_id <- tweets_full$id_str[nrow(tweets_full)] max_id <- tweets_full$id_str[nrow(tweets_full)]
loop <- loop + 1 # just for stats loop <- loop + 1 # just for stats
} }
}
# Every tweet from 2014 from user[r] is downloaded. Now next user in for-loop
cat("User:",user,"finished after",loop,"loops\n")
} }
# --------------- # ---------------
-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)