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 |
|
---|
31 | RetrieveYoutubeUrl::~RetrieveYoutubeUrl() {
|
---|
32 | }
|
---|
33 |
|
---|
34 | void RetrieveYoutubeUrl::fetchPage(const QString & url) {
|
---|
35 | download(url);
|
---|
36 | orig_url = url;
|
---|
37 | }
|
---|
38 |
|
---|
39 | void RetrieveYoutubeUrl::parse(QByteArray text) {
|
---|
40 | qDebug("RetrieveYoutubeUrl::parse");
|
---|
41 |
|
---|
42 | urlMap.clear();
|
---|
43 |
|
---|
44 | QString replyString = QString::fromUtf8(text.constData(), text.size());
|
---|
45 |
|
---|
46 | QRegExp rx_title(".*<title>(.*)</title>.*");
|
---|
47 | if (rx_title.indexIn(replyString) != -1) {
|
---|
48 | url_title = rx_title.cap(1).simplified();
|
---|
49 | url_title = QString(url_title).replace("&","&").replace(">", ">").replace("<", "<").replace(""","\"").replace("'","'")/*.replace(" - YouTube", "")*/;
|
---|
50 | qDebug("RetrieveYoutubeUrl::parse: title '%s'", url_title.toUtf8().constData());
|
---|
51 | } else {
|
---|
52 | url_title = "Youtube video";
|
---|
53 | }
|
---|
54 |
|
---|
55 | QRegExp regex("\\\"url_encoded_fmt_stream_map\\\"\\s*:\\s*\\\"([^\\\"]*)");
|
---|
56 | regex.indexIn(replyString);
|
---|
57 | QString fmtArray = regex.cap(1);
|
---|
58 | fmtArray = sanitizeForUnicodePoint(fmtArray);
|
---|
59 | fmtArray.replace(QRegExp("\\\\(.)"), "\\1");
|
---|
60 | htmlDecode(fmtArray);
|
---|
61 | QStringList codeList = fmtArray.split(',');
|
---|
62 | QStringList::iterator stIt = codeList.begin();
|
---|
63 | foreach(QString code, codeList)
|
---|
64 | {
|
---|
65 | QUrl url(code);
|
---|
66 | int itag = url.queryItemValue("itag").toInt();
|
---|
67 | //qDebug("itag: %d", itag);
|
---|
68 | code.remove(QRegExp("itag=(\\d+)&url="));
|
---|
69 | code.replace("&sig=", "&signature=");
|
---|
70 | urlMap[itag] = code;
|
---|
71 | //qDebug("code: '%s'", code.toUtf8().constData());
|
---|
72 | }
|
---|
73 |
|
---|
74 | qDebug("RetrieveYoutubeUrl::parse: url count: %d", urlMap.count());
|
---|
75 |
|
---|
76 | QString p_url = findPreferredUrl();
|
---|
77 | if (!p_url.isNull()) {
|
---|
78 | emit gotUrls(urlMap);
|
---|
79 | emit gotPreferredUrl(p_url);
|
---|
80 | } else {
|
---|
81 | emit gotEmptyList();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | QString RetrieveYoutubeUrl::findPreferredUrl() {
|
---|
86 | latest_preferred_url = findPreferredUrl(urlMap, preferred_quality);
|
---|
87 | return latest_preferred_url;
|
---|
88 | }
|
---|
89 |
|
---|
90 | QString RetrieveYoutubeUrl::findPreferredUrl(const QMap<int, QString>& urlMap, Quality q) {
|
---|
91 | // Choose a url according to preferred quality
|
---|
92 | QString p_url;
|
---|
93 | //Quality q = preferred_quality;
|
---|
94 |
|
---|
95 | if (q==MP4_1080p) {
|
---|
96 | p_url = urlMap.value(MP4_1080p, QString());
|
---|
97 | if (p_url.isNull()) p_url= urlMap.value(WEBM_1080p, QString());
|
---|
98 | if (p_url.isNull()) q = MP4_720p;
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (q==WEBM_1080p) {
|
---|
102 | p_url = urlMap.value(WEBM_1080p, QString());
|
---|
103 | if (p_url.isNull()) p_url= urlMap.value(MP4_1080p, QString());
|
---|
104 | if (p_url.isNull()) q = WEBM_720p;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (q==MP4_720p) {
|
---|
108 | p_url = urlMap.value(MP4_720p, QString());
|
---|
109 | if (p_url.isNull()) p_url= urlMap.value(WEBM_720p, QString());
|
---|
110 | if (p_url.isNull()) p_url = urlMap.value(WEBM_480p, QString());
|
---|
111 | if (p_url.isNull()) q = MP4_360p;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (q==WEBM_720p) {
|
---|
115 | p_url = urlMap.value(WEBM_720p, QString());
|
---|
116 | if (p_url.isNull()) p_url= urlMap.value(MP4_720p, QString());
|
---|
117 | if (p_url.isNull()) q = WEBM_480p;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (q==WEBM_480p) {
|
---|
121 | p_url = urlMap.value(WEBM_480p, QString());
|
---|
122 | if (p_url.isNull()) q = WEBM_360p;
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (q==MP4_360p) {
|
---|
126 | p_url = urlMap.value(MP4_360p, QString());
|
---|
127 | if (p_url.isNull()) p_url= urlMap.value(WEBM_360p, QString());
|
---|
128 | if (p_url.isNull()) q = FLV_360p;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (q==WEBM_360p) {
|
---|
132 | p_url = urlMap.value(WEBM_360p, QString());
|
---|
133 | if (p_url.isNull()) p_url= urlMap.value(MP4_360p, QString());
|
---|
134 | if (p_url.isNull()) q = FLV_360p;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // FLV, low priority
|
---|
138 | if (q==FLV_480p) {
|
---|
139 | p_url = urlMap.value(FLV_480p, QString());
|
---|
140 | if (p_url.isNull()) q = FLV_360p;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (q==FLV_360p) {
|
---|
144 | p_url = urlMap.value(FLV_360p, QString());
|
---|
145 | if (p_url.isNull()) q = FLV_240p;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (q==FLV_240p) {
|
---|
149 | p_url = urlMap.value(q, QString());
|
---|
150 | }
|
---|
151 |
|
---|
152 | return p_url;
|
---|
153 | }
|
---|
154 |
|
---|
155 | QString RetrieveYoutubeUrl::sanitizeForUnicodePoint(QString string)
|
---|
156 | {
|
---|
157 | QRegExp rx("\\\\u(\\d{4})");
|
---|
158 | while (rx.indexIn(string) != -1) {
|
---|
159 | string.replace(rx.cap(0), QString(QChar(rx.cap(1).toInt(0,16))));
|
---|
160 | }
|
---|
161 | return string;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void RetrieveYoutubeUrl::htmlDecode(QString& string)
|
---|
165 | {
|
---|
166 | string.replace("%3A", ":", Qt::CaseInsensitive);
|
---|
167 | string.replace("%2F", "/", Qt::CaseInsensitive);
|
---|
168 | string.replace("%3F", "?", Qt::CaseInsensitive);
|
---|
169 | string.replace("%3D", "=", Qt::CaseInsensitive);
|
---|
170 | string.replace("%25", "%", Qt::CaseInsensitive);
|
---|
171 | string.replace("%26", "&", Qt::CaseInsensitive);
|
---|
172 | string.replace("%3D", "=", Qt::CaseInsensitive);
|
---|
173 | }
|
---|
174 |
|
---|
175 | #include "moc_retrieveyoutubeurl.cpp"
|
---|