OSDN Git Service

インタフェースを再考
[coroid/inqubus.git] / frontend / src / saccubus / net / NicoClient.java
index bc04f94..b37a893 100644 (file)
 package saccubus.net;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.URL;
-import java.net.HttpURLConnection;
-import java.net.InetSocketAddress;
-import java.net.Proxy;
-import java.net.URLEncoder;
-import java.net.URLDecoder;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import javax.net.ssl.HttpsURLConnection;
-import saccubus.ConvertStopFlag;
-import saccubus.util.FileUtil;
-import yukihane.Util;
-import static saccubus.net.VideoInfo.OfficialOption;
 
 /**
- * <p>
- * \83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·
- * </p>
- * 
- * <p>
- * \90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91
- * </p>
- * 
- * <p>
- * \92\98\8dì\8c : Copyright (c) 2007 PSI
- * </p>
- * 
- * <p>
- * \89ï\8eÐ\96¼:
- * </p>
- * 
- * @author \96¢\93ü\97Í
- * @version 1.0
+ * いんきゅばすがニコニコ動画にアクセスするためのクラスが実装すべきインタフェース.
+ * ログイン処理はコンストラクタ内で実行しておき、各メソッドは同一セッションで実行される必要があります.
+ * @author yuki
  */
-public class NicoClient {
-       private String Cookie = null;
+public interface NicoClient {
 
-       private static final String TITLE_PARSE_STR_START = "<title>";
-       private final static DateFormat DateFmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
-       private final static DateFormat DateFmt2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+    /** @return ログインが正常に完了していればtrue. */
+    boolean isLoggedIn();
 
-       private final String User;
-       private final String Pass;
-       private boolean Logged_in = false;
-       private final ConvertStopFlag StopFlag;
-       private final Proxy ConProxy;
+    VideoInfo getVideoInfo(String videoId, String time) throws IOException;
 
-       private String WayBackKey = "0";
-       private String WayBackTime = "0";
-       private final static String WAYBACKKEY_STR = "waybackkey=";
+    File getVideo(VideoInfo videoInfo, File file, TextProgressListener listener);
 
-       public NicoClient(final String user, final String pass,
-                       final ConvertStopFlag flag, final String proxy, final int proxy_port) {
-               User = user;
-               Pass = pass;
-               if (proxy != null && proxy.length() > 0 && proxy_port >= 0
-                               && proxy_port <= 65535) {
-                       ConProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy,
-                                       proxy_port));
-               } else {
-                       ConProxy = Proxy.NO_PROXY;
-               }
-               // \83\8d\83O\83C\83\93
-               Logged_in = login();
-               StopFlag = flag;
-       }
+    File getComment(VideoInfo videoInfo, File file, TextProgressListener listener, String com);
 
