예제 #1
0
 //--- Default constructor ---------------------------------------------------
 /// <summary>
 /// Create empty picture of the day object
 /// </summary>
 public APOD()
 {
     //nothing special here...
     VideoType     = NasaVideoType.NONE;
     IsDownloading = false;
 }
예제 #2
0
 //--- Date-based constructor ---------------------------------------------------
 /// <summary>
 /// Create picture of the date object for given date
 /// </summary>
 /// <param name="apiDate">Date of picture of the day</param>
 public APOD(DateTime apiDate)
 {
     SetApiDate(apiDate);
     VideoType     = NasaVideoType.NONE;
     IsDownloading = false;
 }
예제 #3
0
        /// <summary>
        /// Set given date for APOD API
        /// </summary>
        /// <param name="apiDate">API date</param>
        public void SetApiDate(DateTime apiDate)
        {
            //Don't go below minimum date
            if (apiDate < _DATE_MIN)
            {
                apiDate = _DATE_MIN;
            }

            //Double check API key and fall back to default if needed
            if (_apiKey == null || _apiKey == string.Empty || _apiKey.Length != 40)
            {
                if (_apiKey != "DEMO_KEY")
                {
                    _apiKey = _apiKeyDefault;
                }
            }

            //Create API URL
            string _apiURL = _baseURL;

            _apiURL += "?api_key=" + _apiKey;
            _apiURL += "&hd=true";
            _apiURL += "&date=" + apiDate.Year + "-";
            if (apiDate.Month < 10)
            {
                _apiURL += "0" + apiDate.Month + "-";
            }
            else
            {
                _apiURL += apiDate.Month + "-";
            }
            if (apiDate.Day < 10)
            {
                _apiURL += "0" + apiDate.Day;
            }
            else
            {
                _apiURL += apiDate.Day;
            }

            //Call websvc and strip json to local vars
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                using WebClient wc = new();
                JsonDeserialize(wc.DownloadString(_apiURL));
                ApiDate = apiDate;

                //Set media type flags
                if (MediaType == "image")
                {
                    IsImage   = true;
                    VideoType = NasaVideoType.NONE;
                }
                else
                {
                    IsImage   = false;
                    VideoType = NasaVideoType.NONE;
                    if (Url.Contains(VID_TYPE_YT) || HdUrl.Contains(VID_TYPE_YT))
                    {
                        VideoType = NasaVideoType.YOUTUBE;
                    }
                    if (Url.Contains(VID_TYPE_VM) || HdUrl.Contains(VID_TYPE_VM))
                    {
                        VideoType = NasaVideoType.VIMEO;
                    }
                }
            }
            catch (Exception e)
            {
                Copyright      = string.Empty;
                Date           = string.Empty;
                Explanation    = string.Empty;
                HdUrl          = string.Empty;
                MediaType      = string.Empty;
                ServiceVersion = string.Empty;
                Title          = string.Empty;
                Url            = string.Empty;
                ApiDate        = apiDate;
                IsImage        = false;
                VideoType      = NasaVideoType.NONE;
                throw e;
            }
        }