-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRSControlPanel.java
More file actions
325 lines (292 loc) · 8.27 KB
/
Copy pathRSControlPanel.java
File metadata and controls
325 lines (292 loc) · 8.27 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
GUI for Reddit Scraper
*/
import javax.swing.*;
import java.util.*;
import javax.swing.UIManager.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URI;
public class RSControlPanel extends JFrame
{
private int WIDTH;
private int HEIGHT;
private boolean hot;
private boolean fresh;
private boolean all;
private boolean html;
private boolean api;
private JButton start;
private JButton stop;
private JButton clearConsole;
private JRadioButton hotButton;
private JRadioButton freshButton;
private JRadioButton htmlButton;
private JRadioButton apiButton;
private JCheckBox allCheckBox;
private JTextField redditLocationField;
private JTextField subredditField;
private redditScraper rsScraper;
private Thread activeThread;
private RSEditorPane consolePane;
private RSEditorPane deletePane;
private redditScraper scraper;
private ArrayList<redditScraper> RSThreads = new ArrayList<redditScraper>();
private int numThreads = 0;
private String REDDIT_ROOT = "all";
public RSControlPanel()
{
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception e){
}
WindowAdapter windowAdapter = new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
addWindowListener(windowAdapter);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// The basic frame construction settings
setTitle("Reddit Scraper");
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
WIDTH = dim.width / 2;
HEIGHT = dim.height / 2;
setBounds(0, 0,WIDTH,HEIGHT);
//Layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.75;
redditLocationField = new JTextField(50);
redditLocationField.setText(REDDIT_ROOT);
add(redditLocationField,c);
c.gridx = 1;
c.weightx = .75;
subredditField = new JTextField(50);
subredditField.setText("Set Subreddit Destination");
add(subredditField,c);
c.weightx = .10;
c.gridx = 2;
start = new JButton("Start");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
startAThread();
}
});
add(start,c);
c.gridx = 3;
stop = new JButton("Stop");
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
stopAThread();
}
});
add(stop,c);
c.gridx=4;
clearConsole = new JButton("Clear Console");
clearConsole.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
consolePane.setBody(" ");
}
});
add(clearConsole,c);
//---------------------------------------------
c.gridx= 0;
c.gridy = 1;
c.weightx = .75;
c.weighty = 0;
c.gridwidth = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_START;
hotButton = new JRadioButton("HOT");
hotButton.setSelected(true);
freshButton = new JRadioButton("NEW");
htmlButton = new JRadioButton("HTML");
apiButton = new JRadioButton("API");
allCheckBox = new JCheckBox("r/all");
allCheckBox.setSelected(false);
hotButton.addItemListener(new guiButtonListener());
freshButton.addItemListener(new guiButtonListener());
htmlButton.addItemListener(new guiButtonListener());
apiButton.addItemListener(new guiButtonListener());
allCheckBox.addItemListener(new guiButtonListener());
allCheckBox.setSelected(true);
ButtonGroup popularityType = new ButtonGroup();
popularityType.add(hotButton);
popularityType.add(freshButton);
add(hotButton,c);
c.gridx=1;
add(freshButton,c);
c.gridx=2;
ButtonGroup requestType = new ButtonGroup();
requestType.add(htmlButton);
requestType.add(apiButton);
add(htmlButton,c);
c.gridx=3;
add(apiButton,c);
c.gridx=4;
add(allCheckBox,c);
//---------------------------------------------
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 5;
c.fill = GridBagConstraints.BOTH;
JPanel leftPane = new JPanel();
JPanel rightPane = new JPanel();
consolePane = new RSEditorPane();
consolePane.setContentType("text/html");
consolePane.setEditable(false);
consolePane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
//System.out.println("Description: " + hle.getDescription() +
//"Event Type: " + hle.getEventType());
if(HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
//System.out.println("LINK CLICKED" + hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
String url = hle.getDescription();
String httpHeader = "";
if(url.contains("http://") == false && url.contains("www.") == false)
{
httpHeader = "http://";
}
String URIConversion = httpHeader + hle.getDescription();
URIConversion = URIConversion.trim();
URI website = new URI(URIConversion);
desktop.browse(website);
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
});
deletePane = new RSEditorPane();
deletePane.setContentType("text/html");
deletePane.setEditable(false);
deletePane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
//System.out.println("Description: " + hle.getDescription() +
//"Event Type: " + hle.getEventType());
if(HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
//System.out.println("LINK CLICKED" + hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
String url = hle.getDescription();
String httpHeader = "";
if(url.contains("http://") == false && url.contains("www.") == false)
{
httpHeader = "http://";
}
String URIConversion = httpHeader + hle.getDescription();
URIConversion = URIConversion.trim();
URI website = new URI(URIConversion);
desktop.browse(website);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
JSplitPane split = new JSplitPane();
JScrollPane sp = new JScrollPane(consolePane);
split.setLeftComponent(sp);
JScrollPane sp2 = new JScrollPane(deletePane);
split.setRightComponent(sp2);
add(split,c);
setVisible(true);
}
public void startAThread()
{
if(numThreads <= 8)
{
scraper = new redditScraper();
RSThreads.add(scraper);
scraper.setRedditLocation(redditLocationField.getText());
scraper.setSubmitSubreddit(subredditField.getText());
scraper.setConsoleRSPane(consolePane);
scraper.setDeleteRSPane(deletePane);
scraper.setUseAPIRequests(api);
scraper.setSearchTopPosts(hot);
scraper.setIsSubredditAll(all);
activeThread = new Thread(scraper);
activeThread.start();
numThreads++;
}
}
public void stopAThread()
{
try{
for(int i = 0;i < RSThreads.size();i++)
{
RSThreads.get(i).setWatching(false);
consolePane.append("Stopping thread # " + i + ". Threads must run their course before stopping");
}
}catch(Exception e){
}
}
public static void main(String[] args)
{
RSControlPanel visuals = new RSControlPanel();
}
public class guiButtonListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItemSelectable();
if(source == apiButton)
{
api = true;
html = false;
}else if(source == htmlButton){
html = true;
api = false;
}else if(source == freshButton){
fresh = true;
hot = false;
api = true;
html = false;
allCheckBox.setSelected(false);
apiButton.setSelected(true);
}else if(source == hotButton){
hot = true;
fresh = false;
}else if(source == allCheckBox)
{
if(e.getStateChange() == ItemEvent.SELECTED) {
all = true;
html = true;
api = false;
fresh = false;
htmlButton.setSelected(true);
}else{
all = false;
apiButton.setSelected(true);
api = true;
}
}
}
}
}