static string GetPicturesByTopic(string topic, bool findMoreGlobal = true) { try { string url = $"https://pixabay.com/api/?key={PicturesAPIKEY}&q={topic}+город&image_type=photo&orientation=horizontal&category=places&pretty=true"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); string response; using (StreamReader stream = new StreamReader(httpWebResponse.GetResponseStream())) { response = stream.ReadToEnd(); } PicturesByTopic picturesByTopic = JsonConvert.DeserializeObject <PicturesByTopic>(response); Random rnd = new Random(); return((picturesByTopic.hits.Count > 0) ? picturesByTopic.hits[rnd.Next(0, picturesByTopic.hits.Count)].webformatURL : picturesByTopic.hits.First().webformatURL); } catch (InvalidOperationException) { return(null); } catch (Exception e) { Console.WriteLine(e.Message + "\n" + e.Source + "\n" + e.StackTrace); Console.WriteLine(new string('-', 140)); return(null); } }
public async Task <string> Get(string topic) { try { JObject search = JObject.Parse(await Gets($"https://pixabay.com/api/?key=11670931-bb66861cd97f09841fe543406&q={topic}+город&image_type=photo&orientation=horizontal&category=places&pretty=true")); PicturesByTopic picturesByTopic = JsonConvert.DeserializeObject <PicturesByTopic>(search.ToString()); if (picturesByTopic.hits.Count == 0) { return("null"); } Random rnd = new Random(); return((picturesByTopic.hits.Count > 0) ? picturesByTopic.hits[rnd.Next(0, picturesByTopic.hits.Count)].webformatURL : picturesByTopic.hits.First().webformatURL); } catch (Exception e) { return(null); } }