1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 | Copyright (C) 2010 Ori Rejwan
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program; if not, write to the Free Software
|
---|
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "retrieveyoutubeurl.h"
|
---|
21 | #include <QUrl>
|
---|
22 |
|
---|
23 | RetrieveYoutubeUrl::RetrieveYoutubeUrl( QObject* parent ) : SimpleHttp(parent)
|
---|
24 | {
|
---|
25 | connect(this, SIGNAL(downloadFinished(QByteArray)),
|
---|
26 | this, SLOT(parse(QByteArray)));
|
---|
27 |
|
---|
28 | preferred_quality = FLV_360p;
|
---|
29 |
|
---|
30 | setUserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
|
---|
31 | }
|
---|
32 |
|
---|
33 | RetrieveYoutubeUrl::~RetrieveYoutubeUrl() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | void RetrieveYoutubeUrl::fetchPage(const QString & url) {
|
---|
37 | download(url);
|
---|
38 | orig_url = url;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void RetrieveYoutubeUrl::parse(QByteArray text) {
|
---|
42 | qDebug("RetrieveYoutubeUrl::parse");
|
---|
43 |
|
---|
44 | urlMap.clear();
|
---|
45 |
|
---|
46 | QString replyString = QString::fromUtf8(text.constData(), text.size());
|
---|
47 |
|
---|
48 | QRegExp rx_title(".*<title>(.*)</title>.*");
|
---|
49 | if (rx_title.indexIn(replyString) != -1) {
|
---|
50 | url_title = rx_title.cap(1).simplified();
|
---|
51 | url_title = QString(url_title).replace("&","&").replace(">", ">").replace("<", "<").replace(""","\"").replace("'","'")/*.replace(" - YouTube", "")*/;
|
---|
52 | qDebug("RetrieveYoutubeUrl::parse: title '%s'", url_title.toUtf8().constData());
|
---|
53 | } else {
|
---|
54 | url_title = "Youtube video";
|
---|
55 | }
|
---|
56 |
|
---|
57 | QRegExp regex("\\\"url_encoded_fmt_stream_map\\\"\\s*:\\s*\\\"([^\\\"]*)");
|
---|
58 | regex.indexIn(replyString);
|
---|
59 | QString fmtArray = regex.cap(1);
|
---|
60 | fmtArray = sanitizeForUnicodePoint(fmtArray);
|
---|
61 | fmtArray.replace(QRegExp("\\\\(.)"), "\\1");
|
---|
62 | htmlDecode(fmtArray);
|
---|
63 | QStringList codeList = fmtArray.split(',');
|
---|
64 | QStringList::iterator stIt = codeList.begin();
|
---|
65 | foreach(QString code, codeList)
|
---|
66 | {
|
---|
67 | code.remove(0, 4);
|
---|
68 | QUrl url(code);
|
---|
69 | urlMap[url.queryItemValue("itag").toInt()] = code.remove(QRegExp("&itag=\\d+$"));
|
---|
70 | }
|
---|
71 |
|
---|
72 | qDebug("RetrieveYoutubeUrl::parse: url count: %d", urlMap.count());
|
---|
73 |
|
---|
74 | QString p_url = findPreferredUrl();
|
---|
75 | if (!p_url.isNull()) {
|
---|
76 | emit gotUrls(urlMap);
|
---|
77 | emit gotPreferredUrl(p_url);
|
---|
78 | } else {
|
---|
79 | emit gotEmptyList();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | QString RetrieveYoutubeUrl::findPreferredUrl() {
|
---|
84 | latest_preferred_url = findPreferredUrl(urlMap, preferred_quality);
|
---|
85 | return latest_preferred_url;
|
---|
86 | }
|
---|
87 |
|
---|
88 | QString RetrieveYoutubeUrl::findPreferredUrl(const QMap<int, QString>& urlMap, Quality q) {
|
---|
89 | // Choose a url according to preferred quality
|
---|
90 | QString p_url;
|
---|
91 | //Quality q = preferred_quality;
|
---|
92 |
|
---|
93 | if (q==MP4_1080p) {
|
---|
94 | p_url = urlMap.value(MP4_1080p, QString());
|
---|
95 | if (p_url.isNull()) p_url= urlMap.value(WEBM_1080p, QString());
|
---|
96 | if (p_url.isNull()) q = MP4_720p;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (q==WEBM_1080p) {
|
---|
100 | p_url = urlMap.value(WEBM_1080p, QString());
|
---|
101 | if (p_url.isNull()) p_url= urlMap.value(MP4_1080p, QString());
|
---|
102 | if (p_url.isNull()) q = WEBM_720p;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (q==MP4_720p) {
|
---|
106 | p_url = urlMap.value(MP4_720p, QString());
|
---|
107 | if (p_url.isNull()) p_url= urlMap.value(WEBM_720p, QString());
|
---|
108 | if (p_url.isNull()) p_url = urlMap.value(WEBM_480p, QString());
|
---|
109 | if (p_url.isNull()) q = MP4_360p;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (q==WEBM_720p) {
|
---|
113 | p_url = urlMap.value(WEBM_720p, QString());
|
---|
114 | if (p_url.isNull()) p_url= urlMap.value(MP4_720p, QString());
|
---|
115 | if (p_url.isNull()) q = WEBM_480p;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (q==WEBM_480p) {
|
---|
119 | p_url = urlMap.value(WEBM_480p, QString());
|
---|
120 | if (p_url.isNull()) q = WEBM_360p;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (q==MP4_360p) {
|
---|
124 | p_url = urlMap.value(MP4_360p, QString());
|
---|
125 | if (p_url.isNull()) p_url= urlMap.value(WEBM_360p, QString());
|
---|
126 | if (p_url.isNull()) q = FLV_360p;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (q==WEBM_360p) {
|
---|
130 | p_url = urlMap.value(WEBM_360p, QString());
|
---|
131 | if (p_url.isNull()) p_url= urlMap.value(MP4_360p, QString());
|
---|
132 | if (p_url.isNull()) q = FLV_360p;
|
---|
133 | }
|
---|
134 |
|
---|
135 | // FLV, low priority
|
---|
136 | if (q==FLV_480p) {
|
---|
137 | p_url = urlMap.value(FLV_480p, QString());
|
---|
138 | if (p_url.isNull()) q = FLV_360p;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (q==FLV_360p) {
|
---|
142 | p_url = urlMap.value(FLV_360p, QString());
|
---|
143 | if (p_url.isNull()) q = FLV_240p;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (q==FLV_240p) {
|
---|
147 | p_url = urlMap.value(q, QString());
|
---|
148 | }
|
---|
149 |
|
---|
150 | return p_url;
|
---|
151 | }
|
---|
152 |
|
---|
153 | QString RetrieveYoutubeUrl::sanitizeForUnicodePoint(QString string)
|
---|
154 | {
|
---|
155 | QRegExp rx("\\\\u(\\d{4})");
|
---|
156 | while (rx.indexIn(string) != -1) {
|
---|
157 | string.replace(rx.cap(0), QString(QChar(rx.cap(1).toInt(0,16))));
|
---|
158 | }
|
---|
159 | return string;
|
---|
160 | }
|
---|
161 |
|
---|
162 | void RetrieveYoutubeUrl::htmlDecode(QString& string)
|
---|
163 | {
|
---|
164 | string.replace("%3A", ":", Qt::CaseInsensitive);
|
---|
165 | string.replace("%2F", "/", Qt::CaseInsensitive);
|
---|
166 | string.replace("%3F", "?", Qt::CaseInsensitive);
|
---|
167 | string.replace("%3D", "=", Qt::CaseInsensitive);
|
---|
168 | string.replace("%25", "%", Qt::CaseInsensitive);
|
---|
169 | string.replace("%26", "&", Qt::CaseInsensitive);
|
---|
170 | string.replace("%3D", "=", Qt::CaseInsensitive);
|
---|
171 | }
|
---|
172 |
|
---|
173 | #include "moc_retrieveyoutubeurl.cpp"
|
---|