-       public String getBackCommentFromLength(VideoInfo vi, String def) {
-        final int videoLength  = vi.getVideoLength();
-               if (videoLength < 0) {
-                       return def;
-               } else if (videoLength >= 0 && videoLength < 60) {
-                       return "100";
-               } else if (videoLength >= 60 && videoLength < 300) {
-                       return "250";
-               } else if (videoLength >= 300 && videoLength < 600) {
-                       return "500";
-               } else {
-                       return "1000";
-               }
-       }
-
-    public File getComment(VideoInfo vi, final File file, final TextProgressListener status, String back_comment) {
-        return downloadComment(back_comment, file, vi, status, false);
-    }
-
-    /**
-     * \93\8a\8de\8eÒ\83R\83\81\83\93\83g\82ð\83_\83E\83\93\83\8d\81[\83h\82·\82é.
-     * @param vi \83r\83f\83I\8fî\95ñ.
-     * @param file \83_\83E\83\93\83\8d\81[\83h\90æ\83t\83@\83C\83\8b.
-     * @param status \90i\92»\92Ê\92m\83\8a\83X\83i.
-     * @return \83_\83E\83\93\83\8d\81[\83h\82³\82ê\82½\83t\83@\83C\83\8b\83_\83E\83\93\83\8d\81[\83h\82Å\82«\82È\82¯\82ê\82Înull.
-     */
-    public File getTcomment(VideoInfo vi, final File file, final TextProgressListener status) {
-        return downloadComment("500", file, vi, status, true);
-    }
-
-    private File downloadComment(String back_comment, final File file, VideoInfo vi, final TextProgressListener status,
-            boolean isTcomm) throws NumberFormatException {
-        System.out.print("Downloading comment size:" + back_comment + "...");
-        try {
-                       if (file.canRead()) { // \83t\83@\83C\83\8b\82ª\82·\82Å\82É\91\8dÝ\82·\82é\82È\82ç\8dí\8f\9c\82·\82é\81B
-                file.delete();
-            }
-            OutputStream fos = new FileOutputStream(file);
-            HttpURLConnection con = (HttpURLConnection) (new URL(vi.getMsgUrl()))
-                                       .openConnection(ConProxy);
-            con.setDoOutput(true);
-            con.setDoInput(true);
-            con.setRequestMethod("POST");
-            con.addRequestProperty("Cookie", Cookie);
-            con.addRequestProperty("Connection", "close");
-            con.connect();
-            OutputStream os = con.getOutputStream();
-            String tcommStr = (isTcomm) ? "fork=\"1\" " : "";
-            String official = "";
-            OfficialOption oo = vi.getOfficialOption();
-            if (oo != null) {
-                official = "force_184=\"" + oo.getForce184() + "\" threadkey=\"" + oo.getThreadKey() + "\" ";
-            }
-            String req = "<thread user_id=\"" + vi.getUserId() + "\" when=\"" + WayBackTime + "\" waybackkey=\""
-                    + WayBackKey + "\" res_from=\"-" + back_comment + "\" version=\"20061206\" thread=\"" + vi.
-                    getThreadId() + "\" " + tcommStr + official + "/>";
-            os.write(req.getBytes());
-            os.flush();
-            os.close();
-            if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                System.out.println("ng.\nCan't download comment:" + vi.getMsgUrl());
-                return null;
-            }
-            InputStream is = con.getInputStream();
-            int read = 0;
-            int max_size = 0;
-            String content_length_str = con.getHeaderField("Content-length");
-            if (content_length_str != null && !content_length_str.equals("")) {
-                max_size = Integer.parseInt(content_length_str);
-            }
-            int size = 0;
-            final byte[] buf = new byte[1024 * 1024];
-            while ((read = is.read(buf, 0, buf.length)) > 0) {
-                fos.write(buf, 0, read);
-                size += read;
-                if (max_size != 0) {
-                    String per = Double.toString((((double) size) * 100) / max_size);
-                    per = per.substring(0, Math.min(per.indexOf(".") + 3, per.length()));
-                    status.setText("\83R\83\81\83\93\83g\83_\83E\83\93\83\8d\81[\83h\81F" + per + "\83p\81[\83Z\83\93\83g\8a®\97¹");
-                } else {
-                    status.setText("\83R\83\81\83\93\83g\83_\83E\83\93\83\8d\81[\83h\92\86\81F" + Integer.toString(size >> 10) + "kbytes\83_\83E\83\93\83\8d\81[\83h");
-                }
-                if (StopFlag.needStop()) {
-                    System.out.println("Stopped.");
-                    is.close();
-                    os.flush();
-                    os.close();
-                    con.disconnect();
-                    file.delete();
-                    return null;
-                }
-            }
-            System.out.println("ok.");
-            is.close();
-            fos.flush();
-            fos.close();
-            con.disconnect();
-            return file;
-        } catch (IOException ex) {
-            ex.printStackTrace();
-        }
-        return null;
-    }
-
-       public File getVideo(VideoInfo vi, final File file, final TextProgressListener status) {
-               if (vi.getVideoUrl() == null) {
-                       System.out.println("Video url is not detected.");
-                       return null;
-               }
-               try {
-//                     if (file.canRead()) { // \83t\83@\83C\83\8b\82ª\82·\82Å\82É\91\8dÝ\82·\82é\82È\82ç\8dí\8f\9c\82·\82é\81B
-//                             file.delete();
-//                     }
-                       HttpURLConnection con = (HttpURLConnection) (new URL(vi.getVideoUrl()))
-                                       .openConnection(ConProxy);
-                       /* \8fo\97Í\82Ì\82Ý */
-                       con.setDoInput(true);
-                       con.setRequestMethod("GET");
-                       con.addRequestProperty("Cookie", Cookie);
-                       con.connect();
-                       if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                               System.out.println("Can't get video:" + vi.getVideoUrl());
-                               return null;
-                       }
-            final String extension = Util.getExtention(con.getContentType());
-            File outFile = appendExtension(file, extension);
-                       InputStream is = con.getInputStream();
-                       OutputStream os = new FileOutputStream(outFile);
-                       String content_length_str = con.getHeaderField("Content-length");
-                       int max_size = 0;
-                       if (content_length_str != null && !content_length_str.equals("")) {
-                               max_size = Integer.parseInt(content_length_str);
-                       }
-                       int size = 0;
-                       System.out.print("Downloading video...");
-                       int read = 0;
-            final byte[] buf = new byte[1024 * 1024];
-                       while ((read = is.read(buf, 0, buf.length)) > 0) {
-                               size += read;
-                               os.write(buf, 0, read);
-                               if (max_size != 0) {
-                                       String per = Double.toString((((double) size) * 100)
-                                                       / max_size);
-                                       per = per.substring(0, Math.min(per.indexOf(".") + 3, per
-                                                       .length()));
-                                       status.setText("\93®\89æ\83_\83E\83\93\83\8d\81[\83h\81F" + per + "\83p\81[\83Z\83\93\83g\8a®\97¹");
-                               } else {
-                                       status.setText("\93®\89æ\83_\83E\83\93\83\8d\81[\83h\92\86\81F" + Integer.toString(size >> 10)
-                                                       + "kbytes\83_\83E\83\93\83\8d\81[\83h");
-                               }
-                               if (StopFlag.needStop()) {
-                                       System.out.println("Stopped.");
-                                       is.close();
-                                       os.flush();
-                                       os.close();
-                                       con.disconnect();
-                                       outFile.delete();
-                                       return null;
-                               }
-                       }
-                       System.out.println("ok.");
-                       is.close();
-                       os.flush();
-                       os.close();
-                       con.disconnect();
-                       return outFile;
-               } catch (FileNotFoundException ex) {
-                       ex.printStackTrace();
-               } catch (IOException ex) {
-                       ex.printStackTrace();
-               }
-               return null;
-       }
-
-    /** @return \83r\83f\83I\96¼ */
-       public String getVideoHistoryAndTitle(String tag) throws IOException {
-               String url = "http://www.nicovideo.jp/watch/" + tag;
-               System.out.print("Getting video history...");
-               String new_cookie = null;
-        String videoTitle = null;
-
-        HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection(ConProxy);
-        /* \83\8a\83N\83G\83X\83g\82Ì\90Ý\92è */
-        con.setRequestMethod("GET");
-        con.addRequestProperty("Cookie", Cookie);
-        con.addRequestProperty("Connection", "close");
-        con.connect();
-        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-            throw new IOException("Can't getVideoHistory:" + url);
-        }
-        int i = 1;
-        String key;
-        String value;
-        while ((key = con.getHeaderFieldKey(i)) != null) {
-            if (key.equalsIgnoreCase("Set-Cookie")) {
-                value = con.getHeaderField(i);
-                if (value != null) {
-                    new_cookie = value.substring(0, value.indexOf(";"));
-                }
-            }
-            i++;
-        }
-        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
-        String ret;
-        int index = -1;
-        while ((ret = br.readLine()) != null && index < 0) {
-            if ((index = ret.indexOf(TITLE_PARSE_STR_START)) >= 0) {
-                videoTitle = ret.substring(index + TITLE_PARSE_STR_START.length(), ret.indexOf("\81]", index));
-                videoTitle = FileUtil.safeFileName(videoTitle);
-            }
-        }
-        br.close();
-        con.disconnect();
-        if (new_cookie == null) {
-            System.out.println("Can't getVideoHistory: cannot get cookie.");
-            return null;
-        }
-        System.out.println("ok.");
-        Cookie += "; ";
-        Cookie += new_cookie;
-
-        return videoTitle;
-       }
-
-    public VideoInfo getVideoInfo(String tag, String time) throws IOException {
-        final String videoTitle = getVideoHistoryAndTitle(tag);
-
-        String url = "http://flapi.nicovideo.jp/api/getflv/" + tag;
-        if (tag.startsWith("nm")) {
-            url += "?as3=1";
-        }
-        System.out.print("Getting video informations...");
-        Map<String,String> res = new NicoApiRequest(url).get();
-        String threadId = res.get("thread_id");
-        String videoUrl = res.get("url");
-        String msgUrl = res.get("ms");
-        String userId = res.get("user_id");
-        int videoLength = -1;
-        String videoLengthStr = res.get("l");
-        try{
-            videoLength = Integer.parseInt(videoLengthStr);
-        }catch(NumberFormatException ex){}
-
-        OfficialOption oo = null;
-        if ("1".equals(res.get("needs_key"))) {
-            oo = getOfficialOption(threadId);
-        }
-
-        VideoInfo vi = new VideoInfo(videoTitle, threadId, videoUrl, msgUrl, userId, videoLength, oo);
-        System.out.println("ok.");
-
-        if (!(time == null || time.equals("")) && !getWayBackKey(vi, time)) { // WayBackKey
-            throw new IOException();
-        }
-        return vi;
-    }
-
-    private OfficialOption getOfficialOption(String threadId) throws IOException {
-        String url = "http://flapi.nicovideo.jp/api/getthreadkey?thread="+threadId;
-        Map<String,String> map = new NicoApiRequest(url).get();
-        return new OfficialOption(map.get("threadkey"), map.get("force_184"));
-    }
-
-    private boolean getWayBackKey(VideoInfo vi, String time) {
-               System.out.print("Setting wayback time...");
-               Date date = null;
-               String waybacktime = "0";
-               try {
-                       date = DateFmt.parse(time);
-               } catch (ParseException ex2) {
-                       date = null;
-               }
-               if (date == null) {
-                       try {
-                               date = DateFmt2.parse(time);
-                       } catch (ParseException ex3) {
-                               date = null;
-                       }
-               }
-               if (date != null) {
-                       waybacktime = Long.toString(date.getTime() / 1000);
-                       System.out.println("ok.(" + date.toString() + "):" + waybacktime);
-               } else {
-                       try {
-                               long tmp_time = Long.parseLong(time);
-                               waybacktime = Long.toString(tmp_time);
-                               date = new Date(tmp_time * 1000);
-                               System.out.println("ok.(" + date.toString() + "):"
-                                               + waybacktime);
-                       } catch (NumberFormatException ex4) {
-                               System.out.println("ng.");
-                               System.out.println("Cannot parse wayback time.");
-                               return false;
-                       }
-               }
-               System.out.print("Getting wayback key...");
-               String url = "http://flapi.nicovideo.jp/api/getwaybackkey?thread="
-                               + vi.getThreadId();
-               String ret = "";
-               try {
-                       HttpURLConnection con = (HttpURLConnection) (new URL(url))
-                                       .openConnection(ConProxy);
-                       /* \83\8a\83N\83G\83X\83g\82Ì\90Ý\92è */
-                       con.setRequestMethod("GET");
-                       con.addRequestProperty("Cookie", Cookie);
-                       con.addRequestProperty("Connection", "close");
-                       con.setDoInput(true);
-                       con.connect();
-                       if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                               System.out.println("Can't get WayBackKey:" + url);
-                               return false;
-                       }
-                       /* \96ß\82è\92l\82Ì\8eæ\93¾ */
-                       BufferedReader br = new BufferedReader(new InputStreamReader(con
-                                       .getInputStream()));
-                       ret = br.readLine();
-                       br.close();
-                       con.disconnect();
-               } catch (IOException ex1) {
-                       System.out.println("ng.");
-                       ex1.printStackTrace();
-                       return false;
-               }
-               int idx = 0;
-               if ((idx = ret.indexOf(WAYBACKKEY_STR)) < 0) {
-                       System.out.println("ng.");
-                       System.out.println("Cannot find wayback key from response.");
-                       return false;
-               }
-               int end_idx = Math.max(ret.length(), ret.indexOf("&"));
-               String waybackkey = ret.substring(idx + WAYBACKKEY_STR.length(),
-                               end_idx);
-               if (waybackkey == null || waybackkey.equals("")) {
-                       System.out.println("ng.");
-                       System.out.println("Cannot get wayback key.");
-                       return false;
-               }
-               System.out.println("ok. key:" + waybackkey);
-               WayBackTime = waybacktime;
-               WayBackKey = waybackkey;
-               return true;
-       }
-
-       public boolean isLoggedIn() {
-               return Logged_in;
-       }
-
-       private boolean login() {
-               try {
-                       HttpURLConnection con = (HttpsURLConnection) (new URL(
-                                       "https://secure.nicovideo.jp/secure/login?site=niconico"))
-                                       .openConnection(ConProxy);
-                       /* \8fo\97Í\82Ì\82Ý */
-                       con.setDoOutput(true);
-                       HttpURLConnection.setFollowRedirects(false);
-                       con.setInstanceFollowRedirects(false);
-                       con.setRequestMethod("POST");
-                       con.addRequestProperty("Connection", "close");
-                       con.connect();
-                       StringBuffer sb = new StringBuffer(4096);
-                       sb.append("next_url=&");
-                       sb.append("mail=");
-                       sb.append(URLEncoder.encode(User, "Shift_JIS"));
-                       sb.append("&password=");
-                       sb.append(URLEncoder.encode(Pass, "Shift_JIS"));
-                       sb.append("&submit.x=103&submit.y=16");
-                       OutputStream os = con.getOutputStream();
-                       os.write(sb.substring(0).getBytes());
-                       os.flush();
-                       os.close();
-                       int code = con.getResponseCode();
-                       if (code < 200 || code >= 400) {
-                               System.out.println("Can't login:" + con.getResponseMessage());
-                               return false;
-                       }
-                       int i = 1;
-                       String key;
-                       String value;
-                       while ((key = con.getHeaderFieldKey(i)) != null) {
-                               if (key.equalsIgnoreCase("Set-Cookie")) {
-                                       value = con.getHeaderField(i);
-                                       if (value != null) {
-                                               Cookie = value.substring(0, value.indexOf(";"));
-                                       }
-                               }
-                               i++;
-                       }
-                       con.disconnect();
-                       if (Cookie == null) {
-                               System.out.println("Can't login: cannot set cookie.");
-                               return false;
-                       }
-                       System.out.println("Logged in.");
-               } catch (IOException ex) {
-                       ex.printStackTrace();
-                       return false;
-               }
-               return true;
-       }
-
-    private File appendExtension(File file, String extension) {
-        final String e = "." + extension;
-        final String defExt = ".flv";
-        String path = file.getPath();
-        if (path.endsWith(e)) {
-            return file;
-        } else if (path.endsWith(defExt)) {
-            path = path.substring(0, path.length() - defExt.length());
-        }
-        return new File(path + e);
-    }
-
-    private class NicoApiRequest {
-
-        private final String url;
-
-        private NicoApiRequest(String url) {
-            this.url = url;
-        }
-
-        private Map<String,String> get() throws IOException {
-            Map<String, String> map = new HashMap<String, String>();
-            System.out.print("Getting video informations...");
-            HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection(ConProxy);
-            /* \83\8a\83N\83G\83X\83g\82Ì\90Ý\92è */
-            con.setRequestMethod("GET");
-            con.addRequestProperty("Cookie", Cookie);
-            con.addRequestProperty("Connection", "close");
-            con.connect();
-            if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                throw new IOException("Can't getVideoInfo:" + url);
-            }
-            /* \96ß\82è\92l\82Ì\8eæ\93¾ */
-            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
-            String ret = br.readLine();
-            br.close();
-            con.disconnect();
-            ret = URLDecoder.decode(ret, "Shift_JIS");
-            String[] array = ret.split("&");
-            int cnt = 0;
-            for (int i = 0; i < array.length; i++) {
-                int idx = array[i].indexOf("=");
-                if (idx < 0) {
-                    continue;
-                }
-                String key = array[i].substring(0, idx);
-                String value = array[i].substring(idx + 1);
-                map.put(key, value);
-            }
-            return map;
-        }
-    }
+    File getTcomment(VideoInfo videoInfo, File file, TextProgressListener listener);
 }