4 // Copyright (C) 2004 Novell, Inc.
6 // Google is a trademark of Google. But you already knew that.
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
31 using System
.Collections
;
34 namespace Beagle
.Daemon
{
36 [QueryableFlavor (Name
="Google", Domain
=QueryDomain
.Global
, RequireInotify
=false)]
37 public class GoogleDriver
: IQueryable
{
41 GoogleSearchService gss
= new GoogleSearchService ();
44 public GoogleDriver ()
46 googleKey
= Environment
.GetEnvironmentVariable ("GOOGLE_WEB_API_KEY");
50 get { return "Google"; }
57 Hit
FromGoogleResultElement (ResultElement res
, int rank
)
61 hit
.Uri
= new Uri (res
.URL
, true);
63 hit
.MimeType
= "text/html"; // FIXME
64 hit
.Source
= "Google";
66 // FIXME: We don't get scoring information from Google
67 // other than the ranks. This is a hack.
68 hit
.ScoreRaw
= 0.2f
/ (1 + rank
);
70 hit
["Summary"] = res
.summary
;
71 hit
["Snippet"] = res
.snippet
;
72 hit
["Title"] = res
.title
;
73 hit
["CachedSize"] = res
.cachedSize
;
74 hit
["HostName"] = res
.hostName
;
75 hit
["DirectoryTitle"] = res
.directoryTitle
;
80 static bool showNoKeyMessage
= true;
82 public bool AcceptQuery (Query query
)
87 if (! query
.AllowsDomain (QueryDomain
.Global
))
90 // FIXME: This is a meta-FIXME, since this is a bad assumption
91 // because the mime-type setting FIXME above.
92 if (! query
.AllowsMimeType ("text/html"))
95 // Reject queries if the key isn't set.
96 if (googleKey
== null || googleKey
== "") {
97 if (showNoKeyMessage
) {
98 Logger
.Log
.Warn ("To query Google, put your Google key into the GOOGLE_WEB_API_KEY environment variable.");
99 Logger
.Log
.Warn ("To get a Google key, go to http://api.google.com/createkey");
100 showNoKeyMessage
= false;
109 public void DoQuery (Query query
,
111 IQueryableChangeData changeData
)
113 GoogleSearchResult gsr
= gss
.doGoogleSearch (googleKey
,
116 false, "", false, "", "", "");
118 ArrayList hits
= new ArrayList ();
120 foreach (ResultElement elt
in gsr
.resultElements
) {
121 Hit hit
= FromGoogleResultElement (elt
, rank
);
128 public string GetSnippet (string[] query_terms
, Hit hit
)
130 return hit
["Snippet"];
133 public int GetItemCount ()
135 // Is there a way to get the # of indexed pages from
136 // google via the web services api?