Exemple #1
0
        public async Task <BingUnit> GetTodayBingUnit(BingImageResolution res = BingImageResolution.PHONE_WVGA, string market = "en-US", double networkTimeout = 7)
        {
            BingUnit unit = new BingUnit();

            bingimageResolution = res;

            XDocument doc = null;

            // Form the URL based on market Since this will be run once per day only 1 image needs to be downloaded
            string url = string.Format(IMG_URL, NUMBER_OF_IMAGES, market);

            HttpClient client = new HttpClient();

            try
            {
#if true
                client.Timeout = TimeSpan.FromSeconds(networkTimeout);
                string data = await client.GetStringAsync(new Uri(url));
#else
                WebClient client = new WebClient();
                string    data   = await client.DownloadStringTask(new Uri(url));
#endif

                doc = XDocument.Parse(data);

                // Iterate through the image elements
                foreach (XElement image in doc.Descendants("image"))
                {
                    unit.Copyright = GetElmentValue(image, "copyright");
                    unit.StartDate = GetElmentValue(image, "startdate");
                    unit.EndDate   = GetElmentValue(image, "enddate");

                    string imageUrl = string.Empty;

                    if (bingimageResolution == BingImageResolution.DEFAULT)
                    {
                        //기본사이즈인 958x512사이즈의 경우, urlBase가 아닌 그냥 url값을 사용하면 된다.
                        imageUrl = GetElmentValue(image, "url");
                    }
                    else
                    {
                        imageUrl = string.Format("{0}_{1}{2}", image.Element("urlBase").Value, BingImageResolutionString[(int)bingimageResolution], BINGIMG_FORMAT);
                    }

                    unit.Path = BING_ROOT + imageUrl;

                    bool bSearched = false;
                    foreach (XElement hotspots in image.Descendants("hotspots"))
                    {
                        foreach (XElement hotspot in hotspots.Descendants("hotspot"))
                        {
                            string strLink = hotspot.Element("link").Value;
                            if (strLink.Contains("maps"))
                            {
                                //unit.MapLink = ReplaceMapLevel(strLink, "4");
                                unit.MapLink = strLink;
                                bSearched    = true;
                                break;
                            }
                        }

                        if (bSearched)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                client.CancelPendingRequests();
                client.Dispose();
                string message = ex.Message;
                return(null);
            }

            return(unit);
        }