/local/box/yt_dump/yt_web.cpp
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
- #include "yt_web.h"
-
- #include <QtCore>
- #include <QtGui>
- #include <QtWebKit>
-
- #include "xmlutil.h"
- #include "env.h"
-
- YtWeb::YtWeb(QWebView *a_webview)
- : m_webview(a_webview)
- {
- connect(m_webview->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
- this, SLOT(populateJavaScriptWindowObject()));
- }
-
- void YtWeb::populateJavaScriptWindowObject()
- {
- qDebug() << "YtWeb::populateJavaScriptWindowObject(ytweb)";
- m_webview->page()->mainFrame()->addToJavaScriptWindowObject("ytweb", this);
- }
-
- bool YtWeb::getFileForVideoIcon(QString a_id, QFile &a_file)
- {
- QString v_deskPath =
- QDesktopServices::storageLocation(
- QDesktopServices::DesktopLocation);
- QDir v_deskDir(v_deskPath);
- v_deskDir.mkpath("@ytcache/dl");
- QString v_cdirPath = v_deskDir.filePath("@ytcache/dl");
- QString v_iconPath = v_cdirPath + "/" + a_id + ".jpg";
- a_file.setFileName(v_iconPath);
- return a_file.exists();
- }
-
- QString YtWeb::getUrlForVideoIcon(QString a_id)
- {
- QFile v_iconFile;
- getFileForVideoIcon(a_id, v_iconFile);
- if(v_iconFile.exists())
- {
- QUrl v_local_url = QUrl::fromLocalFile(v_iconFile.fileName());
- return v_local_url.toString();
- }
- QString v_remote_url = QString("http://i.ytimg.com/vi/%1/2.jpg").arg(a_id);
- return v_remote_url;
- }
-
- void YtWeb::initPage(QWebView *a_webview, QWebElement *a_webelem)
- {
- QString v_html;
- v_html += "<html>\n";
- v_html += "<head>\n";
- v_html += "<link rel='stylesheet' href='qrc:/yt_list.css' type='text/css' />\n";
- v_html += "</head>\n";
- v_html += "<body>\n";
- v_html += "<div id=\"mydiv\">\n</div>\n";
- v_html += "</body>\n";
- v_html += "</html>\n";
- v_html += "<script type='text/javascript' src='qrc:/jslib/jquery.js' charset='uft-8' />\n";
- v_html += "<script type='text/javascript' src='qrc:/yt_list.js' charset='uft-8' />\n";
-
- //a_webview->setHtml(v_html);
- //a_webview->setUrl(QUrl("qrc:/page/page1.html"));
-
- QWebFrame *v_frame = a_webview->page()->mainFrame();
- (*a_webelem) = v_frame->findFirstElement("#mydiv");
- qDebug() << "[a_webelem->isNull()]" << a_webelem->isNull();
- }
-
- QString YtWeb::getTokenForVideoStreamEmbed(QString a_id, QMap<int, QUrl> &a_fmt_map)
- {
- //[Get Token]
- QString v_url_1 =
- QString("http://www.youtube.com/get_video_info?&video_id=%1&el=embedded&ps=default")
- //QString("http://www.youtube.com/get_video_info?&video_id=%1&el=detailpage&ps=&noflv=1")
- .arg(a_id);
- ////QByteArray v_params_1 = NetworkThreadRequest::doGet2(v_url_1);
- QByteArray v_params_1 = g_Env->nam->doGet(v_url_1);
- v_params_1.prepend("?");
- QUrl v_params_2;
- v_params_2.setEncodedUrl(v_params_1);
- QString v_status = v_params_2.queryItemValue("status");
- QString v_reason = v_params_2.queryItemValue("reason");
- QString v_token = v_params_2.queryItemValue("token");
- QString v_allow_embed = v_params_2.queryItemValue("allow_embed");
- QString v_fmt_stream_map = v_params_2.queryItemValue("fmt_stream_map");
- //qDebug() << v_status << v_allow_embed << v_token << v_fmt_stream_map;
- if(v_status == "fail")
- {
- //QMessageBox::information(0, "fail", v_reason);
- g_Env->showMessage(v_reason);
- }
- else
- {
- g_Env->showMessage("");
- }
- if(v_token == "")
- {
- qDebug() << v_params_2;
- }
- a_fmt_map.clear();
- if(v_fmt_stream_map != "")
- {
- QStringList v_slist_1 = v_fmt_stream_map.split(',');
- for(int i=0; i<v_slist_1.size(); i++)
- {
- QStringList v_slist_2 = v_slist_1.at(i).split('|');
- int v_fmt = v_slist_2.at(0).toInt();
- QString v_stream_url_1 = v_slist_2.at(1);
- QUrl v_stream_url_2;
- v_stream_url_2.setEncodedUrl(v_stream_url_1.toLatin1());
- a_fmt_map.insert(v_fmt, v_stream_url_2);
- }
- }
- qDebug() << "[v_token(embed)]" << v_token;
- qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
- return v_token;
- }
-
- QString YtWeb::getTokenForVideoStreamDetail(QString a_id, QMap<int, QUrl> &a_fmt_map)
- {
- QString v_watch_url =
- QString("http://www.youtube.com/watch?v=%1")
- .arg(a_id);
- qDebug() << "[v_watch_url]" << v_watch_url;
- BatchNetworkAccessManager v_bnam;
- QByteArray v_html_raw = v_bnam.doGet(v_watch_url);
- //qDebug() << "[v_html_raw]" << v_html_raw;
- qDebug() << "[v_html_raw.size()]" << v_html_raw.size();
- QString v_html_str = QString::fromUtf8(v_html_raw);
- //qDebug() << "[v_html_str]" << v_html_str;
-
- int v_pos = 0;
-
- QRegExp v_rxStream("[?&](fmt_stream_map=[^&'\"\\\\]+)");
- v_pos = v_rxStream.indexIn(v_html_str, 0);
- a_fmt_map.clear();
- if(v_pos != -1)
- {
- QByteArray v_params_raw = v_rxStream.cap(1).toLatin1();
- v_params_raw.prepend("?");
- QUrl v_params_url;
- v_params_url.setEncodedUrl(v_params_raw);
- QString v_fmt_stream_map = v_params_url.queryItemValue("fmt_stream_map");
- if(v_fmt_stream_map != "")
- {
- QStringList v_slist_1 = v_fmt_stream_map.split(',');
- for(int i=0; i<v_slist_1.size(); i++)
- {
- QStringList v_slist_2 = v_slist_1.at(i).split('|');
- int v_fmt = v_slist_2.at(0).toInt();
- QString v_stream_url_1 = v_slist_2.at(1);
- QUrl v_stream_url_2;
- v_stream_url_2.setEncodedUrl(v_stream_url_1.toLatin1());
- a_fmt_map.insert(v_fmt, v_stream_url_2);
- }
- }
- }
-
-
- QRegExp v_rxToken("[?&](t=[^&'\"\\\\]+)");
-
- v_pos = v_rxToken.indexIn(v_html_str, 0);
- qDebug() << "[pos]" << v_pos;
- if(v_pos == -1)
- {
- return "";
- }
- else
- {
- QByteArray v_params_raw = v_rxToken.cap(1).toLatin1();
- v_params_raw.prepend("?");
- QUrl v_params_url;
- v_params_url.setEncodedUrl(v_params_raw);
- QString v_token = v_params_url.queryItemValue("t");
- qDebug() << "[v_token(detail)]" << v_token;
- qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
- return v_token;
- }
- }
-
- /*
- http://code.google.com/p/get-flash-videos/source/browse/trunk/FlashVideo/Site/Youtube.pm?r=311
- my @formats = (
- { id => 37, resolution => [1920, 1080] },
- { id => 22, resolution => [1280, 720] },
- { id => 35, resolution => [854, 480] },
- { id => 34, resolution => [640, 360] },
- { id => 18, resolution => [480, 270] },
- { id => 5, resolution => [400, 224] },
- { id => 17, resolution => [176, 144] },
- { id => 13, resolution => [176, 144] },
- );
- */
- /*****
- QUrl YtWeb::getUrlForVideoStream(QString a_id, int a_fmt)
- {
- if(a_fmt < 0)
- {
- a_fmt = 34;
- }
- //[Get Token]
- QMap<int, QUrl> v_fmt_map;
- QString v_token = getTokenForVideoStreamEmbed(a_id, v_fmt_map);
-
- if(v_token == "")
- {
- v_token = getTokenForVideoStreamDetail(a_id, v_fmt_map);
- }
-
- if(v_fmt_map.contains(a_fmt))
- {
- return v_fmt_map.value(a_fmt);
- }
-
- if(!v_fmt_map.contains(34) && v_fmt_map.contains(5))
- {
- a_fmt = 5;
- }
-
- qDebug() << "[v_token]" << v_token;
-
- //[Get Video Location]
- QUrl v_video_url("http://www.youtube.com/get_video");
- v_video_url.addQueryItem("video_id", a_id);
- v_video_url.addQueryItem("t", v_token);
- if(a_fmt >= 0)
- {
- QString v_fmt = QString("%1").arg(a_fmt);
- v_video_url.addQueryItem("fmt", v_fmt);
- }
- QByteArray v_location = NetworkThreadRequest::doGet2(v_video_url);
- qDebug() << "[v_location]" << v_location;
-
- //[Return Video Stream Url]
- QUrl v_result;
- v_result.setEncodedUrl(v_location);
-
- return v_result;
- }
- *****/
-
- QString YtWeb::getVideoStreamMap(QString a_id, QMap<int, QUrl> &a_fmt_map)
- {
- //[Get Token & Stream Map]
- QString v_token = getTokenForVideoStreamEmbed(a_id, a_fmt_map);
- if(v_token == "")
- {
- v_token = getTokenForVideoStreamDetail(a_id, a_fmt_map);
- }
- qDebug() << "[v_token(getVideoStreamMap)]" << v_token;
- qDebug() << "[a_fmt_map.keys()]" << a_fmt_map.keys();
- return v_token;
- }
-
- void YtWeb::pageVideoPlay(QWebElement *a_webelem, QString a_id, T_VideoList *a_recList)
- {
- QWebElement v_webelem = m_webview->page()->mainFrame()->findFirstElement("#rec_place");
- a_webelem = &v_webelem;
-
- QWebElement v_vlc_button_place = m_webview->page()->mainFrame()->findFirstElement("#vlc_button_place");
-
- v_vlc_button_place.setInnerXml("");
-
- QString v_inner_html = "";
-
- QUrl v_stream_url;
- QMap<int, QUrl> v_fmt_map;
- QString v_token = getVideoStreamMap(a_id, v_fmt_map);
- int v_fmt = -1;
- if(false) v_fmt = 0;
- else if(v_fmt_map.contains(22)) v_fmt = 22;
- else if(v_fmt_map.contains(35)) v_fmt = 35;
- //else if(v_fmt_map.contains(18)) v_fmt = 18;
- //else if(v_fmt_map.contains(5)) v_fmt = 5;
- else if(v_fmt_map.contains(34)) v_fmt = 34;
- else if(v_fmt_map.contains(18)) v_fmt = 18;
- else if(v_fmt_map.contains(5)) v_fmt = 5;
- else if(v_fmt_map.contains(35)) v_fmt = 35;
- //else if(v_fmt_map.contains(18)) v_fmt = 18;
- else if(v_fmt_map.contains(5)) v_fmt = 5;
- if(v_fmt != -1)
- {
- v_stream_url = v_fmt_map.value(v_fmt);
- }
- if(v_fmt_map.size() > 0)
- {
- QString dirPath =
- QDesktopServices::storageLocation(
- QDesktopServices::DesktopLocation);
- QDir dir(dirPath);
- QString filePath = dir.filePath("saved.m3u");
- QFile file;
- file.setFileName(filePath);
- qDebug() << file.fileName();
- file.open(QIODevice::WriteOnly | QIODevice::Truncate);
- file.write("#EXTM3U\n");
- file.write("#\n");
- QList<int> v_fmt_keys = v_fmt_map.keys();
- for(int i=0; i<v_fmt_keys.size(); i++)
- {
- file.write(QString("#EXTINF:0,[id=%1 fmt=%2]\n").arg(a_id).arg(v_fmt_keys.at(i)).toUtf8().constData());
- file.write(QString("%1\n").arg(v_fmt_map.value(v_fmt_keys.at(i)).toString()).toUtf8().constData());
- }
- file.close();
- }
-
- qDebug() << "[v_fmt]" << v_fmt;
-
- if(v_stream_url.toString()=="")
- {
- qDebug() << "[v_fmt_map]" << v_fmt_map;
- }
- qDebug() << "[v_stream_url]" << v_stream_url;
- qDebug() << "[v_stream_url.toPercentEncoding()]" << QUrl::toPercentEncoding(v_stream_url.toString());
-
- /* VLC plugin
- http://www.videolan.org/doc/play-howto/en/ch04.html
- */
- if(v_stream_url.toString() != "")
- {
- QList<int> v_fmt_keys = v_fmt_map.keys();
- foreach(int v_fmt_key, v_fmt_keys)
- {
- v_inner_html += QString(
- "<button onclick=\"fn_vlc_player('#vlc_place', '%1', 'fmt=%2')\">%2</button>"
- )
- .arg(v_fmt_map.value(v_fmt_key).toString())
- .arg(v_fmt_key)
- ;
-
- }
- //"<button onclick=\"fn_vlc_player('#vlc_place', '%1')\">zzz</button>"
-
- qDebug() << v_inner_html;
- v_vlc_button_place.appendInside(v_inner_html);
- emit answer_vlc_player(v_stream_url.toString(), QString("fmt=%1").arg(v_fmt));
- //emit answer_yt_player(a_id, a_id);
- }
-
- if(!a_recList) return;
- v_inner_html = "";
- for (int i=0; i<a_recList->size(); i++)
- {
- QString v_id = a_recList->value(i)->id;
- QString v_title = a_recList->value(i)->title;
- v_title = Qt::escape(v_title);
- QString v_author = a_recList->value(i)->author;
- qint64 v_view_count = a_recList->value(i)->view_count;
- #if 0x1
- QString v_style = "";
- T_Video v_rec_fv;
- if(YtManager::dbVideoSelect(v_id, "fv_akmiyoshi", &v_rec_fv))
- {
- //v_style = "style='background-color:#ff9933;'";
- v_style = "style='background-color:silver;'";
- }
- #endif
- QString v_append =
- QString(
- "<div id='outer_%1' %5>"
- "<table border=0><tr>"
- " <td>"
- //"<image src='http://i.ytimg.com/vi/%1/2.jpg' width=120 height=90 "
- "<image src='%4' width=120 height=90 "
- "onclick='web1.submitIdToPlay(\"%1\");' "
- "/>"
- " </td>"
- " <td>"
- //" %2<br />"
- " <a href='http://www.youtube.com/watch?v=%1'>%2</a><br />"
- //" <button onclick='web1.submitIdToPlay(\"%1\");'>??</button>"
- " <button onclick='web1.submitIdToBrowse(\"%1\");'>??</button>"
- //" <a href='http://www.youtube.com/watch?v=%1'>??2</a>"
- " <a href='http://www.youtube.com/user/%6'>%6</a>"
- " <button onclick='fn_del_image(\"%1\");'>??</button>"
- " [%L3 ?]"
- " </td>"
- "</table>"
- "</div>"
- )
- .arg(v_id)
- .arg(v_title)
- .arg(v_view_count)
- .arg(YtWeb::getUrlForVideoIcon(v_id))
- .arg(v_style)
- .arg(v_author)
- ;
- //qDebug() << v_append;
-
- v_inner_html += v_append;
- }
- a_webelem->setInnerXml("");
- a_webelem->appendInside(v_inner_html);
-
- }
-