-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterGrabber.java
More file actions
183 lines (150 loc) · 5.37 KB
/
TwitterGrabber.java
File metadata and controls
183 lines (150 loc) · 5.37 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import twitter4j.Query;
import twitter4j.Query.ResultType;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterGrabber {
public Connection connection = null;
public Statement statement = null;
public ResultSet resultsSet = null;
Twitter twitter = null;
public TwitterGrabber() {
//Initialize our connection to twitter:
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(LoginCredentials.twitterConsumerKey).setOAuthConsumerSecret(LoginCredentials.twitterConsumerSecret).setOAuthAccessToken(LoginCredentials.twitterAccessToken).setOAuthAccessTokenSecret(LoginCredentials.twitterAccessSecret);
TwitterFactory factory = new TwitterFactory(cb.build());
twitter = factory.getInstance();
} catch (Exception te) {
te.printStackTrace();
}
//Initialize our conenction with the oracle DB:
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:@oracle.cise.ufl.edu:1521:orcl", LoginCredentials.oracleUsername, LoginCredentials.oraclePassword);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
//Initialize a statement so we can send statements to the DB:
try {
statement = connection.createStatement();
} catch (SQLException e) {
System.out.println(e.toString());
}
}
//////////////////////////////////////////////////////////
// Functions start for the oracle DB: //
//////////////////////////////////////////////////////////
//Drops and purges a given table name
public String deleteOracleTable(String table) {
try {
resultsSet = statement.executeQuery("drop table " + table);
resultsSet = statement.executeQuery("purge table " + table);
//resultsSet = statement.executeQuery("create table TwitterDB(student_id integer,student_name varchar(25) not null,state varchar(2) not null,date_of_birth date,account_balance float,primary key (student_id))");
if (resultsSet.next()) {
System.out.println("Details: " + resultsSet.getString(1));
return resultsSet.getString(1);
}
} catch (SQLException e) {
System.out.println(e.toString());
}
return null;
}
//Creates a table given a string of the table + schema:
public String createOracleTable(String table) {
try {
resultsSet = statement.executeQuery("create table " + table);
if (resultsSet.next()) {
System.out.println("Details: " + resultsSet.getString(1));
return resultsSet.getString(1);
}
} catch (SQLException e) {
System.out.println(e.toString());
}
return null;
}
//Inserts a value into the given table
public String insertIntoTable(String table, String values) {
try {
resultsSet = statement.executeQuery("insert into " + table + " values(" + values + ")");
if (resultsSet.next()) {
System.out.println("Details: " + resultsSet.getString(1));
return resultsSet.getString(1);
}
} catch (SQLException e) {
System.out.println(e.toString());
}
return null;
}
///////////////////////////////////
//End of functions for Oracle DB //
///////////////////////////////////
//////////////////////////////////////////////////////////
// Start of functions for Twitter grabbing: //
//////////////////////////////////////////////////////////
//This function returns an array list of statuses with the 1000 most recent tweets with a given hash tag:
public ArrayList<Status> getTweetsWithHashtag(String hashTag, String fromDate, String toDate, long max_id, boolean decreasing, long last_id) {
ArrayList<Status> tweetData = new ArrayList<Status>();
//Try to query for hashtag:
// get the 1000 most recent tweets tagged #debatenight
for (int page = 1; page <= 1; page++) {
Query query = new Query(hashTag);
query.count(100);
query.setSince(fromDate);
query.setUntil(toDate);
if (max_id != -1) {
if (decreasing) {
query.setMaxId(max_id);
} else {
query.setSinceId(max_id);
}
}
//query.resultType(ResultType.mixed);
try {
QueryResult qr = twitter.search(query);
Status last = null;
while (qr.hasNext()) {
List<Status> qrTweets = qr.getTweets();
// break out if there are no more tweets
//Or if were over 5000 tweets:
if(qrTweets.size() == 0 || tweetData.size() > 10000) break;
for(Status t : qrTweets) {
System.out.println(t.getText());
//If this is a retweet, then get the tweet that was retweeted:
if (t.isRetweet()) {
tweetData.add(t.getRetweetedStatus());
} else {
tweetData.add(t);
}
}
qr = twitter.search(qr.nextQuery());
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
return tweetData;
}
public String selectItem(String table, String valueToCheck, Long value) {
try {
resultsSet = statement.executeQuery("select * from " + table + " where " + valueToCheck + " = " + value);
if (resultsSet.next()) {
System.out.println("Details: " + resultsSet.getString(1));
return resultsSet.getString(1);
}
} catch (SQLException e) {
System.out.println(e.toString());
}
return null;
}
}