lingua: handle network errors

* set timeout to 3s
* get rid of reply with errors
This commit is contained in:
shenleban tongying 2022-12-27 22:25:57 -05:00
parent caa97a7120
commit f277af1a2e

View file

@ -31,14 +31,10 @@ namespace {
/* map of iso lang code to wikipedia lang id /* map of iso lang code to wikipedia lang id
Data was obtained by this query on https://commons-query.wikimedia.org/ Data was obtained by this query on https://commons-query.wikimedia.org/
SELECT ?language ?languageLabel ?iso ?audios SELECT ?language ?languageLabel ?iso ?audios
WHERE { WHERE {
{ {
SELECT ?language (COUNT(?audio) AS ?audios) WHERE { SELECT ?language (COUNT(?audio) AS ?audios) WHERE {
# Comment out the below statement to filter to only certain languages (e.g. Q34 or others)
# VALUES ?language { entity:Q34 }
?audio # Filter: P2 'instance of' is Q2 'record' ?audio # Filter: P2 'instance of' is Q2 'record'
wdt:P407 ?language . wdt:P407 ?language .
} }
@ -57,6 +53,7 @@ namespace {
} }
} }
} }
*/ */
const map< string, string > iso_to_wikipedia_id = { { "grc", "Q35497" }, const map< string, string > iso_to_wikipedia_id = { { "grc", "Q35497" },
@ -313,13 +310,12 @@ namespace {
sptr< DataRequest > getArticle( sptr< DataRequest > getArticle(
wstring const & word, vector< wstring > const & alts, wstring const &, bool ) override wstring const & word, vector< wstring > const & alts, wstring const &, bool ) override
{ {
if( word.size() > 50 ) if( word.size() < 50 )
{
return std::make_shared< DataRequestInstant >( false );
}
else
{ {
return std::make_shared< LinguaArticleRequest >( word, alts, languageCode,langWikipediaID, getId(), netMgr ); return std::make_shared< LinguaArticleRequest >( word, alts, languageCode,langWikipediaID, getId(), netMgr );
} else {
return std::make_shared< DataRequestInstant >( false );
} }
} }
@ -334,7 +330,7 @@ namespace {
} }
}; };
} } // namespace
vector< sptr< Dictionary::Class > > makeDictionaries( vector< sptr< Dictionary::Class > > makeDictionaries(
Dictionary::Initializing &, Config::Lingua const & lingua, QNetworkAccessManager & mgr ) Dictionary::Initializing &, Config::Lingua const & lingua, QNetworkAccessManager & mgr )
@ -383,18 +379,20 @@ void LinguaArticleRequest::addQuery( QNetworkAccessManager & mgr, const wstring
R"(&iiprop=url)" R"(&iiprop=url)"
R"(&iimetadataversion=1)" R"(&iimetadataversion=1)"
R"(&iiextmetadatafilter=Categories)" R"(&iiextmetadatafilter=Categories)"
R"(&gsrsearch=intitle:LL-%1 \(%2\)-.*-%3\.wav/)" R"(&gsrsearch=intitle:LL-%1 \(%2\)-.*-%3\.wav/)" // https://en.wikipedia.org/wiki/Help:Searching/Regex
R"(&gsrnamespace=6)" R"(&gsrnamespace=6)"
R"(&gsrlimit=10)" R"(&gsrlimit=10)"
R"(&gsrwhat=text)"; R"(&gsrwhat=text)";
reqUrl = reqUrl.arg(languageCode,langWikipediaID,QString::fromStdU32String( word ) ); reqUrl = reqUrl.arg(langWikipediaID,languageCode,QString::fromStdU32String( word ) );
qDebug()<< "lingualibre query " << reqUrl; qDebug()<< "lingualibre query " << reqUrl;
auto netRequest = QNetworkRequest( reqUrl );
netRequest.setTransferTimeout(3000);
auto netReply = auto netReply =
std::shared_ptr< QNetworkReply >( mgr.get( std::shared_ptr< QNetworkReply >( mgr.get(netRequest));
QNetworkRequest( reqUrl ) ) );
netReplies.emplace_back( netReply, Utf8::encode( word ) ); netReplies.emplace_back( netReply, Utf8::encode( word ) );
@ -408,6 +406,13 @@ void LinguaArticleRequest::requestFinished( QNetworkReply * r )
sptr< QNetworkReply > netReply = netReplies.front().reply; sptr< QNetworkReply > netReply = netReplies.front().reply;
if( isFinished() || // Was cancelled
!netReply->isFinished() ||
netReply->error() != QNetworkReply::NoError ){
qWarning()<< "Lingua query failed: " << netReply->error();
return;
}
QJsonObject resultJson = QJsonDocument::fromJson( netReply->readAll() ).object(); QJsonObject resultJson = QJsonDocument::fromJson( netReply->readAll() ).object();
/* /*
@ -488,6 +493,7 @@ void LinguaArticleRequest::requestFinished( QNetworkReply * r )
else else
{ {
hasAnyData = false; hasAnyData = false;
finish();
} }
} }