forked from microsoft/Cognitive-Samples-IntelligentKiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingSearchHelper.cs
352 lines (306 loc) · 14.7 KB
/
BingSearchHelper.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace ServiceHelpers
{
public class BingSearchHelper
{
private static string ImageSearchEndPoint = "https://api.cognitive.microsoft.com/bing/v7.0/images/search";
private static string ImageInsightsEndPoint = "https://api.cognitive.microsoft.com/bing/v7.0/images/details";
private static string AutoSuggestionEndPoint = "https://api.cognitive.microsoft.com/bing/v7.0/suggestions";
private static string NewsSearchEndPoint = "https://api.cognitive.microsoft.com/bing/v7.0/news/search";
private static int RetryCountOnQuotaLimitError = 6;
private static int RetryDelayOnQuotaLimitError = 500;
private static HttpClient autoSuggestionClient { get; set; }
private static HttpClient searchClient { get; set; }
private static string autoSuggestionApiKey;
public static string AutoSuggestionApiKey
{
get { return autoSuggestionApiKey; }
set
{
var changed = autoSuggestionApiKey != value;
autoSuggestionApiKey = value;
if (changed)
{
InitializeBingClients();
}
}
}
private static string searchApiKey;
public static string SearchApiKey
{
get { return searchApiKey; }
set
{
var changed = searchApiKey != value;
searchApiKey = value;
if (changed)
{
InitializeBingClients();
}
}
}
private static void InitializeBingClients()
{
autoSuggestionClient = new HttpClient();
autoSuggestionClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AutoSuggestionApiKey);
searchClient = new HttpClient();
searchClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SearchApiKey);
}
private static async Task<HttpResponseMessage> RequestAndAutoRetryWhenThrottled(Func<Task<HttpResponseMessage>> action)
{
int retriesLeft = BingSearchHelper.RetryCountOnQuotaLimitError;
int delay = BingSearchHelper.RetryDelayOnQuotaLimitError;
HttpResponseMessage response = null;
while (true)
{
response = await action();
if ((int)response.StatusCode == 429 && retriesLeft > 0)
{
await Task.Delay(delay);
retriesLeft--;
delay *= 2;
continue;
}
else
{
break;
}
}
return response;
}
public static async Task<IEnumerable<string>> GetImageSearchResults(string query, string imageContent = "Face", int count = 20, int offset = 0)
{
List<string> urls = new List<string>();
var result = await RequestAndAutoRetryWhenThrottled(() => searchClient.GetAsync(string.Format("{0}?q={1}&safeSearch=Strict&imageType=Photo&color=ColorOnly&count={2}&offset={3}{4}", ImageSearchEndPoint, WebUtility.UrlEncode(query), count, offset, string.IsNullOrEmpty(imageContent) ? "" : "&imageContent=" + imageContent)));
result.EnsureSuccessStatusCode();
var json = await result.Content.ReadAsStringAsync();
dynamic data = JObject.Parse(json);
if (data.value != null && data.value.Count > 0)
{
for (int i = 0; i < data.value.Count; i++)
{
urls.Add(data.value[i].contentUrl.Value);
}
}
return urls;
}
public static async Task<IEnumerable<string>> GetAutoSuggestResults(string query, string market = "en-US")
{
List<string> suggestions = new List<string>();
var result = await RequestAndAutoRetryWhenThrottled(() => autoSuggestionClient.GetAsync(string.Format("{0}/?q={1}&mkt={2}", AutoSuggestionEndPoint, WebUtility.UrlEncode(query), market)));
result.EnsureSuccessStatusCode();
var json = await result.Content.ReadAsStringAsync();
dynamic data = JObject.Parse(json);
if (data.suggestionGroups != null && data.suggestionGroups.Count > 0 &&
data.suggestionGroups[0].searchSuggestions != null)
{
for (int i = 0; i < data.suggestionGroups[0].searchSuggestions.Count; i++)
{
suggestions.Add(data.suggestionGroups[0].searchSuggestions[i].displayText.Value);
}
}
return suggestions;
}
public static async Task<IEnumerable<NewsArticle>> GetNewsSearchResults(string query, int count = 20, int offset = 0, string market = "en-US")
{
List<NewsArticle> articles = new List<NewsArticle>();
var result = await RequestAndAutoRetryWhenThrottled(() => searchClient.GetAsync(string.Format("{0}/?q={1}&count={2}&offset={3}&mkt={4}", NewsSearchEndPoint, WebUtility.UrlEncode(query), count, offset, market)));
result.EnsureSuccessStatusCode();
var json = await result.Content.ReadAsStringAsync();
dynamic data = JObject.Parse(json);
if (data.value != null && data.value.Count > 0)
{
for (int i = 0; i < data.value.Count; i++)
{
articles.Add(new NewsArticle
{
Title = data.value[i].name,
Url = data.value[i].url,
Description = data.value[i].description,
ThumbnailUrl = data.value[i].image?.thumbnail?.contentUrl,
Provider = data.value[i].provider?[0].name
});
}
}
return articles;
}
private static async Task<HttpResponseMessage> CallBingImageInsightsAsync(string imgUrl, string module)
{
var result = await RequestAndAutoRetryWhenThrottled(() => searchClient.GetAsync(string.Format("{0}?imgUrl={1}&modules={2}", ImageInsightsEndPoint, WebUtility.UrlEncode(imgUrl), module)));
result.EnsureSuccessStatusCode();
return result;
}
private static async Task<HttpResponseMessage> CallBingImageInsightsAsync(Stream stream, string module)
{
var strContent = new StreamContent(stream);
strContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = "AnyNameWorks" };
var content = new MultipartFormDataContent();
content.Add(strContent);
var result = await RequestAndAutoRetryWhenThrottled(() => searchClient.PostAsync(string.Format("{0}?modules={1}", ImageInsightsEndPoint, module), content));
result.EnsureSuccessStatusCode();
return result;
}
public static async Task<IEnumerable<VisualSearchCelebrityResult>> GetVisuallySimilarCelebrities(string imgUrl)
{
var result = await CallBingImageInsightsAsync(imgUrl, "RecognizedEntities");
return ParseCelebrityResults(await result.Content.ReadAsStringAsync());
}
public static async Task<IEnumerable<VisualSearchCelebrityResult>> GetVisuallySimilarCelebrities(Stream stream)
{
var result = await CallBingImageInsightsAsync(stream, "RecognizedEntities");
return ParseCelebrityResults(await result.Content.ReadAsStringAsync());
}
private static List<VisualSearchCelebrityResult> ParseCelebrityResults(string json)
{
List<VisualSearchCelebrityResult> results = new List<VisualSearchCelebrityResult>();
dynamic data = JObject.Parse(json);
if (data.recognizedEntityGroups != null && data.recognizedEntityGroups.value.Count > 0)
{
for (int i = 0; i < data.recognizedEntityGroups.value.Count; i++)
{
for (int j = 0; j < data.recognizedEntityGroups.value[i].recognizedEntityRegions.Count; j++)
{
for (int k = 0; k < data.recognizedEntityGroups.value[i].recognizedEntityRegions[j].matchingEntities.Count; k++)
{
dynamic entity = data.recognizedEntityGroups.value[i].recognizedEntityRegions[j].matchingEntities[k];
results.Add(new VisualSearchCelebrityResult
{
Name = entity.entity.name.Value,
SimilarityScore = Math.Round(entity.matchConfidence.Value, 2),
Occupation = entity.entity.jobTitle != null ? entity.entity.jobTitle.Value : "",
ReferenceUrl = entity.entity.image.hostPageUrl.Value,
ImageUrl = entity.entity.image.contentUrl.Value
});
}
}
}
}
return results;
}
public static async Task<IEnumerable<VisualSearchPhotoResult>> GetVisuallySimilarImages(string imgUrl)
{
var result = await CallBingImageInsightsAsync(imgUrl, "SimilarImages");
return ParsePhotoResults(await result.Content.ReadAsStringAsync());
}
public static async Task<IEnumerable<VisualSearchPhotoResult>> GetVisuallySimilarImages(Stream stream)
{
var result = await CallBingImageInsightsAsync(stream, "SimilarImages");
return ParsePhotoResults(await result.Content.ReadAsStringAsync());
}
private static List<VisualSearchPhotoResult> ParsePhotoResults(string json)
{
List<VisualSearchPhotoResult> results = new List<VisualSearchPhotoResult>();
dynamic data = JObject.Parse(json);
if (data.visuallySimilarImages != null && data.visuallySimilarImages.value.Count > 0)
{
for (int i = 0; i < data.visuallySimilarImages.value.Count; i++)
{
results.Add(new VisualSearchPhotoResult
{
ImageUrl = data.visuallySimilarImages.value[i].thumbnailUrl.Value
});
}
}
return results;
}
public static async Task<IEnumerable<VisualSearchProductResult>> GetVisuallySimilarProducts(string imgUrl)
{
var result = await CallBingImageInsightsAsync(imgUrl, "SimilarProducts");
return ParseProductResults(await result.Content.ReadAsStringAsync());
}
public static async Task<IEnumerable<VisualSearchProductResult>> GetVisuallySimilarProducts(Stream stream)
{
var result = await CallBingImageInsightsAsync(stream, "SimilarProducts");
return ParseProductResults(await result.Content.ReadAsStringAsync());
}
private static List<VisualSearchProductResult> ParseProductResults(string json)
{
List<VisualSearchProductResult> products = new List<VisualSearchProductResult>();
dynamic data = JObject.Parse(json);
if (data.visuallySimilarProducts != null && data.visuallySimilarProducts.value.Count > 0)
{
for (int i = 0; i < data.visuallySimilarProducts.value.Count; i++)
{
dynamic prod = data.visuallySimilarProducts.value[i];
if (prod?.insightsMetadata?.aggregateOffer?.priceCurrency != null && prod?.insightsMetadata?.aggregateOffer?.lowPrice != null)
{
products.Add(new VisualSearchProductResult
{
Name = prod.insightsMetadata.aggregateOffer.name.Value,
ImageUrl = prod.thumbnailUrl.Value,
ReferenceUrl = prod.hostPageUrl.Value,
Price = string.Format("{0} {1}", prod.insightsMetadata.aggregateOffer.priceCurrency.Value, prod.insightsMetadata.aggregateOffer.lowPrice.Value)
});
}
}
}
return products;
}
}
public class NewsArticle
{
public string Title { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public string ThumbnailUrl { get; set; }
public string Provider { get; set; }
}
public abstract class VisualSearchResult
{
public string ImageUrl { get; set; }
public string ReferenceUrl { get; set; }
}
public class VisualSearchPhotoResult : VisualSearchResult
{
}
public class VisualSearchProductResult : VisualSearchResult
{
public string Name { get; set; }
public string Price { get; set; }
}
public class VisualSearchCelebrityResult : VisualSearchResult
{
public string Name { get; set; }
public string Occupation { get; set; }
public double SimilarityScore { get; set; }
}
}