protected override async Task <ImageFetchResult> FetchImage(ImageFetchQuery model) { var tags = model.Query .Trim() .Split(new[] { ' ' }, 20); var tagsParam = string.Join(",", tags); var requestParams = HttpUtility.ParseQueryString(string.Empty); requestParams["method"] = "flickr.photos.search"; requestParams["api_key"] = AppSetting.FlickrApiKey; requestParams["format"] = "json"; requestParams["nojsoncallback"] = "1"; requestParams["tags"] = tagsParam; requestParams["sort"] = "relevance"; if (model.Take.HasValue) { requestParams["per_page"] = Math.Min(model.Take.Value, 500).ToString(); } if (model.Skip.HasValue) { requestParams["page"] = Math.Max(model.Skip.Value, 1).ToString(); } string jsonString; using (var client = new HttpClient()) { var task = await client.GetAsync($"{AppSetting.FlickrApiUrl}?{requestParams}"); if (task?.Content == null) { throw new ImageFetchException("No response from the API", _name); } jsonString = await task.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(jsonString)) { throw new ImageFetchException("Error when reading the response from the API", _name); } } var search = JsonDeserialize <FlickrJson>(jsonString, _name); var result = new ImageFetchResult { ImageItems = search?.Photos?.Photo?.Select(i => new ImageItem { Link = string.Format(AppSetting.FlickrPhotoUrl, i.Farm, i.Server, i.Id, i.Secret), Title = i.Title }), TotalCount = search?.Photos?.Total.TryToInt64() }; return(result); }
protected override async Task <ImageFetchResult> FetchImage(ImageFetchQuery model) { var requestParams = HttpUtility.ParseQueryString(string.Empty); requestParams["query"] = model.Query; requestParams["sort"] = "relevance"; requestParams["license[0]"] = "commercial"; requestParams["license[1]"] = "editorial"; requestParams["license[2]"] = "enhanced"; requestParams["license[3]"] = "sensitive"; requestParams["license[4]"] = "NOT enhanced"; requestParams["license[5]"] = "NOT sensitive"; if (model.Take.HasValue) { requestParams["per_page"] = Math.Min(model.Take.Value, 500).ToString(); } if (model.Skip.HasValue) { requestParams["page"] = Math.Max(model.Skip.Value, 1).ToString(); } string jsonString; using (var client = new HttpClient()) { var credentials = AppSetting.ShutterstockCredentials; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); client.DefaultRequestHeaders.Add("User-Agent", "ISP"); var task = await client.GetByteArrayAsync($"{AppSetting.ShutterstockApiUrl}?{requestParams}"); if (task == null) { throw new ImageFetchException("No response from the API", _name); } jsonString = Encoding.UTF8.GetString(task); } var search = JsonDeserialize <ShutterstockJson>(jsonString, _name); var result = new ImageFetchResult { ImageItems = search?.Data?.Select(i => new ImageItem { Link = i.Assets?.Preview?.Url, Title = i.Description }), TotalCount = search?.TotalCount }; return(result); }
protected override async Task <ImageFetchResult> FetchImage(ImageFetchQuery model) { var singleTag = model.Query .Trim() .Split(' ') .FirstOrDefault(); if (string.IsNullOrWhiteSpace(singleTag)) { throw new ImageFetchException("API requires a single tag", _name); } var requestParams = HttpUtility.ParseQueryString(string.Empty); requestParams["access_token"] = AppSetting.InstagramAccessToken; if (model.Take.HasValue) { requestParams["count"] = model.Take.Value.ToString(); } var apiUrlWithTag = string.Format(AppSetting.InstagramApiUrl, singleTag); string jsonString; using (var client = new HttpClient()) { var task = await client.GetAsync($"{apiUrlWithTag}?{requestParams}"); if (task?.Content == null) { throw new ImageFetchException("No response from the API", _name); } jsonString = await task.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(jsonString)) { throw new ImageFetchException("Error when reading the response from the API", _name); } } var search = JsonDeserialize <InstagramJson>(jsonString, _name); var result = new ImageFetchResult { ImageItems = search?.Data?.Select(i => new ImageItem { Link = i.Images?.StandardResolution?.Url, Title = i.Caption?.Text }) }; return(result); }
protected override async Task <ImageFetchResult> FetchImage(ImageFetchQuery model) { var requestParams = HttpUtility.ParseQueryString(string.Empty); requestParams["q"] = model.Query; if (model.Take.HasValue) { requestParams["count"] = Math.Min(model.Take.Value, 150).ToString(); } if (model.Skip.HasValue) { requestParams["offset"] = Math.Max(model.Skip.Value, 0).ToString(); } string jsonString; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AppSetting.BingApiKey); var task = await client.GetAsync($"{AppSetting.BingApiUrl}?{requestParams}"); if (task == null) { throw new ImageFetchException("No response from the API", _name); } jsonString = await task.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(jsonString)) { throw new ImageFetchException("Error when reading the response from the API", _name); } } var search = JsonDeserialize <BingJson>(jsonString, _name); var result = new ImageFetchResult { ImageItems = search?.Value?.Select(i => new ImageItem { Link = i.ContentUrl, Title = i.Name }), TotalCount = search?.TotalEstimatedMatches }; return(result); }
protected override async Task <ImageFetchResult> FetchImage(ImageFetchQuery model) { var service = new CustomsearchService(new BaseClientService.Initializer { ApiKey = AppSetting.GoogleApiKey }); var request = service.Cse.List(model.Query); request.Cx = AppSetting.GoogleEngineId; request.SearchType = CseResource.ListRequest.SearchTypeEnum.Image; request.Fields = "items(link,title),searchInformation"; if (model.Skip.HasValue) { request.Start = Math.Max(model.Skip.Value, 1); } if (model.Take.HasValue) { request.Num = Math.Min(model.Take.Value, 10); } Search search; try { search = await request.ExecuteAsync(); } catch (Exception ex) { throw new ImageFetchException($"Error when reading the response from the API: {ex.Message}", _name); } var result = new ImageFetchResult { ImageItems = search?.Items?.Select(i => new ImageItem { Link = i.Link, Title = i.Title }), TotalCount = search?.SearchInformation?.TotalResults ?? search?.Items?.Count ?? 0, Time = search?.SearchInformation?.SearchTime ?? default(double) }; return(result); }