wmoxam (owner)

Revisions

gist: 97069 Download_button fork
public
Public Clone URL: git://gist.github.com/97069.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!eruby
<%
require "cgi"
  require 'MSNSearchServiceClient.rb'
  
  # Create new CGI object for extracting search parameters
cgi = CGI.new
h = cgi.params
 
  # Search criteria
qry = h['searchQuery']
  # Check if search "Near Me"
  nearMe = false
  if (h['nearMeButton'].length > 0)
    nearMe = true
  end
  
# Specify the number of results per page to return
nResults = h['nResults']
# Specify the starting point for results
offset = h['offset']
# Safe Search options
safeGroup = h['safeGroup']
# Specify the language and country / region for search results
culture = h['cultureInfo']
# Longitude of your location
longitude = h['longitude']
# Latitude of your location
latitude = h['latitude']
# Radius of your search in miles
radius = h['sRadius']
# Search Flags : enable query word marking (hit highlighting)
markQueryWords = h['MarkQueryWords']
# Search Flags : disable spell correction for query operators
disableSpellCorrect = h['DisableSpellCorrectForSpecialWords']
# Search Flags : disable host collapsing
disableHostCollapsing = h['DisableHostCollapsing']
# Sort PhoneBook Results By
sortGroup = h['sortGroup']
# Set Search TagFilter(s) for Web
webSearchTags = h['webSearchTags']
# Set Search TagFilter(s) for News
newsSearchTags = h['newsSearchTags']
# Return Files of this Type (Web Source Type)
fileTypeGroup = h['fileTypeGroup']
# Return Results of this FileType (PhoneBook SourceType by listing type)
phoneBookSourceType = h['phoneBookSourceType']
 
  
  # Create new search object and set search parameters
  searchClient = MSNSearchServiceClient.new()
  searchClient.searchQuery = qry
  searchClient.culture = culture
  searchClient.resultsPerPage = nResults
  searchClient.resultsStartPoint = offset
  searchClient.safeSearchOption = safeGroup
  searchClient.longitude = longitude
  searchClient.latitude = latitude
  searchClient.radius = radius
  searchClient.markQueryWords = markQueryWords
  searchClient.disableSpellCorrection = disableSpellCorrect
  searchClient.disableHostCollapsing = disableHostCollapsing
  searchClient.sortPhoneBookResults = sortGroup
  searchClient.webSearchTags = webSearchTags
  searchClient.newsSearchTags = newsSearchTags
  searchClient.fileTypeGroup = fileTypeGroup
  searchClient.phoneBookSearchType = phoneBookSourceType
  searchClient.nearMe = nearMe
 
  # execute search
  searchResp = searchClient.search()
 
  # Print search results
  puts "<html><body>"
 
  # Check the SearchResponse to determine whether it contains a spelling suggestion.
  # The spelling suggestion is returned in the Title field of the Spelling SourceResponse.
  if (searchResp.response.responses[1].source == SourceType::Spelling && searchResp.response.responses[1].total > 0)
    spelling = searchResp.response.responses[1].results[0].title
    puts "<H3><b>Did you mean: <i><a href=\"javascript:window.parent.performSearch('#{spelling}');\">#{spelling}</a> </i></b></H3><BR><BR>"
  end
  
  # iterate through all the SourceResponses in the SearchResponse.
  searchResp.response.responses.each do |srcResp|
    
      # For each SourceResponse, determine whether the total number of results returned
      # is greater than zero. If so, print a simple heading for that SourceResponse
      # type. Note that Total is an *estimate* of the total number of returned results,
      # with the exception of Spelling results, which return zero results or one result.
      if (srcResp.results.length > 0)
          # List the source type and estimated total of result
          puts "<H3><B>#{srcResp.source} - Total Results: #{srcResp.total}</B></H3><HR>"
          
          # Iloop through the results returned in each SourceResponse
          srcResp.results.each do |res|
 
              # Do not print any labels for results that are not returned (NULL) or contain no text (String.Empty).
              puts "<B>Title: </B>#{res.title}<BR>" if res.title && res.title.length > 0
              puts "<B>Description: </B>#{res.description}<BR>" if (res.description && res.description.length > 0)
              puts "<B>DisplayUrl: </B><A HREF=\"#{res.displayUrl}\">#{res.displayUrl}</A><BR>" if (res.displayUrl && res.displayUrl.length > 0)
              puts "<B>Url: </B><A HREF=\"#{res.url}\">#{res.url}</A><BR>" if (res.url && res.url.length > 0)
              puts "<B>SearchTags: </B>#{res.searchTags}<BR>" if (res.searchTags && res.searchTags.length > 0)
 
              if (res.searchTagsArray)
                  puts "<B>SearchTagsArray (Length #{res.searchTagsArray.length})</B><BR><BR>"
                  0.upto(res.searchTagsArray.length - 1) do |index|
                      puts "<B>SearchTagsArray[#{index}] Name: </B>#{res.searchTagsArray[index].name}<BR>"
                      puts "<B>SearchTagsArray[#{index}] Value: </B>#{res.searchTagsArray[index].value}<BR>"
                  end
              end
              
              puts "<B>CacheUrl: </B><A HREF=\"#{res.cacheUrl}\">#{res.cacheUrl}</A><BR>" if (res.cacheUrl && res.cacheUrl.length > 0)
              puts "<B>Source: </B>#{res.source}<BR>" if (res.source && res.source.length > 0)
              puts "<B>Summary: </B>#{res.summary}<BR>" if (res.summary && res.summary.length > 0)
              puts "<B>ResultType: </B>#{res.resultType}<BR>" if (res.resultType && res.resultType.length > 0)
 
              # Process PhoneBook results.
              # If the search was a "Near Me" search, the Location settings are considered as a relevance factor in the
              # Web search and PhoneBook results may also be returned for appropriate queries.
              # If the SourceType returned is a PhoneBook SourceType, print a label for the Phone field, each field
              # in the Address Object, and each field in the returned Location object.
              # Since Address.FormattedAddress is always blank and Location.Radius always returns 5,
              # these fields are skipped in the sample.
              # The SecondaryCity and PostalCode fields are not returned for addresses in the United States;
              # the SecondaryCity field is used for addresses in the United Kingdom.
              if (srcResp.source == SourceType::PhoneBook)
                  puts "<B>Phone: </B>#{res.phone}<BR>" if (res.phone && res.phone.length > 0)
                  if (res.address)
                      puts "<B>AddressLine: </B>#{res.address.addressLine}<BR>" if (res.address.addressLine && res.address.addressLine.length > 0)
                      puts "<B>CountryRegion: </B>#{res.address.countryRegion}<BR>" if (res.address.countryRegion && res.address.countryRegion.length > 0)
                      puts "<B>PostalCode: </B>#{res.address.postalCode}<BR>" if (res.address.postalCode && res.address.postalCode.length > 0)
                      puts "<B>PrimaryCity: </B>#{res.address.primaryCity}<BR>" if (res.address.primaryCity && res.address.primaryCity.length > 0)
                      puts "<B>SecondaryCity: </B>#{res.address.secondaryCity}<BR>" if (res.address.secondaryCity && res.address.secondaryCity.length > 0)
                      puts "<B>Subdivision: </B>#{res.address.subdivision}<BR>" if (res.address.subdivision && res.address.subdivision.length > 0)
                  end
              end
 
              if (res.location)
                  puts "<B>Latitude: </B>#{res.location.latitude}<BR>"
                  puts "<B>Longitude: </B>#{res.location.longitude}<BR>"
              end
 
              # If the query was of the form "keyword(s) Place_Name" or "keyword(s) ZIP_Code and SourceType.QueryLocation was requested,
              # print a string showing the QueryLocation result, as on the Live Search website. QueryLocation can be used in conjunction with PhoneBook results
              # for select markets, as listed in the online documentation.
              # Examples of this type of location-based keywrod query are: "pizza new york", "pizza ottawa", and "pizza 98052".
              if (srcResp.source == SourceType::QueryLocation)
                  if (res.location)
                      puts "<B>Sample Return String for QueryLocation:</B><BR>"
                      puts "Top local listings for <B>#{res.title}</B> near "
                      puts "<B>#{res.description}</B>"
                      puts " (#{res.location.longitude}, #{res.location.latitude})<BR>"
                  end
              end
 
              if (res.image)
                  img = res.image
                  puts "<B>Thumbnail File Size: </B>#{img.thumbnailFileSize}<BR>" if (img.thumbnailFileSize)
                  puts "<B>Thumbnail Height: </B>#{img.thumbnailHeight}<B>, Thumbnail Width: </B>{img.thumbnailWidth}<BR>" if(img.thumbnailHeight && img.thumbnailWidth)
                  puts "<B>Thumbnail:</B><BR><IMG SRC=\"#{img.thumbnailURL}\"></A><BR>"
                  puts "<B>Image File Size: </B>#{img.imageFileSize}<BR>" if (img.imageFileSize)
                  puts "<B>Image Height: </B>#{img.imageHeight}<B>, Image Width: </B>#{img.imageWidth}<BR>" if (img.imageHeight && img.imageWidth)
                  puts "<B>Full Size Image URL: </B><A HREF=\"#{img.imageURL}\"></A><BR><HR>" if (img.imageURL && img.imageURL.length > 0)
              end
 
              puts '<br><br>'
        
          end
 
      end
 
  end
    
    
  puts "</body></html>"
 
 
%>