-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitter.java
More file actions
73 lines (61 loc) · 2.49 KB
/
Twitter.java
File metadata and controls
73 lines (61 loc) · 2.49 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
import sun.misc.BASE64Encoder;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.net.URLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Twitter {
public static void main(String[] args) throws Exception {
if (args.length < 3) {
System.out.println("Twitter <userid> <password> <message>");
System.exit(-1);
}
String serverName = "localhost";
String mydatabase = "test";
String dburl = "jdbc:mysql://" + serverName + ":8889/" + mydatabase;
String username = "root";
String password = "root";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection dbConnection = DriverManager.getConnection(dburl, username, password);
String query = "select id, tip, nexttweet from vimtricks order by nexttweet limit 1";
Statement stmt = dbConnection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
int id = rs.getInt(1);
String tip = rs.getString(2);
int nexttweet = rs.getInt(3);
System.out.println("id=" + id);
System.out.println("tip=" + tip);
System.out.println("nexttweet=" + nexttweet);
System.out.println("---------------");
}
URL url = new URL("https://twitter.com/statuses/update.xml");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
String authorization = args[0] + ":" + args[1];
BASE64Encoder encoder = new BASE64Encoder();
String encoded = new String
(encoder.encodeBuffer(authorization.getBytes())).trim();
connection.setRequestProperty("Authorization", "Basic " + encoded);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("status=" + URLEncoder.encode(args[2], "UTF-8"));
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String response;
while ((response = in.readLine()) != null) {
System.out.println(response);
}
in.close();
}
}