mixiにログインするプログラムを作ってみた。ただログインして終わりじゃつまらないので、足あとを取得してみた。Mixi、AuthenticateException, FootPrintという3つのクラスを作った。Mixiクラスはmixiにログインするメソッドと足あとを取得してくるメソッドを定義していて、AuthenticateExceptionはログインに失敗したり、ログインしてないのに足あとを取得しようとすると発生する例外。FootPrintは足あとの情報を保持するクラスとなっている。これくらいだとJavadocやインテリセンスはなくてもテキストエディタだけで頑張れる
import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; import java.net.URLEncoder; import java.net.MalformedURLException; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Mixi{ private String cookie; private static final String ENCODING = "EUC-JP"; private static final String MIXI_URL = "http://mixi.jp/"; public static void main(String[] args){ try{ Mixi mixi = new Mixi(); mixi.login("+++++++++", "**********"); List<FootPrint> footPrintList = mixi.getFootPrintList(); for(FootPrint footPrint : footPrintList){ System.out.printf("%s %s %s\n", footPrint.getDate(), footPrint.getName(), footPrint.getLink()); } }catch(AuthenticateException e){ System.out.println(e.getMessage()); }catch(IOException e){ System.out.println("通信中にエラーが起こりました: " + e.getMessage()); } } public Mixi(){ cookie = ""; } public void login(String email, String password) throws AuthenticateException, IOException{ BufferedReader reader = null; BufferedWriter writer = null; try{ StringBuilder parameter = new StringBuilder(); parameter.append("email=").append(URLEncoder.encode(email, ENCODING)).append('&'); parameter.append("password=").append(URLEncoder.encode(password, ENCODING)).append('&'); parameter.append("next_url=").append(URLEncoder.encode("./home.pl", ENCODING)); URL url = new URL(MIXI_URL + "login.pl"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); writer.write(parameter.toString()); writer.flush(); Map<String, List<String>> headers = connection.getHeaderFields(); List<String> values = headers.get("Set-Cookie"); if(values != null) for(String value : values) cookie += value + ";"; if(cookie.isEmpty()) throw new AuthenticateException("ログインできませんでした"); reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), ENCODING)); String line; while((line = reader.readLine()) != null) ; reader.close(); writer.close(); }catch(UnsupportedEncodingException e){ throw new RuntimeException(e); }catch(MalformedURLException e){ throw new RuntimeException(e); }catch(IOException e){ try{ if(reader != null) reader.close(); if(writer != null) writer.close(); }catch(IOException ioe){ throw new RuntimeException(ioe); } throw e; } } public List<FootPrint> getFootPrintList() throws AuthenticateException, IOException{ if(cookie.isEmpty()) throw new AuthenticateException("ログインしていません"); List<FootPrint> footPrintList = new ArrayList<FootPrint>(); BufferedReader reader = null; try{ URL url = new URL(MIXI_URL + "show_log.pl"); URLConnection connection = url.openConnection(); connection.setRequestProperty("Cookie", cookie); reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), ENCODING)); StringBuilder html = new StringBuilder(); String line; while((line = reader.readLine()) != null) html.append(line); String patternText = "<span class=\"date\">([^<]+)</span>"; patternText += "<span class=\"name\"><a href=\"(show_friend.pl\\?id=\\d+)\">([^<]+)</a></span>"; Pattern pattern = Pattern.compile(patternText); Matcher matcher = pattern.matcher(html.toString()); while(matcher.find()){ String date = matcher.group(1); String link = matcher.group(2); String name = matcher.group(3); footPrintList.add(new FootPrint(name, date, link)); } }catch(UnsupportedEncodingException e){ throw new RuntimeException(e); }catch(MalformedURLException e){ throw new RuntimeException(e); }catch(IOException e){ try{ if(reader != null) reader.close(); }catch(IOException ioe){ throw new RuntimeException(ioe); } throw e; } return footPrintList; } }
import java.io.IOException; public class AuthenticateException extends IOException{ public AuthenticateException(){ super(); } public AuthenticateException(String message){ super(message); } }
public class FootPrint{ private final String name; private final String date; private final String link; public FootPrint(String name, String date, String link){ this.name = name; this.date = date; this.link = "http://mixi.jp/" + link; } public String getName(){ return name; } public String getDate(){ return date; } public String getLink(){ return link; } }