OSDN Git Service

r64のリファクタリング.
authoryuki <yuki@c066991c-cf13-ec4a-a49a-846e61667af5>
Sat, 2 May 2009 11:07:46 +0000 (11:07 +0000)
committeryuki <yuki@c066991c-cf13-ec4a-a49a-846e61667af5>
Sat, 2 May 2009 11:07:46 +0000 (11:07 +0000)
git-svn-id: http://192.168.11.7/svn/saccubus/trunk@19 c066991c-cf13-ec4a-a49a-846e61667af5

frontend/src/saccubus/net/NicoClient.java

index c21d539..b3a3ef9 100644 (file)
@@ -38,16 +38,34 @@ import saccubus.util.FileUtil;
  * @version 1.0
  */
 public class NicoClient {
-       private final String User;
+       private String Cookie = null;
 
-       private final String Pass;
+       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");
 
+       private final String User;
+       private final String Pass;
        private boolean Logged_in = false;
-
        private final ConvertStopFlag StopFlag;
-
        private final Proxy ConProxy;
 
+       private String VideoTitle = null;
+       private int VideoLength = -1;
+
+       private byte[] buf = new byte[1024 * 1024];
+
+       private String VideoUrl = null;
+       private String UserID = null;
+       private String ThreadID = null;
+       private String MsgUrl = null;
+
+       private String WayBackKey = "0";
+       private String WayBackTime = "0";
+       private final static String WAYBACKKEY_STR = "waybackkey=";
+
        public NicoClient(final String user, final String pass,
                        final ConvertStopFlag flag, final String proxy, final int proxy_port) {
                User = user;
@@ -64,75 +82,159 @@ public class NicoClient {
                StopFlag = flag;
        }
 
-       private String Cookie = null;
+       public String getBackCommentFromLength(String def) {
+               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";
+               }
+       }
 
-       private boolean login() {
+       public File getComment(final File file, final JLabel status,
+                       String back_comment) {
+               System.out.print("Downloading comment size:" + back_comment + "...");
                try {
-                       HttpURLConnection con = (HttpsURLConnection) (new URL(
-                                       "https://secure.nicovideo.jp/secure/login?site=niconico"))
+                       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(MsgUrl))
                                        .openConnection(ConProxy);
-                       /* \8fo\97Í\82Ì\82Ý */
                        con.setDoOutput(true);
-                       HttpURLConnection.setFollowRedirects(false);
-                       con.setInstanceFollowRedirects(false);
+                       con.setDoInput(true);
                        con.setRequestMethod("POST");
+                       con.addRequestProperty("Cookie", Cookie);
                        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());
+                       String req = "<thread user_id=\"" + UserID + "\" when=\""
+                                       + WayBackTime + "\" waybackkey=\"" + WayBackKey
+                                       + "\" res_from=\"-" + back_comment
+                                       + "\" version=\"20061206\" thread=\"" + ThreadID + "\" />";
+                       os.write(req.getBytes());
                        os.flush();
                        os.close();
-                       int code = con.getResponseCode();
-                       if (code < 200 || code >= 400) {
-                               System.out.println("Can't login:" + con.getResponseMessage());
-                               return false;
+                       if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
+                               System.out.println("ng.\nCan't download comment:" + MsgUrl);
+                               return null;
                        }
-                       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(";"));
-                                       }
+                       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;
+                       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;
                                }
-                               i++;
                        }
+                       System.out.println("ok.");
+                       is.close();
+                       fos.flush();
+                       fos.close();
                        con.disconnect();
-                       if (Cookie == null) {
-                               System.out.println("Can't login: cannot set cookie.");
-                               return false;
-                       }
-                       System.out.println("Logged in.");
+                       return file;
                } catch (IOException ex) {
                        ex.printStackTrace();
-                       return false;
                }
-               return true;
-       }
 
-       public boolean isLoggedIn() {
-               return Logged_in;
+               return null;
        }
 
-       public String getVideoTitle() {
-               return VideoTitle;
+       public File getVideo(final File file, final JLabel status) {
+               if (VideoUrl == 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(VideoUrl))
+                                       .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:" + VideoUrl);
+                               return null;
+                       }
+                       InputStream is = con.getInputStream();
+                       OutputStream os = new FileOutputStream(file);
+                       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;
+                       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();
+                                       file.delete();
+                                       return null;
+                               }
+                       }
+                       System.out.println("ok.");
+                       is.close();
+                       os.flush();
+                       os.close();
+                       con.disconnect();
+                       return file;
+               } catch (FileNotFoundException ex) {
+                       ex.printStackTrace();
+               } catch (IOException ex) {
+                       ex.printStackTrace();
+               }
+               return null;
        }
 
-       private String VideoTitle = null;
-
-       private int VideoLength = -1;
-
-       private static final String TITLE_PARSE_STR_START = "<title>";
-
        public boolean getVideoHistoryAndTitle(String tag) {
                String url = "http://www.nicovideo.jp/watch/" + tag;
                System.out.print("Getting video history...");
@@ -258,167 +360,10 @@ public class NicoClient {
                return true;
        }
 
-       private byte[] buf = new byte[1024 * 1024];
-
-       private String VideoUrl = null;
-
-       public File getVideo(final File file, final JLabel status) {
-               if (VideoUrl == 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(VideoUrl))
-                                       .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:" + VideoUrl);
-                               return null;
-                       }
-                       InputStream is = con.getInputStream();
-                       OutputStream os = new FileOutputStream(file);
-                       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;
-                       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();
-                                       file.delete();
-                                       return null;
-                               }
-                       }
-                       System.out.println("ok.");
-                       is.close();
-                       os.flush();
-                       os.close();
-                       con.disconnect();
-                       return file;
-               } catch (FileNotFoundException ex) {
-                       ex.printStackTrace();
-               } catch (IOException ex) {
-                       ex.printStackTrace();
-               }
-               return null;
-       }
-
-       private String UserID = null;
-
-       private String ThreadID = null;
-
-       private String MsgUrl = null;
-
-       public File getComment(final File file, final JLabel status,
-                       String back_comment) {
-               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(MsgUrl))
-                                       .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 req = "<thread user_id=\"" + UserID + "\" when=\""
-                                       + WayBackTime + "\" waybackkey=\"" + WayBackKey
-                                       + "\" res_from=\"-" + back_comment
-                                       + "\" version=\"20061206\" thread=\"" + ThreadID + "\" />";
-                       os.write(req.getBytes());
-                       os.flush();
-                       os.close();
-                       if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                               System.out.println("ng.\nCan't download comment:" + MsgUrl);
-                               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;
-                       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 String getVideoTitle() {
+               return VideoTitle;
        }
 
-       private String WayBackKey = "0";
-
-       private String WayBackTime = "0";
-
-       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");
-
-       private final static String WAYBACKKEY_STR = "waybackkey=";
-
        private boolean getWayBackKey(String time) {
                System.out.print("Setting wayback time...");
                Date date = null;
@@ -499,17 +444,60 @@ public class NicoClient {
                return true;
        }
 
-       public String getBackCommentFromLength(String def) {
-               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 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;
        }
 }