1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Copyright 2005,2006 Fabrice Colin
*
* 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <iostream>
#include "Url.h"
#include "MIMEScanner.h"
#include "GoogleAPIEngine.h"
#include "GAPIGoogleSearchBindingProxy.h"
#include "GAPI.nsmap"
using std::cout;
using std::cerr;
using std::endl;
GoogleAPIEngine::GoogleAPIEngine(const string &key) :
WebEngine(),
m_key(key)
{
}
GoogleAPIEngine::~GoogleAPIEngine()
{
}
/// Retrieves the specified URL from the cache; NULL if error. Caller deletes.
Document *GoogleAPIEngine::retrieveCachedUrl(const string &url)
{
GoogleSearchBinding soapProxy;
xsd__base64Binary base64Page;
if ((m_key.empty() == true) ||
(soapProxy.gapi1__doGetCachedPage(m_key, url, base64Page)))
{
return NULL;
}
if ((base64Page.__ptr != NULL) &&
(base64Page. __size > 0))
{
Url urlObj(url);
Document *pDoc = new Document(url, url, MIMEScanner::scanUrl(urlObj), "");
pDoc->setData((const char*)base64Page.__ptr, (unsigned int)base64Page. __size);
return pDoc;
}
return NULL;
}
/// Checks spelling.
string GoogleAPIEngine::checkSpelling(const string &text)
{
GoogleSearchBinding soapProxy;
string spellOut;
if ((m_key.empty() == true) ||
(soapProxy.gapi1__doSpellingSuggestion(m_key, text, spellOut)))
{
return "";
}
return spellOut;
}
//
// Implementation of SearchEngineInterface
//
/// Runs a query; true if success.
bool GoogleAPIEngine::runQuery(QueryProperties& queryProps,
unsigned int startDoc)
{
string queryString(queryProps.getFreeQuery(true));
unsigned int maxResultsCount = queryProps.getMaximumResultsCount();
m_resultsList.clear();
m_resultsCountEstimate = 0;
if (queryProps.getType() != QueryProperties::XAPIAN_QP)
{
cerr << "GoogleAPIEngine::runQuery: query type not supported" << endl;
return false;
}
setQuery(queryProps);
if (m_key.empty() == true)
{
cerr << "GoogleAPIEngine::runQuery: no key" << endl;
return false;
}
if (queryString.empty() == true)
{
#ifdef DEBUG
cout << "GoogleAPIEngine::runQuery: query is empty" << endl;
#endif
return false;
}
#ifdef DEBUG
cout << "GoogleAPIEngine::runQuery: query is " << queryString << endl;
#endif
GoogleSearchBinding soapProxy;
struct gapi1__doGoogleSearchResponse queryOut;
// No filter, no safe search
int soapStatus = soapProxy.gapi1__doGoogleSearch(m_key, queryString, 0, (int)(maxResultsCount > 10 ? 10 : maxResultsCount),
false, "", false, "", "utf-8", "utf-8", queryOut);
if (soapStatus != SOAP_OK)
{
cerr << "GoogleAPIEngine::runQuery: search failed with status " << soapStatus << endl;
return false;
}
struct gapi1__GoogleSearchResult *searchResult = queryOut.return_;
if ((searchResult != NULL) &&
(searchResult->resultElements != NULL))
{
float pseudoScore = 100;
m_resultsCountEstimate = searchResult->estimatedTotalResultsCount;
for (int i = 0; i < searchResult->resultElements->__size; ++i)
{
struct gapi1__ResultElement *resultElement = searchResult->resultElements->__ptr[i];
DocumentInfo result(resultElement->title, resultElement->URL, "", "");
result.setExtract(resultElement->snippet);
result.setScore(pseudoScore);
if (processResult("http://www.google.com/", result) == true)
{
m_resultsList.push_back(result);
--pseudoScore;
}
}
}
return true;
}
|