-
Notifications
You must be signed in to change notification settings - Fork 1
/
BingSearchService.cs
329 lines (277 loc) · 13.2 KB
/
BingSearchService.cs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Azure.CognitiveServices.Search.ImageSearch;
//using video = Microsoft.Azure.CognitiveServices.Search.VideoSearch;
using Microsoft.Azure.CognitiveServices.Search.ImageSearch.Models;
using Microsoft.Azure.CognitiveServices.Search.VideoSearch;
namespace ClickViajaBot.Services
{
public static class BingSearchService
{
//const string accessKey = "988c2071beae433e8d422f8a52a89e45";
//const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/images/search";
//struct SearchResult
//{
// public String jsonResult;
// public Dictionary<String, String> relevantHeaders;
// }
//static SearchResult BingWebSearch(string searchQuery)
//{
// // Construct the URI of the search request
// var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery);
// // Perform the Web request and get the response
// WebRequest request = HttpWebRequest.Create(uriQuery);
// request.Headers["Ocp-Apim-Subscription-Key"] = accessKey;
// HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
// string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
// // Create result object for return
// var searchResult = new SearchResult()
// {
// jsonResult = json,
// relevantHeaders = new Dictionary<String, String>()
// };
// // Extract Bing HTTP headers
// foreach (String header in response.Headers)
// {
// if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-"))
// searchResult.relevantHeaders[header] = response.Headers[header];
// }
// return searchResult;
//}
//static string JsonPrettyPrint(string json)
//{
// if (string.IsNullOrEmpty(json))
// return string.Empty;
// json = json.Replace(Environment.NewLine, "").Replace("\t", "");
// StringBuilder sb = new StringBuilder();
// bool quote = false;
// bool ignore = false;
// char last = ' ';
// int offset = 0;
// int indentLength = 2;
// foreach (char ch in json)
// {
// switch (ch)
// {
// case '"':
// if (!ignore)
// quote = !quote;
// break;
// case '\\':
// if (quote && last != '\\')
// ignore = true;
// break;
// }
// if (quote)
// {
// sb.Append(ch);
// if (last == '\\' && ignore)
// ignore = false;
// }
// else
// {
// switch (ch)
// {
// case '{':
// case '[':
// sb.Append(ch);
// sb.Append(Environment.NewLine);
// sb.Append(new string(' ', ++offset * indentLength));
// break;
// case '}':
// case ']':
// sb.Append(Environment.NewLine);
// sb.Append(new string(' ', --offset * indentLength));
// sb.Append(ch);
// break;
// case ',':
// sb.Append(ch);
// sb.Append(Environment.NewLine);
// sb.Append(new string(' ', offset * indentLength));
// break;
// case ':':
// sb.Append(ch);
// sb.Append(' ');
// break;
// default:
// if (quote || ch != ' ')
// sb.Append(ch);
// break;
// }
// }
// last = ch;
// }
// return sb.ToString().Trim();
//}
/// <summary>
/// Método que pesquisa imagens do hotel pedido com cognitiveservices do azure
/// </summary>
/// <param name="queryHotel"></param>
/// <param name="canal"></param>
/// <returns></returns>
public static List<string> ImageSearch(string queryHotel, string canal)
{
var client = new ImageSearchAPI(new Microsoft.Azure.CognitiveServices.Search.ImageSearch.ApiKeyServiceClientCredentials(ConfigurationManager.AppSettings["BingSearchKey"].ToString()));
List<string> urlList = null;
int numm_imagess = int.Parse(ConfigurationManager.AppSettings["Numimages"].ToString());
try
{
var imageResults = client.Images.SearchAsync(query: queryHotel).Result;
urlList = new List<string>();
if (canal == "skype")
{
numm_imagess = 10;
}
if (imageResults == null)
{
//nao encontrou nada
urlList.Add("");
}
else
{
// Image results
if (imageResults.Value.Count > 0)
{
//var firstImageResult = imageResults.Value.First();
//Console.WriteLine($"Image result count: {imageResults.Value.Count}");
//Console.WriteLine($"First image insights token: {firstImageResult.ImageInsightsToken}");
// Console.WriteLine($"First image thumbnail url: {firstImageResult.ThumbnailUrl}");
//Console.WriteLine($"First image content url: {firstImageResult.ContentUrl}");
for (int i = 0; i < numm_imagess; i++)
{
urlList.Add(imageResults.Value.ElementAt(i).ContentUrl);
}
}
else
{
urlList.Add("");
}
//Console.WriteLine($"Image result total estimated matches: {imageResults.TotalEstimatedMatches}");
//Console.WriteLine($"Image result next offset: {imageResults.NextOffset}");
//// Pivot suggestions
//if (imageResults.PivotSuggestions.Count > 0)
//{
// var firstPivot = imageResults.PivotSuggestions.First();
// Console.WriteLine($"Pivot suggestion count: {imageResults.PivotSuggestions.Count}");
// Console.WriteLine($"First pivot: {firstPivot.Pivot}");
// if (firstPivot.Suggestions.Count > 0)
// {
// var firstSuggestion = firstPivot.Suggestions.First();
// Console.WriteLine($"Suggestion count: {firstPivot.Suggestions.Count}");
// Console.WriteLine($"First suggestion text: {firstSuggestion.Text}");
// Console.WriteLine($"First suggestion web search url: {firstSuggestion.WebSearchUrl}");
// }
// else
// {
// Console.WriteLine("Couldn't find suggestions!");
// }
//}
//else
//{
// Console.WriteLine("Couldn't find pivot suggestions!");
//}
//// Query expansions
//if (imageResults.QueryExpansions.Count > 0)
//{
// var firstQueryExpansion = imageResults.QueryExpansions.First();
// Console.WriteLine($"Query expansion count: {imageResults.QueryExpansions.Count}");
// Console.WriteLine($"First query expansion text: {firstQueryExpansion.Text}");
// Console.WriteLine($"First query expansion search link: {firstQueryExpansion.SearchLink}");
//}
//else
//{
// Console.WriteLine("Couldn't find query expansions!");
//}
}
}
catch (Exception ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
return urlList;
}
/// <summary>
/// Método que pesquisa videos do hotel pedido com cognitiveservices do azure
/// </summary>
/// <param name="queryHotel"></param>
/// <param name="canal"></param>
/// <returns></returns>
public static List<string> VideoSearch(string queryHotel, string canal)
{
var client = new VideoSearchAPI(new Microsoft.Azure.CognitiveServices.Search.VideoSearch.ApiKeyServiceClientCredentials(ConfigurationManager.AppSettings["BingSearchKey"].ToString()));
List<string> urlList = null;
int num_videos = int.Parse(ConfigurationManager.AppSettings["NumVideos"].ToString());
try
{
var imageResults = client.Videos.SearchAsync(query: queryHotel).Result;
urlList = new List<string>();
if (imageResults == null)
{
//nao encontrou nada
urlList.Add("");
}
else
{
// video results
if (imageResults.Value.Count > 0)
{
for (int i = 0; i < num_videos; i++)
{
urlList.Add(imageResults.Value.ElementAt(i).ContentUrl);
}
}
else
urlList.Add("");
//Console.WriteLine($"Image result total estimated matches: {imageResults.TotalEstimatedMatches}");
//Console.WriteLine($"Image result next offset: {imageResults.NextOffset}");
//// Pivot suggestions
//if (imageResults.PivotSuggestions.Count > 0)
//{
// var firstPivot = imageResults.PivotSuggestions.First();
// Console.WriteLine($"Pivot suggestion count: {imageResults.PivotSuggestions.Count}");
// Console.WriteLine($"First pivot: {firstPivot.Pivot}");
// if (firstPivot.Suggestions.Count > 0)
// {
// var firstSuggestion = firstPivot.Suggestions.First();
// Console.WriteLine($"Suggestion count: {firstPivot.Suggestions.Count}");
// Console.WriteLine($"First suggestion text: {firstSuggestion.Text}");
// Console.WriteLine($"First suggestion web search url: {firstSuggestion.WebSearchUrl}");
// }
// else
// {
// Console.WriteLine("Couldn't find suggestions!");
// }
//}
//else
//{
// Console.WriteLine("Couldn't find pivot suggestions!");
//}
//// Query expansions
//if (imageResults.QueryExpansions.Count > 0)
//{
// var firstQueryExpansion = imageResults.QueryExpansions.First();
// Console.WriteLine($"Query expansion count: {imageResults.QueryExpansions.Count}");
// Console.WriteLine($"First query expansion text: {firstQueryExpansion.Text}");
// Console.WriteLine($"First query expansion search link: {firstQueryExpansion.SearchLink}");
//}
//else
//{
// Console.WriteLine("Couldn't find query expansions!");
//}
}
}
catch (Exception ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
return urlList;
}
}
}