Skip to content

Commit

Permalink
feat(trans): Change network requester to curl
Browse files Browse the repository at this point in the history
Signed-off-by: Hakusai Zhang <xm1994@gmail.com>
  • Loading branch information
summershrimp committed Apr 14, 2024
1 parent 84021e0 commit 68d7a96
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 27 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ endif()
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OpenSSL::Crypto OpenSSL::SSL websocketpp::websocketpp cpr::cpr)

target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE ASIO_STANDALONE)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/src/util/QWebsocketpp")
target_include_directories(${CMAKE_PROJECT_NAME}
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/src/util/QWebsocketpp"
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/src/util/QCpr")

set(obs-auto-subtitle_SOURCES
src/obs-auto-subtitle.cpp
src/obs-autosub-filter.cpp
src/util/QWebsocketpp/QWebsocketpp.cpp
src/util/QCpr/QCprManager.cpp
src/builder/ASR/AliNLSBuilder.cpp
src/builder/ASR/HwCloudRASRBuilder.cpp
src/builder/ASR/XFRtASRBuilder.cpp
Expand Down
2 changes: 1 addition & 1 deletion build-aux/.run-format.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ invoke_formatter() {
for file (${source_files}) {
if (( _loglevel > 1 )) log_info "Checking format of ${file}..."

if ! "${formatter}" ${format_args} "${file}" | diff -q "${file}" - &> /dev/null; then
if ! "${formatter}" ${format_args} "${file}" | diff -u "${file}" - ; then
log_error "${file} requires formatting changes."

if (( fail_on_error == 2 )) return 2;
Expand Down
19 changes: 19 additions & 0 deletions src/util/QCpr/QCprManager
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
QCprManager
Copyright (C) 2019-2024 Yibai Zhang

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; If not, see <https://www.gnu.org/licenses/>
*/

#include "QCprManager.h"
11 changes: 11 additions & 0 deletions src/util/QCpr/QCprManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "QCprManager.h"

QCprManager::QCprManager(cpr::AsyncResponse&& fr, QObject *parent): QThread(parent), fr(std::move(fr)) {
}


void QCprManager::run() {
fr.wait(); // This waits until the request is complete
cpr::Response r = fr.get(); // Since the request is complete, this returns immediately
emit resultReady(r);
}
23 changes: 23 additions & 0 deletions src/util/QCpr/QCprManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _QCPR_MANAGER_H_
#define _QCPR_MANAGER_H_

#include <QThread>
#include <cpr/cpr.h>


class QCprManager: public QThread {
Q_OBJECT

public:
QCprManager(cpr::AsyncResponse&& fr, QObject *parent = nullptr);

private:
void run() override;
cpr::AsyncResponse fr;

signals:
void resultReady(const cpr::Response r);
};

#endif

22 changes: 12 additions & 10 deletions src/vendor/Trans/GScriptTrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,39 @@ You should have received a copy of the GNU General Public License
along with this program; If not, see <https://www.gnu.org/licenses/>
*/

#include <QtNetwork>
#include "GScriptTrans.h"

GScriptTrans::GScriptTrans(QString deployId, QObject *parent)
: TransBase(parent), deployId(deployId)
{
connect(&manager, &QNetworkAccessManager::finished, this,
&GScriptTrans::onResult);
manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
}

GScriptTrans::~GScriptTrans() {}

void GScriptTrans::onRequestTranslate(QString from, QString to, QString content)
{
QNetworkRequest req;
QUrl url;
QUrlQuery query;
query.addQueryItem("from", from);
query.addQueryItem("to", to);
query.addQueryItem("content", content);
url.setUrl(QString(GS_ENDPOINT_URL).arg(deployId));
url.setQuery(query);
req.setUrl(url);
manager.get(req);


auto fr = cpr::GetAsync(cpr::Url{url.toString().toStdString()});
QCprManager *cprm = new QCprManager(std::move(fr));
connect(cprm, &QCprManager::resultReady, this, &GScriptTrans::onResult);
connect(cprm, &QCprManager::finished, cprm, &QCprManager::deleteLater);
cprm->start();
}

void GScriptTrans::onResult(QNetworkReply *rep)
void GScriptTrans::onResult(cpr::Response rep)
{
QByteArray content = rep->readAll();
QJsonDocument body;
body = QJsonDocument::fromJson(content);
QString reptxt = QString::fromStdString(rep.text);
body = QJsonDocument::fromJson(reptxt.toLocal8Bit());

if (!body.isObject()) {
callbackError("Parse response json failed");
Expand All @@ -56,7 +58,7 @@ void GScriptTrans::onResult(QNetworkReply *rep)
auto iter = bodyObject.find("result");

if (iter == bodyObject.end() || !iter.value().isString()) {
callbackError("reply error: " + content);
callbackError("reply error: " + reptxt);
return;
}
auto data = iter.value().toString();
Expand Down
5 changes: 2 additions & 3 deletions src/vendor/Trans/GScriptTrans.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ along with this program; If not, see <https://www.gnu.org/licenses/>

#define GS_ENDPOINT_URL "https://script.google.com/macros/s/%1/exec"

#include <QtNetwork>
#include <QCprManager>
#include "TransBase.h"

#define TRANS_ENDPOINT "https://itrans.xfyun.cn/v2/its"
Expand All @@ -32,12 +32,11 @@ class GScriptTrans : public TransBase {
GScriptTrans(QString deployId, QObject *parent = nullptr);
~GScriptTrans();
public slots:
void onResult(QNetworkReply *rep);
void onResult(cpr::Response rep);
void onRequestTranslate(QString, QString, QString);

private:
QString deployId;
QNetworkAccessManager manager;
};

#endif
26 changes: 16 additions & 10 deletions src/vendor/Trans/XFTrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with this program; If not, see <https://www.gnu.org/licenses/>

#include <QCryptographicHash>
#include <QMessageAuthenticationCode>
#include <QtNetwork>

#include "XFTrans.h"

Expand All @@ -27,11 +28,8 @@ XFTrans::XFTrans(QString appId, QString apiKey, QString apiSecret,
appId(appId),
apiKey(apiKey),
apiSecret(apiSecret),
manager(this),
endpoint(TRANS_ENDPOINT)
{
connect(&manager, &QNetworkAccessManager::finished, this,
&XFTrans::onResult);
}

XFTrans::XFTrans(QString appId, QString apiKey, QString apiSecret,
Expand All @@ -40,18 +38,15 @@ XFTrans::XFTrans(QString appId, QString apiKey, QString apiSecret,
appId(appId),
apiKey(apiKey),
apiSecret(apiSecret),
manager(this),
endpoint(endpoint)
{
connect(&manager, &QNetworkAccessManager::finished, this,
&XFTrans::onResult);
}

XFTrans::~XFTrans() {}

void XFTrans::onResult(QNetworkReply *rep)
void XFTrans::onResult(cpr::Response rep)
{
QByteArray content = rep->readAll();
QByteArray content = QByteArray::fromStdString(rep.text);
QJsonDocument body;
body = QJsonDocument::fromJson(content);

Expand Down Expand Up @@ -111,7 +106,6 @@ void XFTrans::onResult(QNetworkReply *rep)
}
auto result = iter.value().toString();
callbackResult(result);
rep->deleteLater();
}

void XFTrans::onRequestTranslate(QString from, QString to, QString content)
Expand All @@ -130,7 +124,19 @@ void XFTrans::onRequestTranslate(QString from, QString to, QString content)
QByteArray httpBody = doc.toJson(QJsonDocument::JsonFormat::Compact);
QNetworkRequest req = assembleRequest(endpoint, httpBody);

manager.post(req, httpBody);
auto fr = cpr::PostAsync(cpr::Url{req.url().toString().toStdString()},
cpr::Body{httpBody.toStdString()},
cpr::Header{
{"Content-Type", "application/json"},
{"Date", req.rawHeader("Date").toStdString()},
{"Digest", req.rawHeader("Digest").toStdString()},
{"Authorization", req.rawHeader("Authorization").toStdString()}
});

QCprManager *cprm = new QCprManager(std::move(fr));
connect(cprm, &QCprManager::resultReady, this, &XFTrans::onResult);
connect(cprm, &QCprManager::finished, cprm, &QCprManager::deleteLater);
cprm->start();
}

QString XFTrans::signBody(QByteArray body)
Expand Down
4 changes: 2 additions & 2 deletions src/vendor/Trans/XFTrans.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program; If not, see <https://www.gnu.org/licenses/>
#ifndef OBS_AUTO_SUBTITLE_XFTRANS_H
#define OBS_AUTO_SUBTITLE_XFTRANS_H

#include <QCprManager>
#include <QtNetwork>
#include "TransBase.h"

Expand All @@ -33,7 +34,7 @@ class XFTrans : public TransBase {
QString endpoint, QObject *parent = nullptr);
~XFTrans();
public slots:
void onResult(QNetworkReply *rep);
void onResult(cpr::Response rep);
void onRequestTranslate(QString, QString, QString);

private:
Expand All @@ -44,7 +45,6 @@ public slots:
QString appId;
QString apiKey;
QString apiSecret;
QNetworkAccessManager manager;
QString endpoint;
};

Expand Down

0 comments on commit 68d7a96

Please sign in to comment.