Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / GoogleDriver.cs
blob4336e19120614190b2cdb432b467ac9395e0404c
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 Beagle.Util;
34 namespace Beagle.Daemon {
36 [QueryableFlavor (Name="Google", Domain=QueryDomain.Global, RequireInotify=false)]
37 public class GoogleDriver : IQueryable {
39 int maxResults = 5;
41 GoogleSearchService gss = new GoogleSearchService ();
42 string googleKey;
44 public GoogleDriver ()
46 googleKey = Environment.GetEnvironmentVariable ("GOOGLE_WEB_API_KEY");
49 public string Name {
50 get { return "Google"; }
53 public void Start ()
57 Hit FromGoogleResultElement (ResultElement res, int rank)
59 Hit hit = new Hit ();
61 hit.Uri = new Uri (res.URL, true);
62 hit.Type = "Google";
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;
77 return hit;
80 static bool showNoKeyMessage = true;
82 public bool AcceptQuery (Query query)
84 if (query.IsEmpty)
85 return false;
87 if (! query.AllowsDomain (QueryDomain.Global))
88 return false;
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"))
93 return false;
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;
102 return false;
105 return true;
109 public void DoQuery (Query query,
110 IQueryResult result,
111 IQueryableChangeData changeData)
113 GoogleSearchResult gsr = gss.doGoogleSearch (googleKey,
114 query.QuotedText,
115 0, maxResults,
116 false, "", false, "", "", "");
118 ArrayList hits = new ArrayList ();
119 int rank = 0;
120 foreach (ResultElement elt in gsr.resultElements) {
121 Hit hit = FromGoogleResultElement (elt, rank);
122 hits.Add (hit);
123 ++rank;
125 result.Add (hits);
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?
137 return -1;