My favorites
▼
|
Sign in
withgod
withgod's public codes
Project Home
Downloads
Wiki
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
twitter
/
StreamingDemo.java
r36
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
package sample;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import org.freehep.swing.layout.FlowScrollLayout;
import edu.stanford.ejalbert.BrowserLauncher;
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
public class StreamingDemo {
JFrame mainFrame;
JPanel mainPanel;
public StreamingDemo(String twid, String twpass) {
mainFrame = new JFrame("streaming sample");
mainFrame.setSize(850, 750);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
JScrollPane jspane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
FlowScrollLayout f = new FlowScrollLayout(jspane);
f.setAlignment(FlowLayout.LEFT);
mainPanel.setLayout(f);
mainFrame.getContentPane().add(jspane);
//mainFrame.setLayout(null);
mainFrame.setVisible(true);
StreamingListener sl = new StreamingListener(mainPanel, twid, twpass);
sl.start();
}
class StreamingListener extends Thread {
JPanel parent;
boolean cnt = true;
String twid = null;
String twpass = null;
//Random rand = null;
public StreamingListener(JPanel parent, String twid, String twpass) {
this.parent = parent;
//rand = new Random();
this.twid = twid;
this.twpass = twpass;
}
public void run() {
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
String[] strs = status.getText().split(" ");
for (String str: strs) {
if (str.startsWith("http://")) {
String url = null;
String urlOrg = str;
if (str.indexOf("twitpic") > 0) {
url = str.replace("com/", "com/show/thumb/");
} else if (str.indexOf("yfrog") > 0) {
url = str + ".th.jpg";
} else if (str.indexOf("movapic") > 0) {
url = str.replace("movapic.", "image.movapic.");
url = url.replace("com/pic/", "com/pic/t_");
url += ".jpeg";
}
if (str != null) {
System.out.println(urlOrg);
System.out.println(url);
try {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
ImageIcon icon = new ImageIcon(new URL(url));
JButton btn = new JButton(icon);
btn.addActionListener(new ClickEvent(urlOrg));
p.add(btn);
p.add(new JTextField(urlOrg));
/* layout = null してランダム配置しようとしたが、
* validate invalidateしても描画されないのでやめた
*/
/*
int w = icon.getIconWidth();
int h = icon.getIconHeight();
int x = rand.nextInt(1000);
int y = rand.nextInt(750);
p.setBounds(x, y, w, h);
*/
parent.add(p);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
System.out.println(status.getUser().getName() + " : " + status.getText());
parent.invalidate();
parent.validate();
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
};
TwitterStream twitterStream = new TwitterStreamFactory(listener).getInstance(this.twid, this.twpass);
FilterQuery fq = new FilterQuery();
String[] q = {"twitpic", "yfrog", "movapic"};
fq.track(q);
try {
twitterStream.filter(fq);
} catch (TwitterException e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("StreamingDemo twitter_id twitter_pass");
System.exit(0);
}
new StreamingDemo(args[0], args[1]);
}
class ClickEvent implements ActionListener {
String url = null;
public ClickEvent(String url) {
this.url = url;
}
public void actionPerformed(ActionEvent e) {
System.out.println(url);
try {
BrowserLauncher launcher = new BrowserLauncher();
launcher.openURLinBrowser(url);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
Show details
Hide details
Change log
r36
by nocontents on Apr 22, 2010
Diff
twitter4j Streaming Demo
Go to:
/trunk/twitter/StreamingDemo.java
Older revisions
All revisions of this file
File info
Size: 4718 bytes, 152 lines
View raw file
Powered by
Google Project Hosting