// Retrieves the list of images from Bing.
        private string[] LoadImages(int count)
        {
            if (this._images != null)
            {
                return(this._images);
            }

            string url = String.Format("https://www.bing.com/HPImageArchive.aspx?format=js&n={0}&mkt=en-US", Math.Max(count, 1));

            try
            {
                using (WebClient client = new WebClient())
                {
                    var json = client.DownloadString(url);
                    if (!String.IsNullOrEmpty(json))
                    {
                        dynamic response = WisejSerializer.Parse(json);
                        if (response != null)
                        {
                            List <string> list = new List <string>();
                            foreach (dynamic img in response.images)
                            {
                                list.Add("https://www.bing.com" + img.url);
                            }
                            _images = list.ToArray();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("{0}: {1}", ex.GetType().FullName, ex.Message);
            }

            return(this._images);
        }
Beispiel #2
0
        internal void ParseResult(string result)
        {
            Result = result;
            // clear ???
            Description = String.Empty;
            Tags        = null;
            Captions    = null;
            Categories  = null;
            Faces       = null;
            Celebrities = null;

            if (!String.IsNullOrEmpty(Result))
            {
                dynamic json = WisejSerializer.Parse(Result);
                // description
                if (json.description != null)
                {
                    Description = json.description;
                    // captions
                    if (json.description.captions != null)
                    {
                        for (int i = 0; i < json.description.captions.Length; i++)
                        {
                            Captions[i] = json.description.captions[i].text;
                        }
                    }
                }
                // tags
                if (json.tags != null)
                {
                    for (int i = 0; i < json.tags.Length; i++)
                    {
                        Tags[i].name       = json.tags[i].name ?? null;
                        Tags[i].confidence = json.tags[i].confidence ?? null;
                    }
                }
                else if (json.description != null && json.description.tags != null)
                {
                    for (int i = 0; i < json.description.tags.Length; i++)
                    {
                        Tags[i].name = json.description.tags[i].name ?? null;
                    }
                }
                // categories
                if (json.categories != null)
                {
                    for (int i = 0; i < json.categories.Length; i++)
                    {
                        Categories[i].name  = json.categories[i].name ?? null;
                        Categories[i].score = json.categories[i].score ?? null;
                    }
                }
                // faces
                if (json.faces != null)
                {
                    for (int i = 0; i < json.faces.Length; i++)
                    {
                        Faces[i].gender               = json.faces[i].gender ?? null;
                        Faces[i].age                  = json.faces[i].age ?? null;
                        Faces[i].faceRectangle.left   = json.faces[i].faceRectangle.left ?? null;
                        Faces[i].faceRectangle.top    = json.faces[i].faceRectangle.top ?? null;
                        Faces[i].faceRectangle.width  = json.faces[i].faceRectangle.width ?? null;
                        Faces[i].faceRectangle.height = json.faces[i].faceRectangle.height ?? null;
                    }
                }
                // celebrities
                if (json.description != null && json.description.detail != null && json.description.detail.celebrities != null)
                {
                    for (int i = 0; i < json.description.detail.celebrities.Length; i++)
                    {
                        Celebrities[i].name                 = json.description.detail.celbrities[i].name ?? null;
                        Celebrities[i].confidence           = json.description.detail.celbrities[i].confidence ?? null;
                        Celebrities[i].faceRectangle.left   = json.description.detail.celbrities[i].faceRectangle.left ?? null;
                        Celebrities[i].faceRectangle.top    = json.description.detail.celbrities[i].faceRectangle.top ?? null;
                        Celebrities[i].faceRectangle.width  = json.description.detail.celbrities[i].faceRectangle.width ?? null;
                        Celebrities[i].faceRectangle.height = json.description.detail.celbrities[i].faceRectangle.height ?? null;
                    }
                }
                // metadata
                if (json.metadata != null)
                {
                    // TODO: check
                }

                // TODO: add landmarks, adult,
            }
        }