2 * Inspired from Nicorank by rankingloid 2008 - 2009
6 import java.util.Arrays;
15 * 説明: ニコニコ動画の動画をコメントつきで保存
22 public class BrowserInfo {
24 public enum BrowserCookieKind {
26 NONE, MSIE, IE6, Firefox3, Firefox, Chrome,
27 Opera, Chromium, Other,}
28 private BrowserCookieKind validBrowser;
30 public String getBrowserName() {
31 if (validBrowser == BrowserCookieKind.NONE) {
33 } else if (validBrowser == BrowserCookieKind.MSIE) {
34 return "Internet Exploror";
36 return validBrowser.toString();
40 public BrowserInfo() {
41 validBrowser = BrowserCookieKind.NONE;
43 private static final String NICOVIDEO_URL = "http://www.nicovideo.jp";
50 public String getUserSession(BrowserCookieKind browserKind) {
51 String user_session = "";
52 switch (browserKind) {
54 user_session = GetUserSessionFromIE6(NICOVIDEO_URL);
57 user_session = GetUserSessionFromMSIE();
60 user_session = GetUserSessionFromFilefox4();
61 if (!user_session.isEmpty()) {
65 user_session = GetUserSessionFromFilefox3();
68 user_session = GetUserSesionChrome();
71 user_session = GetUserSesionChromium();
74 user_session = GetUserSessionOpera();
77 if (!user_session.isEmpty()) {
78 validBrowser = browserKind;
85 * @param fileOrDir fullname of file or directory
88 public String getUserSessionOther(String fileOrDir) {
89 String user_session = "";
91 if (Path.isDirectory(fileOrDir)) {
92 // Directory Type like MSIE
93 user_session = GetUserSessionFromDirectory(fileOrDir);
96 if (Path.isFile(fileOrDir)) {
97 // File Type like Firefox3
98 String dataStr = Path.ReadAllText(fileOrDir, "UTF-8");
99 user_session = CutUserSession(dataStr, fileOrDir);
103 } catch (Exception e) {
107 if (!user_session.isEmpty()) {
108 validBrowser = BrowserCookieKind.Other;
114 /// IE6 から user_session を取得
116 /// <param name="url">サイト(ニコニコ動画)のURL</param>
117 /// <returns>user_session</returns>
118 private String GetUserSessionFromIE6(String url) {
119 return CutUserSession(GetCookieFromIE6(url), "");
125 /// <param name="url">取得するクッキーに関連づけられたURL</param>
126 /// <returns>クッキー文字列</returns>
127 private String GetCookieFromIE6(String url) {
129 byte[] dummy = new byte[size];
130 Arrays.fill(dummy, (byte) ' ');
131 StringBuilder buff = new StringBuilder(new String(dummy));
132 int[] ref_size = new int[1];
134 //InternetGetCookie(url, null, buff, /*ref*/ ref_size);
135 return buff.toString().replace(';', ',');
138 * [DllImport("wininet.dll")]
139 * private extern static bool InternetGetCookie(string url, string name, StringBuilder data, ref uint size);
141 * shuold use NLink.win32
146 * Chromium から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
148 * @return user_session
150 private String GetUserSesionChromium() {
151 String user_session = "";
152 String cookie_file = "";
153 String chromium = "\\Chromium\\User Data\\Default\\Cookies";
155 String local_Appdir = System.getenv("LOCALAPPDATA");
156 if (local_Appdir != null && !local_Appdir.isEmpty()) {
158 cookie_file = local_Appdir + chromium;
159 if (Path.isFile(cookie_file)) {
160 String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
161 user_session = CutUserSession(dataStr, cookie_file);
165 String profile_dir = System.getenv("USERPROFILE");
166 if (profile_dir != null && !profile_dir.isEmpty()) {
168 cookie_file = profile_dir
169 + "\\Local Settings\\Application Data" + chromium;
170 if (Path.isFile(cookie_file)) {
171 String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
172 user_session = CutUserSession(dataStr, cookie_file);
177 } catch (Exception e) {
184 * Opera から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
186 * @return user_session
188 private String GetUserSessionOpera() {
189 String user_session = "";
190 String cookie_file = "";
192 String app_dir = System.getenv("APPDATA");
193 if (app_dir != null && !app_dir.isEmpty()) {
195 cookie_file = app_dir + "\\Opera\\Opera\\cookies4.dat";
196 if (Path.isFile(cookie_file)) {
197 String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
198 user_session = CutUserSession(dataStr, cookie_file);
203 } catch (Exception e) {
210 /// 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
212 /// <param name="str">切り出す対象文字列</param>
213 /// <returns>user_session 文字列。見つからなければ空文字を返す</returns>
214 private String CutUserSession(String str, String filename) {
216 int start = str.indexOf("user_session_");
218 int index = start + "user_session_".length();
219 while (index < str.length() && ('0' <= str.charAt(index) && str.charAt(index) <= '9'
220 || str.charAt(index) == '_')) {
223 ret = str.substring(start, index);
224 // C# の string.SubString( , ) と Java の String.substring( , ) は違うので注意!
225 if (!ret.isEmpty() && !filename.isEmpty()) {
226 System.out.println("Cookie found: " + filename);