package com;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import weibo4j.Comments;
import weibo4j.Friendships;
import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.http.AccessToken;
import weibo4j.model.Status;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import java.awt.Font;
/**
* @author 刘显安
* 简单的微博抢沙发工具
* 日期:2012年11月11日
*/
public class MainFrame extends JFrame
{
private JPanel contentPane;
private JTextField textFieldCode;
private JComboBox comboBox ;
private AccessToken accessToken;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame()
{
setTitle("\u7B80\u5355\u7684\u5FAE\u535A\u62A2\u6C99\u53D1\u5DE5\u5177 by \u5218\u663E\u5B89");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 434, 411);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textFieldCode = new JTextField();
textFieldCode.setBounds(182, 78, 161, 30);
contentPane.add(textFieldCode);
textFieldCode.setColumns(10);
JButton btnNewButton = new JButton("\u7B2C\u4E00\u6B65\uFF1A\u6253\u5F00\u6388\u6743\u9875\u9762");
btnNewButton.setFont(new Font("SansSerif", Font.PLAIN, 18));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openUrl();
}
});
btnNewButton.setBounds(52, 16, 291, 50);
contentPane.add(btnNewButton);
comboBox = new JComboBox();
comboBox.setBounds(138, 185, 205, 41);
contentPane.add(comboBox);
JButton btnNewButton_1 = new JButton("\u7B2C\u4E8C\u6B65\uFF1A\u83B7\u53D6\u597D\u53CB\u5217\u8868");
btnNewButton_1.setFont(new Font("SansSerif", Font.PLAIN, 18));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getFriends();
}
});
btnNewButton_1.setBounds(52, 120, 291, 53);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u7B2C\u4E09\u6B65\uFF1A\u5F00\u59CB\u76D1\u63A7");
btnNewButton_2.setFont(new Font("SansSerif", Font.PLAIN, 18));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
btnNewButton_2.setBounds(52, 238, 291, 59);
contentPane.add(btnNewButton_2);
JLabel lblNewLabel = new JLabel("\u6388\u6743\u9875\u9762\u8FD4\u56DE\u7684code\uFF1A");
lblNewLabel.setBounds(52, 84, 133, 18);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u60A8\u7684\u597D\u53CB\u5217\u8868\uFF1A");
lblNewLabel_1.setBounds(52, 196, 90, 18);
contentPane.add(lblNewLabel_1);
JLabel lblByhttpweibocomliuxianan = new JLabel("by \u5218\u663E\u5B89 \u5FAE\u535A\uFF1Ahttp://weibo.com/liuxianan");
lblByhttpweibocomliuxianan.setBounds(62, 309, 261, 18);
contentPane.add(lblByhttpweibocomliuxianan);
JLabel label = new JLabel("\u65E5\u671F\uFF1A2012\u5E7411\u670811\u65E5");
label.setBounds(198, 339, 145, 18);
contentPane.add(label);
}
public Date minTime;//最近一条微博的更新时间
/**
* 打开授权页面
*/
public void openUrl()
{
try
{
BareBonesBrowserLaunch.openURL(new Oauth().authorize("code"));
}
catch (WeiboException e)
{
e.printStackTrace();
}
}
/**
* 获取当前登录用户的好友列表
*/
public void getFriends()
{
try
{
//获取好友列表
accessToken=new Oauth().getAccessTokenByCode(textFieldCode.getText());
Friendships friendships=new Friendships();
friendships.setToken(accessToken.getAccessToken());
for(User user:friendships.getFriendsByID(accessToken.getUid()).getUsers())
{
comboBox.addItem(user.getName());
}
}
catch (WeiboException e)
{
e.printStackTrace();
}
}
/**
* 开始监控关注用户的微博
* @throws WeiboException
*/
public void start()
{
try
{
System.out.println("开始监控");
Timeline timeline=new Timeline();
timeline.setToken(accessToken.getAccessToken());
minTime = timeline.getUserTimelineByName(comboBox.getSelectedItem().toString()).getStatuses().get(0).getCreatedAt();
Timer timer=new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
try
{
Timeline timeline=new Timeline();
timeline.setToken(accessToken.getAccessToken());
Status status=timeline.getUserTimelineByName(comboBox.getSelectedItem().toString()).getStatuses().get(0);
if(status.getCreatedAt().after(minTime))//如果最新微博的发表时间在之前相对时间的后面
{
minTime=status.getCreatedAt();
System.out.println("你关注的用户更新微博啦");
Comments comments=new Comments();
comments.setToken(accessToken.getAccessToken());
comments.createComment("哈哈,我来评论啦!", status.getId());
JOptionPane.showMessageDialog(null, "恭喜,抢沙发成功!");
}
}
catch (WeiboException e)
{
e.printStackTrace();
}
}
}, 0, 5000);//设置5秒钟刷新一次
}
catch (WeiboException e)
{
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页