Thread: [Phpcms-plugins-cvs] google2rss google2rss.dat,NONE,1.1 GoogleSearch.wsdl,NONE,1.1 google2rss.php,NO
Brought to you by:
mjahn
From: Martin J. <mj...@us...> - 2005-02-24 00:05:08
|
Update of /cvsroot/phpcms-plugins/google2rss In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16447 Added Files: google2rss.dat GoogleSearch.wsdl google2rss.php .cvsignore Log Message: initial import --- NEW FILE: google2rss.dat --- --- NEW FILE: .cvsignore --- cache *project* api.html --- NEW FILE: GoogleSearch.wsdl --- <?xml version="1.0"?> <!-- WSDL description of the Google Web APIs. The Google Web APIs are in beta release. All interfaces are subject to change as we refine and extend our APIs. Please see the terms of use for more information. --> <!-- Revision 2002-08-16 --> <definitions name="GoogleSearch" targetNamespace="urn:GoogleSearch" xmlns:typens="urn:GoogleSearch" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <!-- Types for search - result elements, directory categories --> <types> <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:GoogleSearch"> <xsd:complexType name="GoogleSearchResult"> <xsd:all> <xsd:element name="documentFiltering" type="xsd:boolean"/> <xsd:element name="searchComments" type="xsd:string"/> <xsd:element name="estimatedTotalResultsCount" type="xsd:int"/> <xsd:element name="estimateIsExact" type="xsd:boolean"/> <xsd:element name="resultElements" type="typens:ResultElementArray"/> <xsd:element name="searchQuery" type="xsd:string"/> <xsd:element name="startIndex" type="xsd:int"/> <xsd:element name="endIndex" type="xsd:int"/> <xsd:element name="searchTips" type="xsd:string"/> <xsd:element name="directoryCategories" type="typens:DirectoryCategoryArray"/> <xsd:element name="searchTime" type="xsd:double"/> </xsd:all> </xsd:complexType> <xsd:complexType name="ResultElement"> <xsd:all> <xsd:element name="summary" type="xsd:string"/> <xsd:element name="URL" type="xsd:string"/> <xsd:element name="snippet" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="cachedSize" type="xsd:string"/> <xsd:element name="relatedInformationPresent" type="xsd:boolean"/> <xsd:element name="hostName" type="xsd:string"/> <xsd:element name="directoryCategory" type="typens:DirectoryCategory"/> <xsd:element name="directoryTitle" type="xsd:string"/> </xsd:all> </xsd:complexType> <xsd:complexType name="ResultElementArray"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:ResultElement[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="DirectoryCategoryArray"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:DirectoryCategory[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="DirectoryCategory"> <xsd:all> <xsd:element name="fullViewableName" type="xsd:string"/> <xsd:element name="specialEncoding" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:schema> </types> <!-- Messages for Google Web APIs - cached page, search, spelling. --> <message name="doGetCachedPage"> <part name="key" type="xsd:string"/> <part name="url" type="xsd:string"/> </message> <message name="doGetCachedPageResponse"> <part name="return" type="xsd:base64Binary"/> </message> <message name="doSpellingSuggestion"> <part name="key" type="xsd:string"/> <part name="phrase" type="xsd:string"/> </message> <message name="doSpellingSuggestionResponse"> <part name="return" type="xsd:string"/> </message> <!-- note, ie and oe are ignored by server; all traffic is UTF-8. --> <message name="doGoogleSearch"> <part name="key" type="xsd:string"/> <part name="q" type="xsd:string"/> <part name="start" type="xsd:int"/> <part name="maxResults" type="xsd:int"/> <part name="filter" type="xsd:boolean"/> <part name="restrict" type="xsd:string"/> <part name="safeSearch" type="xsd:boolean"/> <part name="lr" type="xsd:string"/> <part name="ie" type="xsd:string"/> <part name="oe" type="xsd:string"/> </message> <message name="doGoogleSearchResponse"> <part name="return" type="typens:GoogleSearchResult"/> </message> <!-- Port for Google Web APIs, "GoogleSearch" --> <portType name="GoogleSearchPort"> <operation name="doGetCachedPage"> <input message="typens:doGetCachedPage"/> <output message="typens:doGetCachedPageResponse"/> </operation> <operation name="doSpellingSuggestion"> <input message="typens:doSpellingSuggestion"/> <output message="typens:doSpellingSuggestionResponse"/> </operation> <operation name="doGoogleSearch"> <input message="typens:doGoogleSearch"/> <output message="typens:doGoogleSearchResponse"/> </operation> </portType> <!-- Binding for Google Web APIs - RPC, SOAP over HTTP --> <binding name="GoogleSearchBinding" type="typens:GoogleSearchPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="doGetCachedPage"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="doSpellingSuggestion"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="doGoogleSearch"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <!-- Endpoint for Google Web APIs --> <service name="GoogleSearchService"> <port name="GoogleSearchPort" binding="typens:GoogleSearchBinding"> <soap:address location="http://api.google.com/search/beta2"/> </port> </service> </definitions> --- NEW FILE: google2rss.php --- <?php /** * google2rss * * This script is a user interface for the google-API. It presents and caches * search results with google and updates the results in a daily rhythm. The * results are available as RSS or ATOM-feed for syndication. * * Possible applications are for watching the position of your own site within * the results for some search terms. The service is available for all people and * identifies the feeds by the search terms. If there are more than 1000 API * calls a day, I may stretch the update cycle to two days or maybe three days. * * <b>License</b> * * 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 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 * * @author Martin Jahn <mj...@us...> * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @copyright Copyright (c) 2005, Martin Jahn * @version $Id: google2rss.php,v 1.1 2005/02/24 00:04:53 mjahn Exp $ * @package google2rss */ /* * $Log: google2rss.php,v $ * Revision 1.1 2005/02/24 00:04:53 mjahn * initial import * */ class google2rss { var $key = 'V2XjHP1QFHIMJ+bOfslwEZf2MwbzSinr'; var $TOCfile = 'google2rss.dat'; var $TOC = array (); var $showForm = false; var $_TOC_parsed = false; function __construct () { $this->logger =& Log::factory ('file', dirname (__FILE__).'/google2rss.log', 'Google2RSS', array ('filename'=>dirname (__FILE__).'/google2rss.log')); if (isset ($_GET ['id'])) { $id = ''; if (isset ($_GET ['id'])) { $id = strip_tags ($_GET ['id']); } $format = 'atom'; if (isset ($_GET ['format'])) { $format = strip_tags ($_GET ['format']); } $this->feed ($id, $format); return true; } if (isset ($_POST ['submit-show-feed']) && isset ($_POST ['showfeed'])) { $id = strip_tags ($_POST ['showfeed']); $this->showEntries (); $this->showFeed ($id, $this->_getCache ($id)); return true; } if (!isset ($_POST ['q']) || !isset ($_POST ['q'])) { if ($this->showForm) { $this->showForm (); } $this->showEntries (); return true; } if (!isset ($_POST ['q'])) { $_POST ['q'] = ''; } $this->q = strtolower ($_POST ['q']); if (!isset ($_POST ['lang'])) { $_POST ['lang'] = ''; } $this->lang = strip_tags ($_POST ['lang']); if (!isset ($_POST ['restrict'])) { $_POST ['restrict'] = ''; } $this->restrict = strip_tags ($_POST ['restrict']); if (trim ($this->q) != '') { $this->_cacheSearch (); } else { $this->showEntries (); } } function __destruct () { $this->logger->close (); } function feed ($id, $format = 'atom') { $this->logger->log(strtoupper ($format).' feed '.$id.' from '.$_SERVER ['REMOTE_ADDR'], PEAR_LOG_NOTICE); header ('Content-Type: application/xml'); readfile (dirname (__FILE__).'/cache/'.$id.'.'.$format); } function showForm () { echo '<form action="#" method="post"><fieldset>' . '<legend>Suchbegriffe überwachen</legend>' . '<label><span>Suchbegriff eingeben</span><input type="text" name="q" value="" /></label>' . '<label><span> <span class="hidden">Anfrage abschicken</span></span><input type="submit" name="submit-google-rss" value="Los" /></label>' . '<input type="radio" name="lang" value="lang_de" /> deutsch ' . '<input type="radio" name="lang" value="lang_en" /> englisch ' . '<input type="radio" name="lang" value="" checked="checked" /> egal ' . '</fieldset></form></body></html>'; } function showEntries () { if (!$this->_parseTOCFile ()) { return false; } echo '<form action="#" method="post"><fieldset>' . '<legend>Vorhandene Feeds</legend>' . '<label><span>Suchbegriffe</span><select name="showfeed" size="1">'; foreach ($this->TOC as $id=>$data) { echo '<option value="'.$id.'">'.htmlentities ($data ['q']).'</option>'."\n"; } echo '</select></label>' . '<label><span> <span class="hidden"></span></span><input type="submit" name="submit-show-feed" value="Suchergebnisse anzeigen" /></label>' . '</fieldset></form>'; return true; } function showFeed ($id, $data) { if (!$this->_parseTOCFile ()) { return false; } if (!isset ($this->TOC [$id])) { return false; } $this->logger->log('searchresults for '.$this->TOC [$id].' from '.$_SERVER ['REMOTE_ADDR'], PEAR_LOG_NOTICE); echo '<h3>Suchergebnisse für "'.$this->TOC [$id] ['q'].'"</h3>' . '<div class="e"><h4>Als Feed</h4>'. '<ul class="none">' . '<li><a href="/service/google2rss/'.$id.'.rss" title="RSS 2.0 Feed der Suchergebnisse">RSS-Feed</a></li>' . '<li><a href="/service/google2rss/'.$id.'.atom" title="ATOM 0.3 Feed der Suchergebnisse">ATOM-Feed</a></li>' . '</ul></div>'; echo '<dl>'; foreach ($data as $entry) { echo '<dt><a href="'.$entry['url'].'">'.strip_tags ($entry ['title']).'</a></dt><dd>'.strip_tags ($entry ['desc']).'</dd>'."\n"; } echo '</dl>'; } /** * @return boolean success-status of the parsing the TOCfile into an array */ function _parseTOCFile () { if ($this->_TOC_parsed) { return true; } if (!file_exists (dirname (__FILE__).'/'.$this->TOCfile)) { return false; } $data = file (dirname (__FILE__).'/'.$this->TOCfile); foreach ($data as $line) { $entry = explode (' ', trim ($line), 5); $this->TOC [$entry [0]] = array ('time'=>$entry [1], 'lang'=>$entry [2], 'restrict'=>$entry [3], 'q'=>$entry [4]); } $this->_TOC_parsed = true; return true; } /** * This function writes the tocfile * @return boolean success-status of writing the TOCfile */ function _writeTOCFile () { if (!$this->_TOC_parsed) { $this->_parseTOCFile (); } if (!is_array ($this->TOC)) { $this->TOC = array ($this->TOC); } $fh = fopen (dirname (__FILE__).'/'.$this->TOCfile, 'wb'); foreach ($this->TOC as $id=>$entry) { fwrite ($fh, $id.' '.$entry ['time'].' '.$entry ['lang'].' '.$entry ['restrict'].' '.$entry ['q']."\n"); } fclose ($fh); } /** * @param string id ID of the cacheentry (md5-hash of the searchterm) * @return array Content of the cached file */ function _getCache ($id) { // filename of the cached file $cachefile = dirname (__FILE__).'/cache/'.$id; // unserialize data from file $fh = fopen ($cachefile, 'rb'); return unserialize (join ('', file ($cachefile))); } /** * @param string id ID of the cacheentry (md5-hash of the searchterm) * @param array entry Content that should be cached * @return boolean success-status of writing the cachefile */ function _putCache ($id, $entry) { // filename of the cached file $cachefile = dirname (__FILE__).'/cache/'.$id; // serialize data into file $fh = fopen ($cachefile, 'wb'); fwrite ($fh, serialize ($entry)); fclose ($fh); // save RSS-Feed into file $fh = fopen ($cachefile.'.rss', 'wb'); fwrite ($fh, '<?xml version="1.0"?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel><title>Google: '.strip_tags (htmlentities ($this->q)).'</title> <link>http://new.martin-jahn.info/service/google2rss/index.de.htm</link> <description>Google Suchergebnisse für: '.strip_tags (htmlentities ($this->q)).'</description> <language>de-DE</language>'."\n"); foreach ($entry as $item) { fwrite ($fh, '<item>'."\n".'<title>'.strip_tags ($item ['title']).'</title>'."\n".'<link>'.htmlentities (strip_tags ($item ['url'])).'</link>'."\n".'<description>'.strip_tags ($item['desc']).'</description>'."\n".'</item>'."\n"); } fwrite ($fh, '</channel></rss>'); fclose ($fh); // save ATOM-Feed into file $fh = fopen ($cachefile.'.atom', 'wb'); fwrite ($fh, '<?xml version="1.0" encoding="utf-8" ?> <feed xmlns="http://purl.org/atom/ns#" version="0.3" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="de"> <title>Google2ATOM: '.strip_tags (htmlentities ($this->q)).'</title> <link rel="alternate" type="text/html" href="http://new.martin-jahn.info/service/google2rss/index.de.htm"/> <tagline type="text/plain" mode="escaped">Google Suchergebnisse für: '.strip_tags (htmlentities ($this->q)).'</tagline> <modified>'.date ('r', time ()).'</modified> <generator version="1.0" url="http://new.martin-jahn.info/service/google2rss/index.de.htm">Google2RSS</generator>'."\n"); $i = 1; foreach ($entry as $item) { fwrite ($fh, '<entry xml:lang="de">'."\n".'<title>'.strip_tags ($item ['title']).'</title>'."\n".'<link rel="alternate" type="text/html" href="'.htmlentities (strip_tags ($item ['url'])).'" />'."\n".'<issued>'.date ('r', time ()).'</issued>'."\n".'<modified>'.date ('r', time ()).'</modified>'."\n".'<id>'.$i.'</id>'."\n".'<summary>'.strip_tags ($item ['title']).'</summary>'."\n".'<content type="text/xml" mode="xml">'.strip_tags ($item['desc']).'</content>'."\n".'</entry>'."\n"); $i++; } fwrite ($fh, '</feed>'); fclose ($fh); } /** * This method looks for an already existing entry for the searchterm in the * cachefile. If there is none, it creates one, calls the Google-search-API * and stores the result in a cachefile */ function _cacheSearch () { if (!$this->_parseTOCFile ()) { return false; } $id = md5($this->q.$this->lang.$this->restrict); if (!isset ($this->TOC [$id])) { $this->TOC [$id] = array ('time'=>time (), 'lang'=>$this->lang, 'q'=>$this->q, 'restrict'=>$this->restrict); $result = $this->_callGoogle ($this->q); $data = array (); foreach ($result->resultElements as $entry) { $data [] = array ('url'=>$entry->URL, 'desc'=>$entry->snippet, 'title'=>$entry->title); } $this->_putCache ($id, $data); $this->_writeTOCFile (); } else { $data = $this->_getCache ($id); } $this->showFeed ($id, $data); } /** * This method is the central update-part of the script. It looks for old * cached searchterms and updates the results of the with a new Google- * search-API SOAP-call if necessary. */ function _updateSearch () { } /** * This method is the interface to the Google-search-API * * @param string searchterm The term you want to get the search- results for */ function _callGoogle ($searchterm) { $params = array ( 'key'=>$this->key, 'q'=>$searchterm, 'start'=>0, 'maxResults'=>10, 'filter'=>false, 'restrict'=>$this->restrict, 'safeSearch'=>false, 'lr'=>$this->lang, 'ie'=>'', 'oe'=>''); $client = new SoapClient (dirname (__FILE__).'/GoogleSearch.wsdl'); $results = $client->__call ('doGoogleSearch', $params); return ($results); } } include_once ('Log.php'); $do = new google2rss (); ?> |