Added code to retrieve json responses from wordreference. WiP
This commit is contained in:
parent
95d9b7ca09
commit
89c6c15f1c
3 changed files with 85 additions and 15 deletions
|
@ -20,8 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#include "WordReference.hpp"
|
||||
#include <sstream>
|
||||
#include <boost/algorithm/string/trim_all.hpp>
|
||||
#include <curl/curl.h>
|
||||
#include "libjson.h"
|
||||
|
||||
namespace {
|
||||
const char* ApiVersion = "0.8";
|
||||
|
||||
typedef std::map<std::string, std::string> LanguageMapType;
|
||||
const LanguageMapType SupportedLanguages {
|
||||
std::pair<std::string, std::string>("ar", "Arabic"),
|
||||
|
@ -42,11 +46,62 @@ namespace {
|
|||
|
||||
std::string GetCleanWord (std::string parWord) __attribute__((pure));
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
std::string GetCleanWord (std::string parWord) {
|
||||
boost::algorithm::trim_all(parWord);
|
||||
std::replace(parWord.begin(), parWord.end(), ' ', '+');
|
||||
return parWord;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
void DownloadHttpPage (const std::string& parAddress, std::string& parDest) {
|
||||
struct CurlAuto {
|
||||
CurlAuto ( void ) : curl(curl_easy_init()) {}
|
||||
~CurlAuto ( void ) { if (curl) curl_easy_cleanup(curl); }
|
||||
CURL* const curl;
|
||||
};
|
||||
|
||||
CurlAuto curl;
|
||||
if (curl.curl) {
|
||||
curl_easy_setopt(curl.curl, CURLOPT_URL, parAddress.c_str());
|
||||
//Tell curl to follow redirection when needed
|
||||
curl_easy_setopt(curl.curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
|
||||
const CURLcode res = curl_easy_perform(curl.curl);
|
||||
if (CURLE_OK == res) {
|
||||
char* reading;
|
||||
const CURLcode res = curl_easy_getinfo(curl.curl, CURLINFO_CONTENT_TYPE, &reading);
|
||||
if (CURLE_OK != res or not reading) {
|
||||
std::ostringstream oss;
|
||||
oss << "Error while requesting http page: " << curl_easy_strerror(res);
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
parDest = reading;
|
||||
}
|
||||
else {
|
||||
std::ostringstream oss;
|
||||
oss << "Error during the CURL request: " << curl_easy_strerror(res);
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Error initializing CURL");
|
||||
}
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
JSONNode QueryJSon (const std::string& parFrom, const std::string& parTo, const std::string& parKey, const std::string& parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://api.wordreference.com/" << ApiVersion << "/"
|
||||
<< parKey << "/json/" << parFrom << parTo << "/" << GetCleanWord(parWord);
|
||||
|
||||
std::string jsonResponse;
|
||||
DownloadHttpPage(oss.str(), jsonResponse);
|
||||
return libjson::parse(libjson::to_json_string(jsonResponse));
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
|
@ -77,9 +132,10 @@ ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) :
|
|||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
WordReference::WordReference (const char* parFrom, const char* parTo) :
|
||||
WordReference::WordReference (const char* parFrom, const char* parTo, const char* parApiKey) :
|
||||
m_langFrom(parFrom),
|
||||
m_langTo(parTo)
|
||||
m_langTo(parTo),
|
||||
m_apiKey(parApiKey)
|
||||
{
|
||||
if (SupportedLanguages.find(parFrom) == SupportedLanguages.end()) {
|
||||
std::ostringstream oss;
|
||||
|
@ -96,11 +152,16 @@ WordReference::WordReference (const char* parFrom, const char* parTo) :
|
|||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
|
||||
if (WordRefLangFrom == parDir)
|
||||
return m_langFrom;
|
||||
else
|
||||
return m_langTo;
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
const std::string& WordReference::GetLanguageName (WordReferenceLangDirection parDir) const {
|
||||
return SupportedLanguages.at(GetLanguageCode(parDir));
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
|
@ -120,3 +181,15 @@ std::string WordReference::GetHttpLink (const std::string& parWord) {
|
|||
oss << GetCleanWord(parWord) << "&dict=" << m_langFrom << m_langTo;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetApiVersion() {
|
||||
return std::string(ApiVersion);
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
void WordReference::Translate (const std::string& parWord) {
|
||||
JSONNode root = QueryJSon(m_langFrom, m_langTo, m_apiKey, parWord);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue