6 import java.net.HttpURLConnection;
7 import java.net.InetSocketAddress;
9 import java.net.URLEncoder;
10 import java.net.URLDecoder;
11 import javax.swing.JLabel;
12 import java.text.DateFormat;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
16 import javax.net.ssl.HttpsURLConnection;
17 import saccubus.ConvertStopFlag;
18 import saccubus.util.FileUtil;
22 *
\83^
\83C
\83g
\83\8b:
\82³
\82«
\82ã
\82Î
\82·
26 *
\90à
\96¾:
\83j
\83R
\83j
\83R
\93®
\89æ
\82Ì
\93®
\89æ
\82ð
\83R
\83\81\83\93\83g
\82Â
\82«
\82Å
\95Û
\91¶
30 *
\92\98\8dì
\8c : Copyright (c) 2007 PSI
37 * @author
\96¢
\93ü
\97Í
40 public class NicoClient {
41 private String Cookie = null;
43 private static final String TITLE_PARSE_STR_START = "<title>";
44 private final static DateFormat DateFmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
45 private final static DateFormat DateFmt2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
47 private final String User;
48 private final String Pass;
49 private boolean Logged_in = false;
50 private final ConvertStopFlag StopFlag;
51 private final Proxy ConProxy;
53 private String VideoTitle = null;
54 private int VideoLength = -1;
56 private byte[] buf = new byte[1024 * 1024];
58 private String VideoUrl = null;
59 private String UserID = null;
60 private String ThreadID = null;
61 private String MsgUrl = null;
63 private String WayBackKey = "0";
64 private String WayBackTime = "0";
65 private final static String WAYBACKKEY_STR = "waybackkey=";
67 public NicoClient(final String user, final String pass,
68 final ConvertStopFlag flag, final String proxy, final int proxy_port) {
71 if (proxy != null && proxy.length() > 0 && proxy_port >= 0
72 && proxy_port <= 65535) {
73 ConProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy,
76 ConProxy = Proxy.NO_PROXY;
78 //
\83\8d\83O
\83C
\83\93
83 public String getBackCommentFromLength(String def) {
84 if (VideoLength < 0) {
86 } else if (VideoLength >= 0 && VideoLength < 60) {
88 } else if (VideoLength >= 60 && VideoLength < 300) {
90 } else if (VideoLength >= 300 && VideoLength < 600) {
97 public File getComment(final File file, final JLabel status,
98 String back_comment) {
99 System.out.print("Downloading comment size:" + back_comment + "...");
101 if (file.canRead()) { //
\83t
\83@
\83C
\83\8b\82ª
\82·
\82Å
\82É
\91¶
\8dÝ
\82·
\82é
\82È
\82ç
\8dí
\8f\9c\82·
\82é
\81B
104 OutputStream fos = new FileOutputStream(file);
105 HttpURLConnection con = (HttpURLConnection) (new URL(MsgUrl))
106 .openConnection(ConProxy);
107 con.setDoOutput(true);
108 con.setDoInput(true);
109 con.setRequestMethod("POST");
110 con.addRequestProperty("Cookie", Cookie);
111 con.addRequestProperty("Connection", "close");
113 OutputStream os = con.getOutputStream();
114 String req = "<thread user_id=\"" + UserID + "\" when=\""
115 + WayBackTime + "\" waybackkey=\"" + WayBackKey
116 + "\" res_from=\"-" + back_comment
117 + "\" version=\"20061206\" thread=\"" + ThreadID + "\" />";
118 os.write(req.getBytes());
121 if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
122 System.out.println("ng.\nCan't download comment:" + MsgUrl);
125 InputStream is = con.getInputStream();
128 String content_length_str = con.getHeaderField("Content-length");
129 if (content_length_str != null && !content_length_str.equals("")) {
130 max_size = Integer.parseInt(content_length_str);
133 while ((read = is.read(buf, 0, buf.length)) > 0) {
134 fos.write(buf, 0, read);
137 String per = Double.toString((((double) size) * 100)
139 per = per.substring(0, Math.min(per.indexOf(".") + 3, per
141 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¹");
143 status.setText("
\83R
\83\81\83\93\83g
\83_
\83E
\83\93\83\8d\81[
\83h
\92\86\81F"
144 + Integer.toString(size >> 10) + "kbytes
\83_
\83E
\83\93\83\8d\81[
\83h");
146 if (StopFlag.needStop()) {
147 System.out.println("Stopped.");
156 System.out.println("ok.");
162 } catch (IOException ex) {
163 ex.printStackTrace();
169 public File getVideo(final File file, final JLabel status) {
170 if (VideoUrl == null) {
171 System.out.println("Video url is not detected.");
175 if (file.canRead()) { //
\83t
\83@
\83C
\83\8b\82ª
\82·
\82Å
\82É
\91¶
\8dÝ
\82·
\82é
\82È
\82ç
\8dí
\8f\9c\82·
\82é
\81B
178 HttpURLConnection con = (HttpURLConnection) (new URL(VideoUrl))
179 .openConnection(ConProxy);
180 /*
\8fo
\97Í
\82Ì
\82Ý */
181 con.setDoInput(true);
182 con.setRequestMethod("GET");
183 con.addRequestProperty("Cookie", Cookie);
185 if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
186 System.out.println("Can't get video:" + VideoUrl);
189 InputStream is = con.getInputStream();
190 OutputStream os = new FileOutputStream(file);
191 String content_length_str = con.getHeaderField("Content-length");
193 if (content_length_str != null && !content_length_str.equals("")) {
194 max_size = Integer.parseInt(content_length_str);
197 System.out.print("Downloading video...");
199 while ((read = is.read(buf, 0, buf.length)) > 0) {
201 os.write(buf, 0, read);
203 String per = Double.toString((((double) size) * 100)
205 per = per.substring(0, Math.min(per.indexOf(".") + 3, per
207 status.setText("
\93®
\89æ
\83_
\83E
\83\93\83\8d\81[
\83h
\81F" + per + "
\83p
\81[
\83Z
\83\93\83g
\8a®
\97¹");
209 status.setText("
\93®
\89æ
\83_
\83E
\83\93\83\8d\81[
\83h
\92\86\81F" + Integer.toString(size >> 10)
210 + "kbytes
\83_
\83E
\83\93\83\8d\81[
\83h");
212 if (StopFlag.needStop()) {
213 System.out.println("Stopped.");
222 System.out.println("ok.");
228 } catch (FileNotFoundException ex) {
229 ex.printStackTrace();
230 } catch (IOException ex) {
231 ex.printStackTrace();
236 public boolean getVideoHistoryAndTitle(String tag) {
237 String url = "http://www.nicovideo.jp/watch/" + tag;
238 System.out.print("Getting video history...");
239 String new_cookie = null;
241 HttpURLConnection con = (HttpURLConnection) (new URL(url))
242 .openConnection(ConProxy);
243 /*
\83\8a\83N
\83G
\83X
\83g
\82Ì
\90Ý
\92è */
244 con.setRequestMethod("GET");
245 con.addRequestProperty("Cookie", Cookie);
246 con.addRequestProperty("Connection", "close");
248 if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
249 System.out.println("Can't getVideoHistory:" + url);
255 while ((key = con.getHeaderFieldKey(i)) != null) {
256 if (key.equalsIgnoreCase("Set-Cookie")) {
257 value = con.getHeaderField(i);
259 new_cookie = value.substring(0, value.indexOf(";"));
264 BufferedReader br = new BufferedReader(new InputStreamReader(con
265 .getInputStream(), "UTF-8"));
268 while ((ret = br.readLine()) != null && index < 0) {
269 if ((index = ret.indexOf(TITLE_PARSE_STR_START)) >= 0) {
270 VideoTitle = ret.substring(index+TITLE_PARSE_STR_START.length(), ret.indexOf("
\81]", index));
271 VideoTitle = FileUtil.safeFileName(VideoTitle);
276 if (new_cookie == null) {
277 System.out.println("Can't getVideoHistory: cannot get cookie.");
280 System.out.println("ok.");
282 Cookie += new_cookie;
283 } catch (IOException ex) {
284 ex.printStackTrace();
291 public boolean getVideoInfo(String tag, String time) {
292 if (!getVideoHistoryAndTitle(tag)) {
296 String url = "http://www.nicovideo.jp/api/getflv/" + tag;
297 if (tag.startsWith("nm")) {
300 System.out.print("Getting video informations...");
301 HttpURLConnection con = (HttpURLConnection) (new URL(url))
302 .openConnection(ConProxy);
303 /*
\83\8a\83N
\83G
\83X
\83g
\82Ì
\90Ý
\92è */
304 con.setRequestMethod("GET");
305 con.addRequestProperty("Cookie", Cookie);
306 con.addRequestProperty("Connection", "close");
308 if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
309 System.out.println("Can't getVideoInfo:" + url);
312 /*
\96ß
\82è
\92l
\82Ì
\8eæ
\93¾ */
313 BufferedReader br = new BufferedReader(new InputStreamReader(con
315 String ret = br.readLine();
318 ret = URLDecoder.decode(ret, "Shift_JIS");
319 String[] array = ret.split("&");
321 for (int i = 0; i < array.length; i++) {
322 int idx = array[i].indexOf("=");
326 String key = array[i].substring(0, idx);
327 String value = array[i].substring(idx + 1);
328 if (ThreadID == null && key.equalsIgnoreCase("thread_id")) {
331 } else if (VideoUrl == null && key.equalsIgnoreCase("url")) {
334 } else if (MsgUrl == null && key.equalsIgnoreCase("ms")) {
337 } else if (UserID == null && key.equalsIgnoreCase("user_id")) {
340 } else if (VideoLength < 0 && key.equalsIgnoreCase("l")) {
342 VideoLength = Integer.parseInt(value);
343 } catch (NumberFormatException e) {
350 .println("ng.\nCan't getVideoInfo: Can't get video informations.");
353 System.out.println("ok.");
354 } catch (IOException ex) {
355 ex.printStackTrace();
358 if (!(time == null || time.equals("")) && !getWayBackKey(time)) { // WayBackKey
364 public String getVideoTitle() {
368 private boolean getWayBackKey(String time) {
369 System.out.print("Setting wayback time...");
371 String waybacktime = "0";
373 date = DateFmt.parse(time);
374 } catch (ParseException ex2) {
379 date = DateFmt2.parse(time);
380 } catch (ParseException ex3) {
385 waybacktime = Long.toString(date.getTime() / 1000);
386 System.out.println("ok.(" + date.toString() + "):" + waybacktime);
389 long tmp_time = Long.parseLong(time);
390 waybacktime = Long.toString(tmp_time);
391 date = new Date(tmp_time * 1000);
392 System.out.println("ok.(" + date.toString() + "):"
394 } catch (NumberFormatException ex4) {
395 System.out.println("ng.");
396 System.out.println("Cannot parse wayback time.");
400 System.out.print("Getting wayback key...");
401 String url = "http://www.nicovideo.jp/api/getwaybackkey?thread="
405 HttpURLConnection con = (HttpURLConnection) (new URL(url))
406 .openConnection(ConProxy);
407 /*
\83\8a\83N
\83G
\83X
\83g
\82Ì
\90Ý
\92è */
408 con.setRequestMethod("GET");
409 con.addRequestProperty("Cookie", Cookie);
410 con.addRequestProperty("Connection", "close");
411 con.setDoInput(true);
413 if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
414 System.out.println("Can't get WayBackKey:" + url);
417 /*
\96ß
\82è
\92l
\82Ì
\8eæ
\93¾ */
418 BufferedReader br = new BufferedReader(new InputStreamReader(con
423 } catch (IOException ex1) {
424 System.out.println("ng.");
425 ex1.printStackTrace();
429 if ((idx = ret.indexOf(WAYBACKKEY_STR)) < 0) {
430 System.out.println("ng.");
431 System.out.println("Cannot find wayback key from response.");
434 int end_idx = Math.max(ret.length(), ret.indexOf("&"));
435 String waybackkey = ret.substring(idx + WAYBACKKEY_STR.length(),
437 if (waybackkey == null || waybackkey.equals("")) {
438 System.out.println("ng.");
439 System.out.println("Cannot get wayback key.");
442 System.out.println("ok. key:" + waybackkey);
443 WayBackTime = waybacktime;
444 WayBackKey = waybackkey;
448 public boolean isLoggedIn() {
452 private boolean login() {
454 HttpURLConnection con = (HttpsURLConnection) (new URL(
455 "https://secure.nicovideo.jp/secure/login?site=niconico"))
456 .openConnection(ConProxy);
457 /*
\8fo
\97Í
\82Ì
\82Ý */
458 con.setDoOutput(true);
459 HttpURLConnection.setFollowRedirects(false);
460 con.setInstanceFollowRedirects(false);
461 con.setRequestMethod("POST");
462 con.addRequestProperty("Connection", "close");
464 StringBuffer sb = new StringBuffer(4096);
465 sb.append("next_url=&");
467 sb.append(URLEncoder.encode(User, "Shift_JIS"));
468 sb.append("&password=");
469 sb.append(URLEncoder.encode(Pass, "Shift_JIS"));
470 sb.append("&submit.x=103&submit.y=16");
471 OutputStream os = con.getOutputStream();
472 os.write(sb.substring(0).getBytes());
475 int code = con.getResponseCode();
476 if (code < 200 || code >= 400) {
477 System.out.println("Can't login:" + con.getResponseMessage());
483 while ((key = con.getHeaderFieldKey(i)) != null) {
484 if (key.equalsIgnoreCase("Set-Cookie")) {
485 value = con.getHeaderField(i);
487 Cookie = value.substring(0, value.indexOf(";"));
493 if (Cookie == null) {
494 System.out.println("Can't login: cannot set cookie.");
497 System.out.println("Logged in.");
498 } catch (IOException ex) {
499 ex.printStackTrace();