From 700818005f48ee1a5db41aac34539769bba5abed Mon Sep 17 00:00:00 2001 From: yukihane Date: Wed, 10 Aug 2011 18:02:42 +0900 Subject: [PATCH] =?utf8?q?WayBackKey,=20WayBackTime=E3=82=92NicoCacneImpl?= =?utf8?q?=E3=81=AE=E3=83=95=E3=82=A3=E3=83=BC=E3=83=AB=E3=83=89=E3=81=8B?= =?utf8?q?=E3=82=89=E5=89=8A=E9=99=A4=E3=81=97=E3=82=B3=E3=83=A1=E3=83=B3?= =?utf8?q?=E3=83=88=E7=94=A8=E6=83=85=E5=A0=B1=E3=82=AF=E3=83=A9=E3=82=B9?= =?utf8?q?=E3=81=AB=E7=A7=BB=E3=81=99.=20(cherry=20picked=20from=20commit?= =?utf8?q?=2070b1d4b7a65e2fe7465c1e9bd66bfbbf6edc7f4e)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Conflicts: frontend/src/saccubus/converter/filegetter/WebFileInstanciator.java --- .../converter/filegetter/CommentFileWebGetter.java | 11 +- .../converter/filegetter/WebFileInstanciator.java | 8 +- frontend/src/saccubus/net/CommentInfo.java | 29 ++ frontend/src/saccubus/net/NicoClient.java | 6 +- frontend/src/saccubus/net/NicoClientImpl.java | 494 ++++++++++----------- 5 files changed, 293 insertions(+), 255 deletions(-) create mode 100644 frontend/src/saccubus/net/CommentInfo.java diff --git a/frontend/src/saccubus/converter/filegetter/CommentFileWebGetter.java b/frontend/src/saccubus/converter/filegetter/CommentFileWebGetter.java index 11ee32b..697d599 100644 --- a/frontend/src/saccubus/converter/filegetter/CommentFileWebGetter.java +++ b/frontend/src/saccubus/converter/filegetter/CommentFileWebGetter.java @@ -6,6 +6,7 @@ package saccubus.converter.filegetter; import java.io.File; import java.io.IOException; +import saccubus.net.CommentInfo; import saccubus.net.NicoClient; import saccubus.net.TextProgressListener; import saccubus.net.VideoInfo; @@ -18,11 +19,13 @@ public class CommentFileWebGetter extends TcommFileWebGetter { private final boolean autoCommentNum; private final int backComment; + private final CommentInfo commentInfo; - CommentFileWebGetter(NicoClient client, VideoInfo vi, boolean autoCommentNum, int backComment) { + CommentFileWebGetter(NicoClient client, VideoInfo vi, CommentInfo ci, boolean autoCommentNum, int backComment) { super(client, vi); this.autoCommentNum = autoCommentNum; this.backComment = backComment; + this.commentInfo = ci; } @Override @@ -32,7 +35,7 @@ public class CommentFileWebGetter extends TcommFileWebGetter { com = getBackCommentFromLength(getVideoInfo(), com); } - File res = getClient().getComment(getVideoInfo(), file, listener, com); + File res = getClient().getComment(getVideoInfo(), getCommentInfo(), file, listener, com); if (res == null) { throw new IOException("コメントファイルのダウンロードに失敗しました。"); } @@ -53,4 +56,8 @@ public class CommentFileWebGetter extends TcommFileWebGetter { return "1000"; } } + + private CommentInfo getCommentInfo() { + return commentInfo; + } } diff --git a/frontend/src/saccubus/converter/filegetter/WebFileInstanciator.java b/frontend/src/saccubus/converter/filegetter/WebFileInstanciator.java index 35083db..bcda523 100644 --- a/frontend/src/saccubus/converter/filegetter/WebFileInstanciator.java +++ b/frontend/src/saccubus/converter/filegetter/WebFileInstanciator.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.text.Normalizer; import saccubus.ConvertStopFlag; import saccubus.converter.profile.Proxy; +import saccubus.net.CommentInfo; import saccubus.net.NicoClient; import saccubus.net.NicoClientImpl; import saccubus.net.VideoInfo; @@ -17,6 +18,7 @@ public class WebFileInstanciator extends FileInstanciator { private final NicoClient client; private final VideoInfo videoInfo; + private final CommentInfo commentInfo; WebFileInstanciator( ConvertStopFlag stopFlag, @@ -49,7 +51,9 @@ public class WebFileInstanciator extends FileInstanciator { } try { - videoInfo = client.getVideoInfo(tag, time); + videoInfo = client.getVideoInfo(tag); + commentInfo = client.getWayBackKey(videoInfo, time); + } catch (IOException ex) { throw new IOException(tag + "の情報の取得に失敗", ex); } @@ -59,7 +63,7 @@ public class WebFileInstanciator extends FileInstanciator { } if (commentType.isDoanload()) { - setCommentFileGetter(new CommentFileWebGetter(client, videoInfo, commentType.isAutoCommentNum(), + setCommentFileGetter(new CommentFileWebGetter(client, videoInfo, commentInfo, commentType.isAutoCommentNum(), commentType.getBackComment())); } diff --git a/frontend/src/saccubus/net/CommentInfo.java b/frontend/src/saccubus/net/CommentInfo.java new file mode 100644 index 0000000..a9f3cbb --- /dev/null +++ b/frontend/src/saccubus/net/CommentInfo.java @@ -0,0 +1,29 @@ +package saccubus.net; + +/** + * + * @author yuki + */ +public class CommentInfo { + public static final CommentInfo DEFAULT = new CommentInfo(); + + private final String wayBackKey; + private final String wayBackTime; + + private CommentInfo() { + this("0", "0"); + } + + public CommentInfo(String wayBackKey, String wayBackTime) { + this.wayBackKey = wayBackKey; + this.wayBackTime = wayBackTime; + } + + public String getWayBackKey() { + return wayBackKey; + } + + public String getWayBackTime() { + return wayBackTime; + } +} diff --git a/frontend/src/saccubus/net/NicoClient.java b/frontend/src/saccubus/net/NicoClient.java index b37a893..1fc2991 100644 --- a/frontend/src/saccubus/net/NicoClient.java +++ b/frontend/src/saccubus/net/NicoClient.java @@ -13,11 +13,13 @@ public interface NicoClient { /** @return ログインが正常に完了していればtrue. */ boolean isLoggedIn(); - VideoInfo getVideoInfo(String videoId, String time) throws IOException; + VideoInfo getVideoInfo(String videoId) throws IOException; + + CommentInfo getWayBackKey(VideoInfo vi, String time) throws IOException; File getVideo(VideoInfo videoInfo, File file, TextProgressListener listener); - File getComment(VideoInfo videoInfo, File file, TextProgressListener listener, String com); + File getComment(VideoInfo videoInfo, CommentInfo commentInfo, File file, TextProgressListener listener, String com); File getTcomment(VideoInfo videoInfo, File file, TextProgressListener listener); } diff --git a/frontend/src/saccubus/net/NicoClientImpl.java b/frontend/src/saccubus/net/NicoClientImpl.java index 03d6590..ff4e99f 100644 --- a/frontend/src/saccubus/net/NicoClientImpl.java +++ b/frontend/src/saccubus/net/NicoClientImpl.java @@ -21,6 +21,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HttpsURLConnection; +import org.apache.commons.lang.StringUtils; import saccubus.ConvertStopFlag; import saccubus.util.FileUtil; import yukihane.Util; @@ -47,41 +48,34 @@ import static saccubus.net.VideoInfo.OfficialOption; * @version 1.0 */ public class NicoClientImpl implements NicoClient { - private String Cookie = null; - private static final String TITLE_PARSE_STR_START = ""; - 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 String Cookie = null; + private final String User; + private final String Pass; + private boolean Logged_in = false; + private final ConvertStopFlag StopFlag; + private final Proxy ConProxy; + private final static String WAYBACKKEY_STR = "waybackkey="; - private final String User; - private final String Pass; - private boolean Logged_in = false; - private final ConvertStopFlag StopFlag; - private final Proxy ConProxy; - - private String WayBackKey = "0"; - private String WayBackTime = "0"; - private final static String WAYBACKKEY_STR = "waybackkey="; - - public NicoClientImpl(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; - } - // ログイン - Logged_in = login(); - StopFlag = flag; - } + public NicoClientImpl(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; + } + // ログイン + Logged_in = login(); + StopFlag = flag; + } @Override - public File getComment(VideoInfo vi, final File file, final TextProgressListener status, String back_comment) { - return downloadComment(back_comment, file, vi, status, false); + public File getComment(VideoInfo vi, CommentInfo ci, final File file, final TextProgressListener status, String back_comment) { + return downloadComment(back_comment, file, vi, ci, status, false); } /** @@ -98,14 +92,18 @@ public class NicoClientImpl implements NicoClient { private File downloadComment(String back_comment, final File file, VideoInfo vi, final TextProgressListener status, boolean isTcomm) throws NumberFormatException { + return downloadComment(back_comment, file, vi, CommentInfo.DEFAULT, status, isTcomm); + } + + private File downloadComment(String back_comment, final File file, VideoInfo vi, CommentInfo ci, final TextProgressListener status, + boolean isTcomm) throws NumberFormatException { System.out.print("Downloading comment size:" + back_comment + "..."); try { - if (file.canRead()) { // ファイルがすでに存在するなら削除する。 + if (file.canRead()) { // ファイルがすでに存在するなら削除する。 file.delete(); } OutputStream fos = new FileOutputStream(file); - HttpURLConnection con = (HttpURLConnection) (new URL(vi.getMsgUrl())) - .openConnection(ConProxy); + HttpURLConnection con = (HttpURLConnection) (new URL(vi.getMsgUrl())).openConnection(ConProxy); con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); @@ -119,8 +117,8 @@ public class NicoClientImpl implements NicoClient { 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. + String req = "<thread user_id=\"" + vi.getUserId() + "\" when=\"" + ci.getWayBackTime() + "\" waybackkey=\"" + + ci.getWayBackKey() + "\" res_from=\"-" + back_comment + "\" version=\"20061206\" thread=\"" + vi. getThreadId() + "\" " + tcommStr + official + "/>"; os.write(req.getBytes()); os.flush(); @@ -171,81 +169,79 @@ public class NicoClientImpl implements NicoClient { } @Override - 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 { + 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()) { // ファイルがすでに存在するなら削除する。 // file.delete(); // } - HttpURLConnection con = (HttpURLConnection) (new URL(vi.getVideoUrl())) - .openConnection(ConProxy); - /* 出力のみ */ - 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; - } + HttpURLConnection con = (HttpURLConnection) (new URL(vi.getVideoUrl())).openConnection(ConProxy); + /* 出力のみ */ + 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; + 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("動画ダウンロード:" + per + "パーセント完了"); - } else { - status.setText("動画ダウンロード中:" + Integer.toString(size >> 10) - + "kbytesダウンロード"); - } - 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; - } + 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("動画ダウンロード:" + per + "パーセント完了"); + } else { + status.setText("動画ダウンロード中:" + Integer.toString(size >> 10) + + "kbytesダウンロード"); + } + 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 ビデオ名 */ - 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; + 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); @@ -273,6 +269,7 @@ public class NicoClientImpl implements NicoClient { String ret; int index = -1; while ((ret = br.readLine()) != null && index < 0) { + final String TITLE_PARSE_STR_START = "<title>"; if ((index = ret.indexOf(TITLE_PARSE_STR_START)) >= 0) { videoTitle = ret.substring(index + TITLE_PARSE_STR_START.length(), ret.indexOf("‐", index)); videoTitle = FileUtil.safeFileName(videoTitle); @@ -289,10 +286,10 @@ public class NicoClientImpl implements NicoClient { Cookie += new_cookie; return videoTitle; - } + } @Override - public VideoInfo getVideoInfo(String tag, String time) throws IOException { + public VideoInfo getVideoInfo(String tag) throws IOException { final String videoTitle = getVideoHistoryAndTitle(tag); String url = "http://flapi.nicovideo.jp/api/getflv/" + tag; @@ -300,16 +297,17 @@ public class NicoClientImpl implements NicoClient { url += "?as3=1"; } System.out.print("Getting video informations..."); - Map<String,String> res = new NicoApiRequest(url).get(); + 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{ + try { videoLength = Integer.parseInt(videoLengthStr); - }catch(NumberFormatException ex){} + } catch (NumberFormatException ex) { + } OfficialOption oo = null; if ("1".equals(res.get("needs_key"))) { @@ -318,156 +316,154 @@ public class NicoClientImpl implements NicoClient { 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(); + 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); - /* リクエストの設定 */ - 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; - } - /* 戻り値の取得 */ - 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; - } + @Override + public CommentInfo getWayBackKey(VideoInfo vi, String time) throws IOException { + if (StringUtils.isBlank(time)) { + return CommentInfo.DEFAULT; + } + + System.out.print("Setting wayback time..."); + Date date = null; + String waybacktime = "0"; + try { + final DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + date = fmt.parse(time); + } catch (ParseException ex2) { + date = null; + } + if (date == null) { + try { + final DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm"); + date = fmt.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."); + throw new IOException("Cannot parse wayback time.", ex4); + } + } + 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); + /* リクエストの設定 */ + 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); + throw new IOException("Can't get WayBackKey:" + url); + } + /* 戻り値の取得 */ + BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); + ret = br.readLine(); + br.close(); + con.disconnect(); + } catch (IOException ex1) { + System.out.println("ng."); + ex1.printStackTrace(); + throw ex1; + } + int idx = 0; + if ((idx = ret.indexOf(WAYBACKKEY_STR)) < 0) { + System.out.println("ng."); + System.out.println("Cannot find wayback key from response."); + throw new IOException("Cannot find wayback key from response."); + } + 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."); + throw new IOException("Cannot get wayback key."); + } + System.out.println("ok. key:" + waybackkey); + return new CommentInfo(waybackkey, waybacktime); + } @Override - public boolean isLoggedIn() { - return Logged_in; - } + 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); - /* 出力のみ */ - 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 boolean login() { + try { + HttpURLConnection con = (HttpsURLConnection) (new URL( + "https://secure.nicovideo.jp/secure/login?site=niconico")).openConnection(ConProxy); + /* 出力のみ */ + 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; @@ -489,7 +485,7 @@ public class NicoClientImpl implements NicoClient { this.url = url; } - private Map<String,String> get() throws IOException { + 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); -- 2.11.0