Object that contains the basic properties that are returned in the XML data from NASA.
Exemplo n.º 1
0
        /// <summary>
        /// Overloaded GetImage that allows the retrieval of previous images based on their position in the XML document
        /// provided from the nasa.gov website.
        /// </summary>
        /// <param name="selectedOffset">Position of the image within the node list of images, 1 being the very first (newest)</param>
        /// <param name="selectedPage">Page number to retrieve the page that contains the selected image.</param>
        public BackgroundImage GetImage(int? selectedOffset, int? selectedPage)
        {
            try
             {
            if (!NetworkHelper.InternetAccessIsAvailable())
               throw new Exception("Attempted to retrieve image, but internet connection was not available.");

            if (GlobalVariables.LoggingEnabled) ExceptionManager.WriteInformation("Retrieving system screen resolution");
            GetCurrentScreenResolution();
            if (GlobalVariables.LoggingEnabled) ExceptionManager.WriteInformation(string.Format("System screen resolution is: {0}", _currentScreenResolution));

            if (selectedOffset == null)
               selectedOffset = DefaultimageOffset;

            //Get the JSON string data
            if (GlobalVariables.LoggingEnabled) ExceptionManager.WriteInformation("Preparing to download image information");
            var nasaImages = JsonHelper.DownloadSerializedJsonData(string.Format(NasaLatestImagesUrl, selectedPage));
            if (nasaImages == null || nasaImages.UberNodes.Length == 0)
               throw new Exception("Unable to retrieve image data from JSON request");

            //Get the image node
            var imageId = nasaImages.UberNodes[(int) selectedOffset].UberNodeId;

            var currentImageFullUrl = string.Format("{0}{1}.json", NasaApiUrl, imageId);

            var currentImage = JsonHelper.DownloadImageData(currentImageFullUrl);
            if (currentImage == null)
               throw new Exception(string.Format("Unable to retrieve selected image: {0}", currentImageFullUrl));

            if (GlobalVariables.LoggingEnabled) ExceptionManager.WriteInformation("Selecting image based on current screen resolution");
            switch (_currentScreenResolution)
            {
               case "100x75":
               case "226x170":
                  currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].LrThumbnail);
                  break;
               case "346x260":
               case "360x225":
               case "466x248":
               case "430x323":
                  currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop1X1);
                  break;
               case "800x600":
                  currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop2X1);
                  break;
               default:
                  currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].FullWidthFeature);
                  break;
            }

            var localImageFolderPath = string.Format("{0}\\NASA\\PicOfTheDay", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

            if (!Directory.Exists(localImageFolderPath))
            {
               Directory.CreateDirectory(localImageFolderPath);
            }

            var imageName = currentImage.ImageData[0].FileName;

            //Setting the full local image path to save the file
            var fullImagePath = string.Format("{0}\\{1}", localImageFolderPath, imageName);

            //Download the current image
            if (GlobalVariables.LoggingEnabled) ExceptionManager.WriteInformation(string.Format("Preparing to download image: {0}", currentImageFullUrl));
            if (!DownloadHelper.DownloadImage(fullImagePath, currentImageFullUrl))
               throw new Exception("Error downloading current image.");

            //Create BackgroundImage object
            var backgroundImage = new BackgroundImage
            {
               ImageUrl = currentImageFullUrl,
               ImageDate = Convert.ToDateTime(currentImage.UberData.PromoDateTime),
               ImageDescription = StripHtml(currentImage.UberData.Body),
               DownloadedPath = fullImagePath,
               ImageTitle =
                  !string.IsNullOrEmpty(currentImage.ImageData[0].Title)
                     ? currentImage.ImageData[0].Title
                     : currentImage.UberData.Title
            };

            return backgroundImage;
             }
             catch (Exception ex)
             {
            ExceptionManager.WriteException(ex);
            return null;
             }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Overloaded GetImage that allows the retrieval of previous images based on their position in the XML document
        /// provided from the nasa.gov website.
        /// </summary>
        /// <param name="selectedOffset">Position of the image within the node list of images, 1 being the very first (newest)</param>
        /// <param name="selectedPage">Page number to retrieve the page that contains the selected image.</param>
        public BackgroundImage GetImage(int?selectedOffset, int?selectedPage)
        {
            try
            {
                if (!NetworkHelper.InternetAccessIsAvailable())
                {
                    throw new Exception("Attempted to retrieve image, but internet connection was not available.");
                }

                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Retrieving system screen resolution");
                }
                GetCurrentScreenResolution();
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation(string.Format("System screen resolution is: {0}", _currentScreenResolution));
                }

                if (selectedOffset == null)
                {
                    selectedOffset = DefaultimageOffset;
                }

                //Get the JSON string data
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Preparing to download image information");
                }
                var nasaImages = JsonHelper.DownloadSerializedJsonData(string.Format(NasaLatestImagesUrl, selectedPage));
                if (nasaImages == null || nasaImages.UberNodes.Length == 0)
                {
                    throw new Exception("Unable to retrieve image data from JSON request");
                }

                //Get the image node
                var imageId = nasaImages.UberNodes[(int)selectedOffset].UberNodeId;

                var currentImageFullUrl = string.Format("{0}{1}.json", NasaApiUrl, imageId);

                var currentImage = JsonHelper.DownloadImageData(currentImageFullUrl);
                if (currentImage == null)
                {
                    throw new Exception(string.Format("Unable to retrieve selected image: {0}", currentImageFullUrl));
                }

                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Selecting image based on current screen resolution");
                }
                switch (_currentScreenResolution)
                {
                case "100x75":
                case "226x170":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].LrThumbnail);
                    break;

                case "346x260":
                case "360x225":
                case "466x248":
                case "430x323":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop1X1);
                    break;

                case "800x600":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop2X1);
                    break;

                default:
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].FullWidthFeature);
                    break;
                }

                var localImageFolderPath = string.Format("{0}\\NASA\\PicOfTheDay", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

                if (!Directory.Exists(localImageFolderPath))
                {
                    Directory.CreateDirectory(localImageFolderPath);
                }

                var imageName = currentImage.ImageData[0].FileName;

                //Setting the full local image path to save the file
                var fullImagePath = string.Format("{0}\\{1}", localImageFolderPath, imageName);

                //Download the current image
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation(string.Format("Preparing to download image: {0}", currentImageFullUrl));
                }
                if (!DownloadHelper.DownloadImage(fullImagePath, currentImageFullUrl))
                {
                    throw new Exception("Error downloading current image.");
                }

                //Create BackgroundImage object
                var backgroundImage = new BackgroundImage
                {
                    ImageUrl         = currentImageFullUrl,
                    ImageDate        = Convert.ToDateTime(currentImage.UberData.PromoDateTime),
                    ImageDescription = StripHtml(currentImage.UberData.Body),
                    DownloadedPath   = fullImagePath,
                    ImageTitle       =
                        !string.IsNullOrEmpty(currentImage.ImageData[0].Title)
                     ? currentImage.ImageData[0].Title
                     : currentImage.UberData.Title
                };

                return(backgroundImage);
            }
            catch (Exception ex)
            {
                ExceptionManager.WriteException(ex);
                return(null);
            }
        }