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 "WordReference.hpp"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <boost/algorithm/string/trim_all.hpp>
|
#include <boost/algorithm/string/trim_all.hpp>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include "libjson.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
const char* ApiVersion = "0.8";
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> LanguageMapType;
|
typedef std::map<std::string, std::string> LanguageMapType;
|
||||||
const LanguageMapType SupportedLanguages {
|
const LanguageMapType SupportedLanguages {
|
||||||
std::pair<std::string, std::string>("ar", "Arabic"),
|
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) __attribute__((pure));
|
||||||
|
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
std::string GetCleanWord (std::string parWord) {
|
std::string GetCleanWord (std::string parWord) {
|
||||||
boost::algorithm::trim_all(parWord);
|
boost::algorithm::trim_all(parWord);
|
||||||
std::replace(parWord.begin(), parWord.end(), ' ', '+');
|
std::replace(parWord.begin(), parWord.end(), ' ', '+');
|
||||||
return parWord;
|
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
|
} //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_langFrom(parFrom),
|
||||||
m_langTo(parTo)
|
m_langTo(parTo),
|
||||||
|
m_apiKey(parApiKey)
|
||||||
{
|
{
|
||||||
if (SupportedLanguages.find(parFrom) == SupportedLanguages.end()) {
|
if (SupportedLanguages.find(parFrom) == SupportedLanguages.end()) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
@ -96,11 +152,16 @@ WordReference::WordReference (const char* parFrom, const char* parTo) :
|
||||||
///-----------------------------------------------------------------------------
|
///-----------------------------------------------------------------------------
|
||||||
///-----------------------------------------------------------------------------
|
///-----------------------------------------------------------------------------
|
||||||
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
|
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 {
|
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;
|
oss << GetCleanWord(parWord) << "&dict=" << m_langFrom << m_langTo;
|
||||||
return oss.str();
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -34,17 +34,20 @@ public:
|
||||||
|
|
||||||
class WordReference {
|
class WordReference {
|
||||||
public:
|
public:
|
||||||
WordReference ( const char* parFrom, const char* parTo );
|
WordReference ( const char* parFrom, const char* parTo, const char* parApiKey );
|
||||||
~WordReference ( void ) = default;
|
~WordReference ( void ) = default;
|
||||||
|
|
||||||
const std::string& GetLanguageCode ( WordReferenceLangDirection parDir ) const;
|
const std::string& GetLanguageCode ( WordReferenceLangDirection parDir ) const;
|
||||||
const std::string& GetLanguageName ( WordReferenceLangDirection parDir ) const;
|
const std::string& GetLanguageName ( WordReferenceLangDirection parDir ) const;
|
||||||
std::string GetHttpLink ( const char* parWord );
|
std::string GetHttpLink ( const char* parWord );
|
||||||
std::string GetHttpLink ( const std::string& parWord );
|
std::string GetHttpLink ( const std::string& parWord );
|
||||||
|
static std::string GetApiVersion ( void );
|
||||||
|
void Translate ( const std::string& parWord );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_langFrom;
|
std::string m_langFrom;
|
||||||
std::string m_langTo;
|
std::string m_langTo;
|
||||||
|
std::string m_apiKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
18
src/main.cpp
18
src/main.cpp
|
@ -26,17 +26,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
#define UNDEF_NDEBUG
|
|
||||||
#define NDEBUG
|
|
||||||
#endif
|
|
||||||
#include <libjson.h>
|
|
||||||
#ifdef UNDEF_NDEBUG
|
|
||||||
#undef UNDEF_NDEBUG
|
|
||||||
#undef NDEBUG
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
const char* DefApiKey = "58327";
|
||||||
|
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
const char* GetBaseName (const char* parPath) {
|
const char* GetBaseName (const char* parPath) {
|
||||||
|
@ -91,8 +83,6 @@ namespace {
|
||||||
///-----------------------------------------------------------------------------
|
///-----------------------------------------------------------------------------
|
||||||
///-----------------------------------------------------------------------------
|
///-----------------------------------------------------------------------------
|
||||||
int main (int parArgc, const char* const parArgv[]) {
|
int main (int parArgc, const char* const parArgv[]) {
|
||||||
WordReference wref("en", "it");
|
|
||||||
|
|
||||||
boost::program_options::variables_map vm;
|
boost::program_options::variables_map vm;
|
||||||
if (GetCommandLine(vm, parArgc, parArgv))
|
if (GetCommandLine(vm, parArgc, parArgv))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -110,6 +100,10 @@ int main (int parArgc, const char* const parArgv[]) {
|
||||||
std::cout << "This program does not enforce this rule, but it may malfunction if you\nchoose the wrong languages.\n";
|
std::cout << "This program does not enforce this rule, but it may malfunction if you\nchoose the wrong languages.\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WordReference wref("en", "it", DefApiKey);
|
||||||
|
wref.Translate("house");
|
||||||
|
|
||||||
std::cout << wref.GetHttpLink("north face") << "\nWritten by King_DuckZ; © WordReference.com" << std::endl;
|
std::cout << wref.GetHttpLink("north face") << "\nWritten by King_DuckZ; © WordReference.com" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue