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
;
36 namespace Beagle
.Daemon
{
38 [QueryableFlavor (Name
="Google", Domain
=QueryDomain
.Global
, RequireInotify
=false)]
39 public class GoogleDriver
: IQueryable
{
43 GoogleSearchService gss
= new GoogleSearchService ();
46 public GoogleDriver ()
48 googleKey
= Environment
.GetEnvironmentVariable ("GOOGLE_WEB_API_KEY");
52 get { return "Google"; }
59 Hit
FromGoogleResultElement (ResultElement res
, int rank
)
63 hit
.Uri
= new Uri (res
.URL
, true);
65 hit
.MimeType
= "text/html"; // FIXME
66 hit
.Source
= "Google";
68 // FIXME: We don't get scoring information from Google
69 // other than the ranks. This is a hack.
70 hit
.Score
= 0.2f
/ (1 + rank
);
72 hit
["Summary"] = res
.summary
;
73 hit
["Snippet"] = res
.snippet
;
74 hit
["Title"] = res
.title
;
75 hit
["CachedSize"] = res
.cachedSize
;
76 hit
["HostName"] = res
.hostName
;
77 hit
["DirectoryTitle"] = res
.directoryTitle
;
82 static bool showNoKeyMessage
= true;
84 public bool AcceptQuery (Query query
)
89 if (! query
.AllowsDomain (QueryDomain
.Global
))
92 // FIXME: This is a meta-FIXME, since this is a bad assumption
93 // because the mime-type setting FIXME above.
94 if (! query
.AllowsMimeType ("text/html"))
97 // Reject queries if the key isn't set.
98 if (googleKey
== null || googleKey
== "") {
99 if (showNoKeyMessage
) {
100 Logger
.Log
.Warn ("To query Google, put your Google key into the GOOGLE_WEB_API_KEY environment variable.");
101 Logger
.Log
.Warn ("To get a Google key, go to http://api.google.com/createkey");
102 showNoKeyMessage
= false;
111 public void DoQuery (Query query
,
113 IQueryableChangeData changeData
)
115 StringBuilder sb
= new StringBuilder ();
117 foreach (QueryPart part
in query
.Parts
) {
118 if (part
is QueryPart_Human
) {
119 sb
.Append (((QueryPart_Human
) part
).QueryString
);
120 } else if (part
is QueryPart_Text
) {
121 sb
.Append (((QueryPart_Text
) part
).Text
);
123 continue; // ignore other query parts
126 Logger
.Log
.Debug ("Querying google for '" + sb
.ToString () + "'");
127 GoogleSearchResult gsr
= gss
.doGoogleSearch (googleKey
,
130 false, "", false, "", "", "");
132 ArrayList hits
= new ArrayList ();
134 foreach (ResultElement elt
in gsr
.resultElements
) {
135 Hit hit
= FromGoogleResultElement (elt
, rank
);
142 public string GetSnippet (string[] query_terms
, Hit hit
)
144 return hit
["Snippet"];
147 public QueryableStatus
GetQueryableStatus ()