Fix a fd leak in knotes backend (stupid me) and some cleanup in kaddrbook backend.
[beagle.git] / beagled / GoogleDriver.cs
blob5497323838819701fe6612ebe395eaf664bee363
1 //
2 // GoogleDriver.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
6 // Google is a trademark of Google. But you already knew that.
7 //
9 //
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.
30 using System;
31 using System.Collections;
32 using System.Text;
34 using Beagle.Util;
36 namespace Beagle.Daemon {
38 [QueryableFlavor (Name="Google", Domain=QueryDomain.Global, RequireInotify=false)]
39 public class GoogleDriver : IQueryable {
41 int maxResults = 5;
43 GoogleSearchService gss = new GoogleSearchService ();
44 string googleKey;
46 public GoogleDriver ()
48 googleKey = Environment.GetEnvironmentVariable ("GOOGLE_WEB_API_KEY");
51 public string Name {
52 get { return "Google"; }
55 public void Start ()
59 Hit FromGoogleResultElement (ResultElement res, int rank)
61 Hit hit = new Hit ();
63 hit.Uri = new Uri (res.URL, true);
64 hit.Type = "Google";
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;
79 return hit;
82 static bool showNoKeyMessage = true;
84 public bool AcceptQuery (Query query)
86 if (query.IsEmpty)
87 return false;
89 if (! query.AllowsDomain (QueryDomain.Global))
90 return false;
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"))
95 return false;
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;
104 return false;
107 return true;
111 public void DoQuery (Query query,
112 IQueryResult result,
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);
122 } else
123 continue; // ignore other query parts
126 Logger.Log.Debug ("Querying google for '" + sb.ToString () + "'");
127 GoogleSearchResult gsr = gss.doGoogleSearch (googleKey,
128 sb.ToString (),
129 0, maxResults,
130 false, "", false, "", "", "");
132 ArrayList hits = new ArrayList ();
133 int rank = 0;
134 foreach (ResultElement elt in gsr.resultElements) {
135 Hit hit = FromGoogleResultElement (elt, rank);
136 hits.Add (hit);
137 ++rank;
139 result.Add (hits, gsr.estimatedTotalResultsCount);
142 public string GetSnippet (string[] query_terms, Hit hit)
144 return hit ["Snippet"];
147 public QueryableStatus GetQueryableStatus ()
149 return null;