PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/local/box/yt_dump/yt_web.cpp

http://mingw-lib.googlecode.com/
C++ | 398 lines | 282 code | 32 blank | 84 comment | 42 complexity | ee7cec3ad687be2bdbdf8f085db3de7f MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. #include "yt_web.h"
  2. #include <QtCore>
  3. #include <QtGui>
  4. #include <QtWebKit>
  5. #include "xmlutil.h"
  6. #include "env.h"
  7. YtWeb::YtWeb(QWebView *a_webview)
  8. : m_webview(a_webview)
  9. {
  10. connect(m_webview->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
  11. this, SLOT(populateJavaScriptWindowObject()));
  12. }
  13. void YtWeb::populateJavaScriptWindowObject()
  14. {
  15. qDebug() << "YtWeb::populateJavaScriptWindowObject(ytweb)";
  16. m_webview->page()->mainFrame()->addToJavaScriptWindowObject("ytweb", this);
  17. }
  18. bool YtWeb::getFileForVideoIcon(QString a_id, QFile &a_file)
  19. {
  20. QString v_deskPath =
  21. QDesktopServices::storageLocation(
  22. QDesktopServices::DesktopLocation);
  23. QDir v_deskDir(v_deskPath);
  24. v_deskDir.mkpath("@ytcache/dl");
  25. QString v_cdirPath = v_deskDir.filePath("@ytcache/dl");
  26. QString v_iconPath = v_cdirPath + "/" + a_id + ".jpg";
  27. a_file.setFileName(v_iconPath);
  28. return a_file.exists();
  29. }
  30. QString YtWeb::getUrlForVideoIcon(QString a_id)
  31. {
  32. QFile v_iconFile;
  33. getFileForVideoIcon(a_id, v_iconFile);
  34. if(v_iconFile.exists())
  35. {
  36. QUrl v_local_url = QUrl::fromLocalFile(v_iconFile.fileName());
  37. return v_local_url.toString();
  38. }
  39. QString v_remote_url = QString("http://i.ytimg.com/vi/%1/2.jpg").arg(a_id);
  40. return v_remote_url;
  41. }
  42. void YtWeb::initPage(QWebView *a_webview, QWebElement *a_webelem)
  43. {
  44. QString v_html;
  45. v_html += "<html>\n";
  46. v_html += "<head>\n";
  47. v_html += "<link rel='stylesheet' href='qrc:/yt_list.css' type='text/css' />\n";
  48. v_html += "</head>\n";
  49. v_html += "<body>\n";
  50. v_html += "<div id=\"mydiv\">\n</div>\n";
  51. v_html += "</body>\n";
  52. v_html += "</html>\n";
  53. v_html += "<script type='text/javascript' src='qrc:/jslib/jquery.js' charset='uft-8' />\n";
  54. v_html += "<script type='text/javascript' src='qrc:/yt_list.js' charset='uft-8' />\n";
  55. //a_webview->setHtml(v_html);
  56. //a_webview->setUrl(QUrl("qrc:/page/page1.html"));
  57. QWebFrame *v_frame = a_webview->page()->mainFrame();
  58. (*a_webelem) = v_frame->findFirstElement("#mydiv");
  59. qDebug() << "[a_webelem->isNull()]" << a_webelem->isNull();
  60. }
  61. QString YtWeb::getTokenForVideoStreamEmbed(QString a_id, QMap<int, QUrl> &a_fmt_map)
  62. {
  63. //[Get Token]
  64. QString v_url_1 =
  65. QString("http://www.youtube.com/get_video_info?&video_id=%1&el=embedded&ps=default")
  66. //QString("http://www.youtube.com/get_video_info?&video_id=%1&el=detailpage&ps=&noflv=1")
  67. .arg(a_id);
  68. ////QByteArray v_params_1 = NetworkThreadRequest::doGet2(v_url_1);
  69. QByteArray v_params_1 = g_Env->nam->doGet(v_url_1);
  70. v_params_1.prepend("?");
  71. QUrl v_params_2;
  72. v_params_2.setEncodedUrl(v_params_1);
  73. QString v_status = v_params_2.queryItemValue("status");
  74. QString v_reason = v_params_2.queryItemValue("reason");
  75. QString v_token = v_params_2.queryItemValue("token");
  76. QString v_allow_embed = v_params_2.queryItemValue("allow_embed");
  77. QString v_fmt_stream_map = v_params_2.queryItemValue("fmt_stream_map");
  78. //qDebug() << v_status << v_allow_embed << v_token << v_fmt_stream_map;
  79. if(v_status == "fail")
  80. {
  81. //QMessageBox::information(0, "fail", v_reason);
  82. g_Env->showMessage(v_reason);
  83. }
  84. else
  85. {
  86. g_Env->showMessage("");
  87. }
  88. if(v_token == "")
  89. {
  90. qDebug() << v_params_2;
  91. }
  92. a_fmt_map.clear();
  93. if(v_fmt_stream_map != "")
  94. {
  95. QStringList v_slist_1 = v_fmt_stream_map.split(',');
  96. for(int i=0; i<v_slist_1.size(); i++)
  97. {
  98. QStringList v_slist_2 = v_slist_1.at(i).split('|');
  99. int v_fmt = v_slist_2.at(0).toInt();
  100. QString v_stream_url_1 = v_slist_2.at(1);
  101. QUrl v_stream_url_2;
  102. v_stream_url_2.setEncodedUrl(v_stream_url_1.toLatin1());
  103. a_fmt_map.insert(v_fmt, v_stream_url_2);
  104. }
  105. }
  106. qDebug() << "[v_token(embed)]" << v_token;
  107. qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
  108. return v_token;
  109. }
  110. QString YtWeb::getTokenForVideoStreamDetail(QString a_id, QMap<int, QUrl> &a_fmt_map)
  111. {
  112. QString v_watch_url =
  113. QString("http://www.youtube.com/watch?v=%1")
  114. .arg(a_id);
  115. qDebug() << "[v_watch_url]" << v_watch_url;
  116. BatchNetworkAccessManager v_bnam;
  117. QByteArray v_html_raw = v_bnam.doGet(v_watch_url);
  118. //qDebug() << "[v_html_raw]" << v_html_raw;
  119. qDebug() << "[v_html_raw.size()]" << v_html_raw.size();
  120. QString v_html_str = QString::fromUtf8(v_html_raw);
  121. //qDebug() << "[v_html_str]" << v_html_str;
  122. int v_pos = 0;
  123. QRegExp v_rxStream("[?&](fmt_stream_map=[^&'\"\\\\]+)");
  124. v_pos = v_rxStream.indexIn(v_html_str, 0);
  125. a_fmt_map.clear();
  126. if(v_pos != -1)
  127. {
  128. QByteArray v_params_raw = v_rxStream.cap(1).toLatin1();
  129. v_params_raw.prepend("?");
  130. QUrl v_params_url;
  131. v_params_url.setEncodedUrl(v_params_raw);
  132. QString v_fmt_stream_map = v_params_url.queryItemValue("fmt_stream_map");
  133. if(v_fmt_stream_map != "")
  134. {
  135. QStringList v_slist_1 = v_fmt_stream_map.split(',');
  136. for(int i=0; i<v_slist_1.size(); i++)
  137. {
  138. QStringList v_slist_2 = v_slist_1.at(i).split('|');
  139. int v_fmt = v_slist_2.at(0).toInt();
  140. QString v_stream_url_1 = v_slist_2.at(1);
  141. QUrl v_stream_url_2;
  142. v_stream_url_2.setEncodedUrl(v_stream_url_1.toLatin1());
  143. a_fmt_map.insert(v_fmt, v_stream_url_2);
  144. }
  145. }
  146. }
  147. QRegExp v_rxToken("[?&](t=[^&'\"\\\\]+)");
  148. v_pos = v_rxToken.indexIn(v_html_str, 0);
  149. qDebug() << "[pos]" << v_pos;
  150. if(v_pos == -1)
  151. {
  152. return "";
  153. }
  154. else
  155. {
  156. QByteArray v_params_raw = v_rxToken.cap(1).toLatin1();
  157. v_params_raw.prepend("?");
  158. QUrl v_params_url;
  159. v_params_url.setEncodedUrl(v_params_raw);
  160. QString v_token = v_params_url.queryItemValue("t");
  161. qDebug() << "[v_token(detail)]" << v_token;
  162. qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
  163. return v_token;
  164. }
  165. }
  166. /*
  167. http://code.google.com/p/get-flash-videos/source/browse/trunk/FlashVideo/Site/Youtube.pm?r=311
  168. my @formats = (
  169. { id => 37, resolution => [1920, 1080] },
  170. { id => 22, resolution => [1280, 720] },
  171. { id => 35, resolution => [854, 480] },
  172. { id => 34, resolution => [640, 360] },
  173. { id => 18, resolution => [480, 270] },
  174. { id => 5, resolution => [400, 224] },
  175. { id => 17, resolution => [176, 144] },
  176. { id => 13, resolution => [176, 144] },
  177. );
  178. */
  179. /*****
  180. QUrl YtWeb::getUrlForVideoStream(QString a_id, int a_fmt)
  181. {
  182. if(a_fmt < 0)
  183. {
  184. a_fmt = 34;
  185. }
  186. //[Get Token]
  187. QMap<int, QUrl> v_fmt_map;
  188. QString v_token = getTokenForVideoStreamEmbed(a_id, v_fmt_map);
  189. if(v_token == "")
  190. {
  191. v_token = getTokenForVideoStreamDetail(a_id, v_fmt_map);
  192. }
  193. if(v_fmt_map.contains(a_fmt))
  194. {
  195. return v_fmt_map.value(a_fmt);
  196. }
  197. if(!v_fmt_map.contains(34) && v_fmt_map.contains(5))
  198. {
  199. a_fmt = 5;
  200. }
  201. qDebug() << "[v_token]" << v_token;
  202. //[Get Video Location]
  203. QUrl v_video_url("http://www.youtube.com/get_video");
  204. v_video_url.addQueryItem("video_id", a_id);
  205. v_video_url.addQueryItem("t", v_token);
  206. if(a_fmt >= 0)
  207. {
  208. QString v_fmt = QString("%1").arg(a_fmt);
  209. v_video_url.addQueryItem("fmt", v_fmt);
  210. }
  211. QByteArray v_location = NetworkThreadRequest::doGet2(v_video_url);
  212. qDebug() << "[v_location]" << v_location;
  213. //[Return Video Stream Url]
  214. QUrl v_result;
  215. v_result.setEncodedUrl(v_location);
  216. return v_result;
  217. }
  218. *****/
  219. QString YtWeb::getVideoStreamMap(QString a_id, QMap<int, QUrl> &a_fmt_map)
  220. {
  221. //[Get Token & Stream Map]
  222. QString v_token = getTokenForVideoStreamEmbed(a_id, a_fmt_map);
  223. if(v_token == "")
  224. {
  225. v_token = getTokenForVideoStreamDetail(a_id, a_fmt_map);
  226. }
  227. qDebug() << "[v_token(getVideoStreamMap)]" << v_token;
  228. qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
  229. return v_token;
  230. }
  231. void YtWeb::pageVideoPlay(QWebElement *a_webelem, QString a_id, T_VideoList *a_recList)
  232. {
  233. QWebElement v_webelem = m_webview->page()->mainFrame()->findFirstElement("#rec_place");
  234. a_webelem = &v_webelem;
  235. QWebElement v_vlc_button_place = m_webview->page()->mainFrame()->findFirstElement("#vlc_button_place");
  236. v_vlc_button_place.setInnerXml("");
  237. QString v_inner_html = "";
  238. QUrl v_stream_url;
  239. QMap<int, QUrl> v_fmt_map;
  240. QString v_token = getVideoStreamMap(a_id, v_fmt_map);
  241. int v_fmt = -1;
  242. if(false) v_fmt = 0;
  243. else if(v_fmt_map.contains(22)) v_fmt = 22;
  244. else if(v_fmt_map.contains(35)) v_fmt = 35;
  245. //else if(v_fmt_map.contains(18)) v_fmt = 18;
  246. //else if(v_fmt_map.contains(5)) v_fmt = 5;
  247. else if(v_fmt_map.contains(34)) v_fmt = 34;
  248. else if(v_fmt_map.contains(18)) v_fmt = 18;
  249. else if(v_fmt_map.contains(5)) v_fmt = 5;
  250. else if(v_fmt_map.contains(35)) v_fmt = 35;
  251. //else if(v_fmt_map.contains(18)) v_fmt = 18;
  252. else if(v_fmt_map.contains(5)) v_fmt = 5;
  253. if(v_fmt != -1)
  254. {
  255. v_stream_url = v_fmt_map.value(v_fmt);
  256. }
  257. if(v_fmt_map.size() > 0)
  258. {
  259. QString dirPath =
  260. QDesktopServices::storageLocation(
  261. QDesktopServices::DesktopLocation);
  262. QDir dir(dirPath);
  263. QString filePath = dir.filePath("saved.m3u");
  264. QFile file;
  265. file.setFileName(filePath);
  266. qDebug() << file.fileName();
  267. file.open(QIODevice::WriteOnly | QIODevice::Truncate);
  268. file.write("#EXTM3U\n");
  269. file.write("#\n");
  270. QList<int> v_fmt_keys = v_fmt_map.keys();
  271. for(int i=0; i<v_fmt_keys.size(); i++)
  272. {
  273. file.write(QString("#EXTINF:0,[id=%1 fmt=%2]\n").arg(a_id).arg(v_fmt_keys.at(i)).toUtf8().constData());
  274. file.write(QString("%1\n").arg(v_fmt_map.value(v_fmt_keys.at(i)).toString()).toUtf8().constData());
  275. }
  276. file.close();
  277. }
  278. qDebug() << "[v_fmt]" << v_fmt;
  279. if(v_stream_url.toString()=="")
  280. {
  281. qDebug() << "[v_fmt_map]" << v_fmt_map;
  282. }
  283. qDebug() << "[v_stream_url]" << v_stream_url;
  284. qDebug() << "[v_stream_url.toPercentEncoding()]" << QUrl::toPercentEncoding(v_stream_url.toString());
  285. /* VLC plugin
  286. http://www.videolan.org/doc/play-howto/en/ch04.html
  287. */
  288. if(v_stream_url.toString() != "")
  289. {
  290. QList<int> v_fmt_keys = v_fmt_map.keys();
  291. foreach(int v_fmt_key, v_fmt_keys)
  292. {
  293. v_inner_html += QString(
  294. "<button onclick=\"fn_vlc_player('#vlc_place', '%1', 'fmt=%2')\">%2</button>"
  295. )
  296. .arg(v_fmt_map.value(v_fmt_key).toString())
  297. .arg(v_fmt_key)
  298. ;
  299. }
  300. //"<button onclick=\"fn_vlc_player('#vlc_place', '%1')\">zzz</button>"
  301. qDebug() << v_inner_html;
  302. v_vlc_button_place.appendInside(v_inner_html);
  303. emit answer_vlc_player(v_stream_url.toString(), QString("fmt=%1").arg(v_fmt));
  304. //emit answer_yt_player(a_id, a_id);
  305. }
  306. if(!a_recList) return;
  307. v_inner_html = "";
  308. for (int i=0; i<a_recList->size(); i++)
  309. {
  310. QString v_id = a_recList->value(i)->id;
  311. QString v_title = a_recList->value(i)->title;
  312. v_title = Qt::escape(v_title);
  313. QString v_author = a_recList->value(i)->author;
  314. qint64 v_view_count = a_recList->value(i)->view_count;
  315. #if 0x1
  316. QString v_style = "";
  317. T_Video v_rec_fv;
  318. if(YtManager::dbVideoSelect(v_id, "fv_akmiyoshi", &v_rec_fv))
  319. {
  320. //v_style = "style='background-color:#ff9933;'";
  321. v_style = "style='background-color:silver;'";
  322. }
  323. #endif
  324. QString v_append =
  325. QString(
  326. "<div id='outer_%1' %5>"
  327. "<table border=0><tr>"
  328. " <td>"
  329. //"<image src='http://i.ytimg.com/vi/%1/2.jpg' width=120 height=90 "
  330. "<image src='%4' width=120 height=90 "
  331. "onclick='web1.submitIdToPlay(\"%1\");' "
  332. "/>"
  333. " </td>"
  334. " <td>"
  335. //" %2<br />"
  336. " <a href='http://www.youtube.com/watch?v=%1'>%2</a><br />"
  337. //" <button onclick='web1.submitIdToPlay(\"%1\");'>??</button>"
  338. " <button onclick='web1.submitIdToBrowse(\"%1\");'>??</button>"
  339. //" <a href='http://www.youtube.com/watch?v=%1'>??2</a>"
  340. " <a href='http://www.youtube.com/user/%6'>%6</a>"
  341. " <button onclick='fn_del_image(\"%1\");'>??</button>"
  342. " [%L3 ?]"
  343. " </td>"
  344. "</table>"
  345. "</div>"
  346. )
  347. .arg(v_id)
  348. .arg(v_title)
  349. .arg(v_view_count)
  350. .arg(YtWeb::getUrlForVideoIcon(v_id))
  351. .arg(v_style)
  352. .arg(v_author)
  353. ;
  354. //qDebug() << v_append;
  355. v_inner_html += v_append;
  356. }
  357. a_webelem->setInnerXml("");
  358. a_webelem->appendInside(v_inner_html);
  359. }