-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGiveawayScript.java
More file actions
175 lines (154 loc) · 3.69 KB
/
GiveawayScript.java
File metadata and controls
175 lines (154 loc) · 3.69 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
import twitter4j.*;
import java.util.ArrayList;
/**
* @author Aashish Jain
* @version September 20, 2015
*
* Runs a script to find giveaways and retweet/follow if necessary
*/
public class GiveawayScript
{
/**
* The twitterAccount to run the script on
*/
private static TwitterAccount tA;
/**
* The number of refresh iterations
*/
private static final int REFRESH_ITERATIONS = 5;
/**
* The sleep time after reaching proper refresh iterations
*/
private static final int SLEEP_TIME = 5000;
/**
* The search term
*/
private static final String searchTerm = "giveaway";
/**
* The twitterAccounts twitter instance
*/
private static Twitter twitter;
/**
* Initializes everything necessary and runs script
* @param args
* @throws twitter4j.TwitterException
* @throws java.lang.InterruptedException
*/
public static void main(String[] args) throws twitter4j.TwitterException,
java.lang.InterruptedException
{
initializeTwitterAccount();
twitter = tA.getTwitter();
runScript();
}
/**
* Initializes the twitterAccount with the proper keys
*/
public static void initializeTwitterAccount()
{
String cK = "";
String cS = "";
String aT = "";
String aTS = "";
tA = new TwitterAccount(cK, cS, aT, aTS);
}
/**
* Runs the giveaway script
* @throws twitter4j.TwitterException
* @throws java.lang.InterruptedException
*/
public static void runScript() throws twitter4j.TwitterException, java
.lang.InterruptedException
{
int currentIteration = 0;
while (currentIteration < REFRESH_ITERATIONS)
{
ArrayList<Status> searches = getSearch(searchTerm);
if (searches.size() == 0)
{
System.out.println("No tweets found with current search term " +
searchTerm);
}
for (Status status : searches)
{
/*
Prints out the search
*/
System.out.println("@" + status.getUser().getScreenName() + ":" +
status.getText());
StatusUtils sU = new StatusUtils(status);
boolean follow = sU.checkToFollow();
boolean retweet = sU.checkToRetweet();
if ((!follow && !retweet) || status.isRetweetedByMe())
{
System.out.println("IGNORE");
continue;
}
if (status.isRetweet())
{
status = status.getRetweetedStatus();
}
if (follow)
{
try
{
twitter.createFriendship(sU.getPersonToFollow());
System.out.println("FOLLOWED");
fixFollowers();
}
catch (Exception e)
{
System.out.println("Threw error");
}
}
if (retweet)
{
try
{
twitter.retweetStatus(status.getId());
System.out.println("RETWEETED");
}
catch (Exception e)
{
System.out.println("Threw error");
}
}
currentIteration++;
if (currentIteration == REFRESH_ITERATIONS)
{
System.out.println("sleeping");
Thread.sleep(SLEEP_TIME);
currentIteration = 0;
}
}
}
}
/**
* Fixes followers if account goes over the limit
* @throws twitter4j.TwitterException
*/
public static void fixFollowers() throws twitter4j.TwitterException
{
long[] friends = twitter.getFriendsIDs(-1).getIDs();
if (friends.length > 2000)
{
for (int i = 0; i < friends.length / 2; i++)
{
twitter.destroyFriendship(friends[i]);
}
}
}
/**
* Searches twitter for the given search term.
* @param searchTerm The search term to search twitter for.
* @return An ArrayList of the Status' searches
* @throws twitter4j.TwitterException exception for search
*/
public static ArrayList<Status> getSearch(String searchTerm) throws twitter4j
.TwitterException
{
Query q = new Query(searchTerm);
QueryResult result = twitter.search(q);
return (ArrayList<Status>) result.getTweets();
}
}