Example #1
0
        public void CancelPublish()
        {
            Cancel();

            try
            {
                if (_stream != null)
                {
                    _stream.Cancel();
                    _stream.Dispose();
                    _stream = null;
                }
            }
            catch (NullReferenceException)
            {
            }

            if (!string.IsNullOrEmpty(_updateUrl))
            {
                HttpWebRequest req = HttpRequestHelper.CreateHttpWebRequest(_updateUrl, true, -1, -1);
                YouTubeUploadRequestHelper.AddSimpleHeader(req, _authToken);
                req.Method = "DELETE";
                req.GetResponse().Close();
            }
        }
Example #2
0
        private static Stream CallYouTubeApi(string requestUrl, int timeoutMs)
        {
            // download the document
            Stream responseStream;

            try
            {
                HttpWebRequest req = HttpRequestHelper.CreateHttpWebRequest(requestUrl, true, timeoutMs, timeoutMs);
                YouTubeUploadRequestHelper.AddSimpleHeader(req, YouTubeAuth.Instance.AuthToken);
                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream responseStreamOrginal = response.GetResponseStream())
                    {
                        responseStream = StreamHelper.CopyToMemoryStream(responseStreamOrginal);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new YouTubeException("YouTube Error", "Unable to download request from YouTube due to the following error: " + ex.Message);
            }

            // check for timeout
            if (responseStream == null)
            {
                throw new YouTubeException("Request Timed Out", "The request to YouTube timed out (the service may be unavailable right now).");
            }
            else
            {
                return(responseStream);
            }
        }
Example #3
0
        private bool IsVideoComepleted()
        {
            // Send a request to get the status
            HttpWebRequest req = HttpRequestHelper.CreateHttpWebRequest(_updateUrl, true, -1, -1);

            YouTubeUploadRequestHelper.AddSimpleHeader(req, _authToken);
            string innerResult;

            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    innerResult = responseReader.ReadToEnd();
                }
            }

            // Find the status elemnt
            XmlDocument xmlDoc = new XmlDocument(NamespaceManager.NameTable);

            xmlDoc.LoadXml(innerResult);
            XmlNode stateNode = xmlDoc.SelectSingleNode("//atom:entry/app:control/yt:state", NamespaceManager);

            // If there is no element with status, then the video is completed
            if (stateNode == null)
            {
                _status  = VideoPublishStatus.Completed;
                _message = Res.Get("Video" + _status);
                return(true);
            }

            // Check to make sure the video doesnt have a bad status
            string value = stateNode.Attributes["name"] != null ? stateNode.Attributes["name"].Value : "";

            if (value == "rejected" || value == "failed")
            {
                throw new Exception(Res.Get("YouTube" + stateNode.Attributes["reasonCode"].Value));
            }

            return(false);
        }
Example #4
0
        private string Upload()
        {
            HttpWebRequest req = HttpRequestHelper.CreateHttpWebRequest("http://uploads.gdata.youtube.com/feeds/api/users/default/uploads", true, -1, -1);

            YouTubeUploadRequestHelper uploader = new YouTubeUploadRequestHelper(req);

            uploader.AddHeader(_authToken, Path.GetFileName(Path.GetFileName(_filePath)));
            uploader.Open();
            uploader.AddXmlRequest(_title, _description, _tags, _categoryId, _permissionValue);
            uploader.AddFile(_filePath);
            uploader.Close();

            try
            {
                using (_stream = new CancelableStream(new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    uploader.SendRequest(_stream);
                }
            }
            finally
            {
                _stream = null;
            }



            string result;

            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    result = responseReader.ReadToEnd();
                }
            }


            return(result);
        }
        private string Upload()
        {
            HttpWebRequest req = HttpRequestHelper.CreateHttpWebRequest("http://uploads.gdata.youtube.com/feeds/api/users/default/uploads", true, -1, -1);

            YouTubeUploadRequestHelper uploader = new YouTubeUploadRequestHelper(req);
            uploader.AddHeader(_authToken, Path.GetFileName(Path.GetFileName(_filePath)));
            uploader.Open();
            uploader.AddXmlRequest(_title, _description, _tags, _categoryId, _permissionValue);
            uploader.AddFile(_filePath);
            uploader.Close();

            try
            {
                using (_stream = new CancelableStream(new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    uploader.SendRequest(_stream);
                }
            }
            finally
            {
                _stream = null;
            }

            string result;
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    result = responseReader.ReadToEnd();
                }
            }

            return result;
        }