mixiにログインするプログラムを作ってみたが古くなってしまい、足跡を取得できなくなっていたので、書き直し。とは言っても正規表現の部分を変えただけ。他のエントリも古くなっているので、変えておこうか
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 Pattern pattern; 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 = ""; String patternText = "<span class=\"date\">([^<]+)</span>"; patternText += "<span class=\"name\"><a href=\"(show_friend.pl\\?id=\\d+&route_trace=\\d+)\">([^<]+)</a></span>"; pattern = Pattern.compile(patternText); } 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); } 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; } }