src dir moved into wordref
This commit is contained in:
parent
a03143f145
commit
666d25c4d1
5 changed files with 3 additions and 3 deletions
195
wordref/src/WordReference.cpp
Normal file
195
wordref/src/WordReference.cpp
Normal file
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
WordReferenceCLI, a c++ program to access WordReference.com.
|
||||
Copyright (C) 2013 King_DuckZ
|
||||
|
||||
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 3 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "main.hpp"
|
||||
#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"),
|
||||
std::pair<std::string, std::string>("zh", "Chinese"),
|
||||
std::pair<std::string, std::string>("cz", "Czech"),
|
||||
std::pair<std::string, std::string>("en", "English"),
|
||||
std::pair<std::string, std::string>("fr", "French"),
|
||||
std::pair<std::string, std::string>("gr", "Greek"),
|
||||
std::pair<std::string, std::string>("it", "Italian"),
|
||||
std::pair<std::string, std::string>("ja", "Japanese"),
|
||||
std::pair<std::string, std::string>("ko", "Korean"),
|
||||
std::pair<std::string, std::string>("pl", "Polish"),
|
||||
std::pair<std::string, std::string>("pt", "Portuguese"),
|
||||
std::pair<std::string, std::string>("ro", "Romanian"),
|
||||
std::pair<std::string, std::string>("es", "Spanish"),
|
||||
std::pair<std::string, std::string>("tr", "Turkish")
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vector<const std::string*>& parNames) {
|
||||
typedef std::vector<std::pair<const std::string*, const std::string*> > SortedListType;
|
||||
SortedListType sorted;
|
||||
sorted.reserve(SupportedLanguages.size());
|
||||
parCodes.reserve(SupportedLanguages.size());
|
||||
parNames.reserve(SupportedLanguages.size());
|
||||
|
||||
for (LanguageMapType::const_iterator itCurr = SupportedLanguages.begin(), itEND = SupportedLanguages.end(); itCurr != itEND; ++itCurr) {
|
||||
sorted.push_back(std::make_pair(&itCurr->first, &itCurr->second));
|
||||
}
|
||||
std::sort(sorted.begin(), sorted.end(), [](const SortedListType::value_type& parA, const SortedListType::value_type& parB) { return *(parA.second) < *(parB.second); });
|
||||
for (SortedListType::const_iterator itCurr = sorted.begin(), itEND = sorted.end(); itCurr != itEND; ++itCurr) {
|
||||
parCodes.push_back(itCurr->first);
|
||||
parNames.push_back(itCurr->second);
|
||||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) :
|
||||
std::runtime_error(parMessage)
|
||||
{
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
WordReference::WordReference (const char* parFrom, const char* parTo, const char* parApiKey) :
|
||||
m_langFrom(parFrom),
|
||||
m_langTo(parTo),
|
||||
m_apiKey(parApiKey)
|
||||
{
|
||||
if (SupportedLanguages.find(parFrom) == SupportedLanguages.end()) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid source language: \"" << m_langFrom << "\"";
|
||||
throw ErrBadLanguage(oss.str());
|
||||
}
|
||||
if (SupportedLanguages.find(parTo) == SupportedLanguages.end()) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid destination language: \"" << m_langTo << "\"";
|
||||
throw ErrBadLanguage(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
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));
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetHttpLink (const char* parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
oss << GetCleanWord(parWord) << "&dict=" << m_langFrom << m_langTo;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetHttpLink (const std::string& parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
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