| 1 | """ |
|---|
| 2 | google-api.cobra |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | This command line program demonstates how to programmatically execute Google searches. |
|---|
| 6 | |
|---|
| 7 | BUT on Apr 19, 2006, I can get no search results for it. When I visited the |
|---|
| 8 | Google Group that discusses this API (google.public.web-apis) I found some |
|---|
| 9 | disturbing complaints: |
|---|
| 10 | |
|---|
| 11 | * One poster is getting no search results. |
|---|
| 12 | * The API results are very different from the human interface results. |
|---|
| 13 | * Another poster was informed by Google that requests to use the API in production |
|---|
| 14 | applications are no longer being accepted, and the poster points out that the API |
|---|
| 15 | has not been moved out of its original "Beta" status from 2002. It may not have |
|---|
| 16 | even been updated since then. |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | HOW TO RUN |
|---|
| 20 | |
|---|
| 21 | - Using the Google Web APIs requires a license key which you can get for free here: |
|---|
| 22 | http://www.google.com/apis/ |
|---|
| 23 | |
|---|
| 24 | > cd path\to\Cobra\Examples |
|---|
| 25 | |
|---|
| 26 | - Create a google-key.text file with your Google Web APIs license key. |
|---|
| 27 | |
|---|
| 28 | > google-api "stock tips" |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | HOW TO BUILD FROM SCRATCH |
|---|
| 32 | |
|---|
| 33 | > cd path\to\Cobra\Examples |
|---|
| 34 | |
|---|
| 35 | - Launch a .NET SDK prompt and issue: |
|---|
| 36 | > wsdl.exe http://api.google.com/GoogleSearch.wsdl |
|---|
| 37 | |
|---|
| 38 | - Open GoogleSearchService.cs and: |
|---|
| 39 | - Add a capitalized method version of doGoogleSearch(): |
|---|
| 40 | public GoogleSearchResult DoGoogleSearch(string key, string q, int start, int maxResults, bool filter, string restrict, bool safeSearch, string lr, string ie, string oe) { |
|---|
| 41 | return doGoogleSearch(key, q, start, maxResults, filter, restrict, safeSearch, lr, ie, oe); |
|---|
| 42 | } |
|---|
| 43 | - Capitalize the properties of class GoogleSearchResult |
|---|
| 44 | - Capitalize the properties of class ResultElement |
|---|
| 45 | |
|---|
| 46 | > cobra --compile google-api.cobra GoogleSearchService.cs |
|---|
| 47 | |
|---|
| 48 | - Then go to HOW TO RUN above |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | TODO |
|---|
| 52 | [ ] parameterize some of the Google options |
|---|
| 53 | [ ] add MSN support |
|---|
| 54 | [ ] number the results |
|---|
| 55 | [ ] Handle this with some retries: |
|---|
| 56 | Unhandled Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connect |
|---|
| 57 | ion attempt failed because the connected party did not properly respond after a period of time, or established connection failed becaus |
|---|
| 58 | e connected host has failed to respond |
|---|
| 59 | |
|---|
| 60 | """ |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | class WebSearch |
|---|
| 64 | |
|---|
| 65 | def main |
|---|
| 66 | args = CobraCore.commandLineArgs |
|---|
| 67 | |
|---|
| 68 | # Get the Google Web APIs license key. |
|---|
| 69 | if File.exists('google-key.text') |
|---|
| 70 | googleKey = File.readAllText('google-key.text') |
|---|
| 71 | else if File.exists('google-key.txt') |
|---|
| 72 | googleKey = File.readAllText('google-key.txt') |
|---|
| 73 | else |
|---|
| 74 | print 'Get a free Google Web APIs license key at http://www.google.com/apis/ and' |
|---|
| 75 | print 'put it in a file named google-key.text' |
|---|
| 76 | return |
|---|
| 77 | googleKey = googleKey.trim |
|---|
| 78 | assert googleKey |
|---|
| 79 | |
|---|
| 80 | # Take the query from the command line. |
|---|
| 81 | if args.count < 2 |
|---|
| 82 | print 'usage: google-api <query>' |
|---|
| 83 | return |
|---|
| 84 | query = args[1] |
|---|
| 85 | |
|---|
| 86 | googleSearch = GoogleSearchService() |
|---|
| 87 | |
|---|
| 88 | # Run the query. |
|---|
| 89 | results = googleSearch.doGoogleSearch(googleKey, query, 0, 10, false, '', false, '', '', '') |
|---|
| 90 | |
|---|
| 91 | if results.resultElements is nil |
|---|
| 92 | print 'The call was made, but no results were returned.' |
|---|
| 93 | print 'As of April 19, 2006, numerous problems with the Google API have been reported.' |
|---|
| 94 | return |
|---|
| 95 | |
|---|
| 96 | for result as ResultElement in results.resultElements |
|---|
| 97 | print |
|---|
| 98 | print result.title |
|---|
| 99 | print result.url |
|---|
| 100 | print result.snippet |
|---|
| 101 | print |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | # tell Cobra what is in GoogleSearchService.cs: |
|---|
| 105 | |
|---|
| 106 | class GoogleSearchService is extern |
|---|
| 107 | def doGoogleSearch(key as String, q as String, start as int, maxResults as int, filter as bool, restrict as String, safeSearch as bool, lr as String, ie as String, oe as String) as GoogleSearchResult |
|---|
| 108 | pass |
|---|
| 109 | |
|---|
| 110 | class GoogleSearchResult is extern |
|---|
| 111 | get resultElements as List<of ResultElement> |
|---|
| 112 | pass |
|---|
| 113 | |
|---|
| 114 | class ResultElement is extern |
|---|
| 115 | get snippet as String |
|---|
| 116 | pass |
|---|
| 117 | get title as String |
|---|
| 118 | pass |
|---|
| 119 | get url as String |
|---|
| 120 | pass |
|---|