Wiki

root/cobra/trunk/Samples/google-api.cobra

Revision 2129, 3.6 KB (checked in by Chuck.Esterbrook, 3 years ago)

Take out is shared on def main in the how-to's.

  • Property svn:eol-style set to native
Line 
1"""
2google-api.cobra
3
4
5This command line program demonstates how to programmatically execute Google searches.
6
7BUT on Apr 19, 2006, I can get no search results for it. When I visited the
8Google Group that discusses this API (google.public.web-apis) I found some
9disturbing 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
19HOW 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
31HOW 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
51TODO
52    [ ] parameterize some of the Google options
53    [ ] add MSN support
54    [ ] number the results
55    [ ] Handle this with some retries:
56Unhandled Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connect
57ion attempt failed because the connected party did not properly respond after a period of time, or established connection failed becaus
58e connected host has failed to respond
59
60"""
61
62
63class 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
106class 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
110class GoogleSearchResult is extern
111    get resultElements as List<of ResultElement>
112        pass
113
114class ResultElement is extern
115    get snippet as String
116        pass
117    get title as String
118        pass
119    get url as String
120        pass
Note: See TracBrowser for help on using the browser.