Download videos from youtube - help.
-
[quote author="Volker" date="1294357703"]
And being QObject based, it is always a good idea to create QNetworkCookieJar on the heap.Also, the object you created on the stack might go out of scope (and be deleted) before QNetworkAccessManager accesses it.
[/quote]
Volker: good catch! But I am not sure this is the only problem. I would expect a seg fault...
Skycrusher: once you are taking the time to write this part as a Qt application why don't you rewrite the python script as well? It may actually make the overall process easier and certainly more portable.
-
If you want to a little crazier you can check out "cuteTube":http://talk.maemo.org/showthread.php?t=65854
I haven't tried and don't know how it does but it may actually be as simple as recompiling it for your platform.
-
fcrochik: This "cutetube" is awesome, but I want a program a little different than that. :)
I'm trying to make something like a "virtual video jukebox", but thanks anyway. ;)
I want this program the be Qt-based because I think with Qt/C++ it is easier to make GUIs.Volker:
[quote author="Volker" date="1294357703"]It looks like you create the QNetworkCookieJar object on the stack, because you get the pointer with the & operator:@
manager->setCookieJar(&cookie_JAR)
@If you do so, it is wrong. You have to create the cookie jar on the heap with new.
[/quote]
I just did the changes you told me. And the problem remains.
Let me show you the code:@
void Script_Youtube_Downloader::do_download()
{
qDebug("do_download");QNetworkAccessManager * manager = new QNetworkAccessManager(); QNetworkCookieJar * cookie_JAR = new QNetworkCookieJar(); QUrl url; QUrl passed_url; QList<QNetworkCookie> cookies; QNetworkRequest* request = new QNetworkRequest(); cookies = this->parse_my_cookiejar(this->get_cookie_info()); passed_url.setUrl(this->get_passed_url(), QUrl::StrictMode); url.setUrl(this->get_real_url(), QUrl::StrictMode); request->setUrl(url); request->setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101028 Firefox/3.6.12"); request->setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); request->setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request->setRawHeader("Accept-Encoding", "gzip, deflate"); request->setRawHeader("Accept-Language", "en-us,en;q=0.5"); if (cookie_JAR->setCookiesFromUrl(cookies, url)) qDebug("cookie JAr OK!\n"); else qDebug("COOKIE_JAR_ERROR\n"); manager->setCookieJar(cookie_JAR); if (this->get_cookie_info().isEmpty()) qDebug("EMPTY cookie jar from python"); request->setUrl(url); reply = manager->get(*request); //"reply" is a public QNetworkReply attribute of this class reply->waitForReadyRead(-1); qDebug("Is it any redirection address?"); qDebug(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString().toUtf8()); connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(printProgress(qint64, qint64))); connect(reply, SIGNAL(readyRead()), this, SLOT(printOk())); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(videoDownloaded(QNetworkReply*))); connect(reply, SIGNAL(readChannelFinished()), this, SLOT(printOk()) ); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(printNotOk(QNetworkReply::NetworkError)));
}
@And the program debug outuput is:
@
/*
do_download
Is it any redirection address?An error HAS SERIOUSLY OCCURED: 202
Redirection URL is EMPTY...
And error occured while I was trying to download
Http attribute:
HttpStatusCode-->403
*/
@ -
Fcrochik,
Just removed the "waitForReadyRead" command.
The error continues.
Actually, 202 is an error from QNetworkReply (it means that the requested operation was denied by the server). Look what I wrote in the "printnotOk" slot:
@
void Script_Youtube_Downloader::printNotOk(QNetworkReply::NetworkError error)
{
qDebug("An error HAS SERIOUSLY OCCURED: " + QString::number(error).toUtf8());
}
@and error 403 is an error from the Http protocol (it means that the operation was denied by the server).
Look the "videoDownloaded" SLOT implementation:
@
void Script_Youtube_Downloader::videoDownloaded(QNetworkReply* reply)
{
qDebug("received "finished" SIGNAL.\n");QUrl url; QUrl redir; redir = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); url = reply->url(); if(!url.isValid()) qDebug("Reply's url is not valid! Review your code."); if (redir.isEmpty()) { qDebug("Redirection URL is EMPTY..."); } if (reply->error()) { qDebug("And error occured while I was trying to download"); qDebug("Http attribute:"); qDebug("HttpStatusCode-->%d", reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()); } else { if (saveToDisk("SALVA_DIABO!!!", reply)) qDebug("file saved to: SALVA_DIABO" ); }
}
@ -
There is a nice technique that you can use to explore what is going on. Instead of using the connection manager use a QWebView. You can get access to the networkmanager set the cookiejar and then use the webview to navigate to the URL. This will let webkit in charge of all the request handling and you will be able to verify that the cookie/request handling is working.
p.s. Gostei do "Salva diabo"! (Portuguese)
-
I am having almost the same problem. I am decrypting the real URL of youtube video myself, using the same way as JDownloader, as follows:
-
Use QNetworkAccessManager to access the HTML content of the youtube URL, say: www.youtube.com/watch?v=tBF6MA0xfps
-
Analyze HTML and construct the video's real URL (a very long URL).
After the above two steps, I could get the video's real URL as well QNetworkCookieJar from the QNetworkAccessManager I used.
However, I got ERROR 202 from QNetworkReply when try to download from the real URL using the same QNetworkAccessManager and the same QNetworkCookieJar.
The decrypted URL could be directly played by Chrome, Safari, VLC, or downloaded by wget, curl, and libcurl. This means that accessing the decrypted URL does not require cookie magic.
I tried using the same or different QNetworkAccessManager with the same or different QNetworkCookieJar to access the decrypted URL. But I always got error 202.
There must be some bugs in QNetworkAccessManager.I also tried Qt 4.8.1 on Windows 7, and Qt 4.7.4 on Mac OS X 10.7, but got the same error 202. I think the bug is probably in Qt's OS-independent code path.
-