Skip to content

Instantly share code, notes, and snippets.

@koduki
Created September 7, 2008 22:08
Show Gist options
  • Save koduki/9320 to your computer and use it in GitHub Desktop.
Save koduki/9320 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.meterware.httpunit.*;
public class TwitterAPI {
private WebConversation wc;
public TwitterAPI() throws Exception {
HttpUnitOptions.setScriptingEnabled(false);
this.wc = new WebConversation();
}
private void login(String id, String password) throws Exception {
WebResponse res = wc.getResponse("https://twitter.com/");
WebForm form = res.getFormWithID("signin");
form.setParameter("session[username_or_email]", id);
form.setParameter("session[password]", password);
form.setParameter("remember_me", "1");
form.submit();
}
private String[] getMessages() throws Exception {
WebResponse res = wc.getResponse("http://twitter.com/home");
List<String> list = new ArrayList<String>();
for(HTMLElement element : res.getElementsByTagName("span")){
if (element.getClassName().equals("entry-content")) list.add(element.getText());
}
return list.toArray(new String[0]);
}
private void update(String msg) throws Exception {
WebResponse res = wc.getResponse("http://twitter.com/home");
WebForm form = res.getFormWithID("doingForm");
form.setParameter("status", msg);
form.submit();
}
private void set_icon() throws Exception {
WebResponse res = wc.getResponse("http://twitter.com/account/picture");
WebForm form = res.getForms()[0];
form.setParameter("profile_image[uploaded_data]", new File("icon.png"));
SubmitButton commit = form.getSubmitButton("commit");
form.submit(commit);
}
public static void main(String[] args) throws Exception {
TwitterAPI api = new TwitterAPI();
api.login("ID", "パスワード");
System.out.println("logined");
api.update("投稿テスト");
for(String msg:api.getMessages()) System.out.println(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment