Created
September 7, 2008 22:08
-
-
Save koduki/9320 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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