int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QJSEngine engine; //! [0] engine.globalObject().setProperty("foo", 123); qDebug() << "foo times two is:" << engine.evaluate("foo * 2").toNumber(); //! [0] return 0; }
QJSValue JSKitXMLHttpRequest::response() const { QJSEngine *engine = _mgr->engine(); if (_responseType.isEmpty() || _responseType == "text") { return engine->toScriptValue(QString::fromUtf8(_response)); } else if (_responseType == "arraybuffer") { QJSValue arrayBufferProto = engine->globalObject().property("ArrayBuffer").property("prototype"); QJSValue arrayBuf = engine->newObject(); if (!arrayBufferProto.isUndefined()) { arrayBuf.setPrototype(arrayBufferProto); arrayBuf.setProperty("byteLength", engine->toScriptValue<uint>(_response.size())); QJSValue array = engine->newArray(_response.size()); for (int i = 0; i < _response.size(); i++) { array.setProperty(i, engine->toScriptValue<int>(_response[i])); } arrayBuf.setProperty("_bytes", array); qCDebug(l) << "returning ArrayBuffer of" << _response.size() << "bytes"; } else { qCWarning(l) << "Cannot find proto of ArrayBuffer"; } return arrayBuf; } else { qCWarning(l) << "unsupported responseType:" << _responseType; return engine->toScriptValue<void*>(0); } }
Authenticate::Authenticate(std::map<std::string, std::string> config, QObject *parent) : QObject(parent) { QSettings settings; authorized = settings.value("authorized").toStringList(); QJSEngine engine; QJSValue eventEngineValue = engine.newQObject(this); engine.globalObject().setProperty("auth", eventEngineValue); QString str = config["authSettings"].c_str(); QFile file(str); if(!file.open(QIODevice::ReadOnly)) { qDebug()<<"failed to open config file: "<<str; return; } QString script = file.readAll(); file.close(); engine.evaluate(script); }
QStringList VideoAnalyzer::analyze(QString pageUrl, int videoClarity) { qDebug()<< pageUrl; //目前使用flvsp.com解析 QEventLoop loop; QByteArray url = pageUrl.replace("//","##").toLatin1(); QFile scriptFile(":/base64.js"); scriptFile.open(QIODevice::ReadOnly); QTextStream out(&scriptFile); QString contents = out.readAll(); scriptFile.close(); QJSEngine myEngine; QJSValue fun = myEngine.evaluate(contents); QJSValueList args; args << QJSValue(QString(url)) ; QJSValue base64_encode = myEngine.globalObject().property("base64_encode"); QJSValue encodeResult = base64_encode.call(args).toString(); //qDebug()<<encodeResult.toString(); //qDebug()<<base64_encode(&a,strlen(&a)); QNetworkRequest request; request.setUrl(QUrl("http://www.flvsp.com/parse/getData.php?url="+encodeResult.toString())); request.setRawHeader("Accept","text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"); request.setRawHeader("Accept-Encoding","gzip, deflate, sdch"); request.setRawHeader("Referer","http://www.flvsp.com/?url=https://www.letv.com/ptv/vplay/22827477.html"); request.setRawHeader("X-Requested-With","XMLHttpRequest"); request.setRawHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 4.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 "); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); QString data = reply->readAll(); qDebug()<<"get flvsp data start"; QStringList videoClarityList = data.split("panel-heading"); if(videoClarityList.count()>2) { videoClarityList.removeFirst(); videoClarityList.removeFirst(); }else { return QStringList()<<"null"; } //搜寻符合清晰度的视频 int videoClarityIndex = 10-videoClarity;//flvsp的解析结果中排在前面的清晰度高 QString videos; for(int i=0;i<10;i++)//往清晰度低的方向查找合适的 { videos = videoClarityList.value(videoClarityIndex+i,""); if(videos.isEmpty()) { continue; }else { break; } } if(videos.isEmpty())//上述循环找不到,往清晰度高的方向找 { for(int i=0;i<10;i++) { videos = videoClarityList.value(videoClarityIndex-i,""); if(videos.isEmpty()) { continue; }else { break; } } } qDebug()<<"get flvsp data end"; //得到真实地址列表realUrlList QStringList realUrlList; QRegExp rx("class=\"file_url\" href=\".*\""); rx.setMinimal(true); int pos = 0; while ((pos = rx.indexIn(videos, pos)) != -1) { QString href = rx.cap(0); realUrlList.append(href.replace(QRegExp("class=\"file_url\" href=\"|\""),"")); pos += rx.matchedLength(); } qDebug()<<realUrlList; return realUrlList; }