-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.R
66 lines (53 loc) · 1.84 KB
/
collect.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'
Script : Collector
Created : November 21, 2014
Author(s) : iHub Research
Version : v1.0
License : Apache License, Version 2.0
'
#============================= Collect Tweets =============================================================================
# Load Authentication File
load("twitteR_credentials")
# Register Authentication
registerTwitterOAuth(twitCred)
#============================ Capture Tweets and Push to Database ===========================================================================
# Twitter ID for Tracked Account
UserID = ''
#Capture Tweets and Push to Database
while(TRUE)
{
# Load Keyword file and Convert to Characters
keywords <-unlist(read.csv('keys.csv',header=FALSE))
chr_keywords <-as.character(keywords)
# Capture Tweets via the Streaming API
filterStream(file.name="tweets.json",track=chr_keywords,tweets=0,timeout=10,oauth=twitCred)
# Convert Tweets from JSON to Data Frame
try(a <-parseTweets('tweets.json'))
# Listen for New Keyword/Hashtag
for(thisid in a$user_id_str){
if(thisid == UserID){
tmp <-subset(a,a$user_id_str == UserID)
tmp2<-tmp[,c(1)]
# Add new keyword
sink('keys.csv',append=TRUE)
cat(c(',',tmp2))
sink()
# Log the change
log <-file('log.txt')
report <-paste('The Keyword',tmp2,'has been added', 'at', Sys.time())
writeLines(text=report,con=log,sep=",")
close(log)
# Capture Tweets via Search API
search <-searchTwitter(tmp2)
try(search_df <-twListToDF(search))
# Push Search results to DB in separate table
dbWriteTable(con1,'search_table',search_df,append=TRUE)
dbCommit(con)
}
}
# Write Tweets to Database
dbWriteTable(con,'tweet_data',a,append=TRUE)
dbCommit(con)
# Delete JSON file after Push to DB
file.remove('tweets.json')
}