internal override void Run()
        {
            progress = 0;

            if (Video.AddToPlaylist && !string.IsNullOrWhiteSpace(Video.PlaylistId))
            {
                LOGGER.Info($"Adding video '{Video.Title}' to playlist with id '{Video.PlaylistId}'");

                var request = HttpWebRequestCreator.CreateWithAuthHeader("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet", "POST", Account.GetActiveToken());
                request.ContentType = "application/json";

                YoutubePlaylistItem resource = new YoutubePlaylistItem(Video.PlaylistId, Video.Id);
                var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(resource));

                var response = WebService.Communicate(request, bytes);
                Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(response);

                if (!Status.QuotaReached)
                {
                    LOGGER.Info($"Video '{Video.Title}' was successfully added to playlist with id '{Video.PlaylistId}'");

                    FinishedSuccessful = true;
                    progress           = 100;
                }
            }
            else
            {
                LOGGER.Info($"Skipping playlist add since it is either not wanted or there is no playlist id");

                FinishedSuccessful = true;
                progress           = 100;
            }

            OnStepFinished();
        }
        internal override void Run()
        {
            if (File.Exists(Video.ThumbnailPath))
            {
                LOGGER.Info($"Uploading thumbnail of path '{Video.ThumbnailPath}'");

                HttpWebRequest request = CreateThumbnailUploadRequest();

                LOGGER.Info($"Uploading thumbnail to '{request.Method} {request.RequestUri}'");

                Upload(Video.ThumbnailPath, request);

                if (FinishedSuccessful)
                {
                    LOGGER.Info($"Finishing thumbnail upload request");

                    request.Headers.Set("Authorization", $"Bearer {Account.GetActiveToken()}");
                    var thumbnailResource = WebService.Communicate(request);

                    LOGGER.Info($"Thumbnail upload of file '{Video.ThumbnailPath}' finished successfully");

                    Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(thumbnailResource);
                }
            }
            else
            {
                LOGGER.Warn($"Skipping thumbnail upload since a thumbnail did not exist or maybe should not be uploaded. Thumbnail path: '{Video.ThumbnailPath}'");

                // Keine Datei -> Upload war erfolgreich
                FinishedSuccessful = true;
            }

            OnStepFinished();
        }
Exemple #3
0
        internal override void Run()
        {
            LOGGER.Info($"Changing video details for video '{Video.Title}'");

            progress = 0;

            var request = HttpWebRequestCreator.CreateWithAuthHeader("https://www.googleapis.com/youtube/v3/videos?part=snippet,status", "PUT", Account.GetActiveToken());

            request.ContentType = "application/json";

            SerializableYoutubeVideo resource = SerializableYoutubeVideo.Create(Video);
            var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(resource));

            var response = WebService.Communicate(request, bytes);

            Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(response);

            if (!Status.QuotaReached)
            {
                LOGGER.Info($"Video details for video '{Video.Title}' were successfully changed");

                FinishedSuccessful = true;
                progress           = 100;
            }

            OnStepFinished();
        }
Exemple #4
0
        private long CheckUploadStatus()
        {
            LOGGER.Info($"Checking upload status");

            var request = HttpWebRequestCreator.CreateWithAuthHeader(Status.UploadAddress.AbsoluteUri, "PUT", Account.GetActiveToken());

            request.ContentLength = 0;
            request.Headers.Add($"content-range: bytes */{Video.File.Length}");

            WebException ex     = null;
            var          answer = WebService.Communicate(request, out ex);

            if (answer == null)
            {
                if (ex?.Response != null && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    // Upload kann nicht fortgesetzt werden, da Verarbeitung mittlerweile abgebrochen wurde.
                    // Workaround: Upload neu starten
                    LOGGER.Error($"Youtube upload cannot be continued because it was paused for too long", ex);
                    return(-2);
                }
                else if (ex?.Response != null && (int)((HttpWebResponse)ex.Response).StatusCode != 308)
                {
                    // Es gab einen anderen unerwarteten Fehler.
                    // Auch hier ist der Workaround ein Neustart.
                    LOGGER.Error($"Youtube upload cannot be continued because an exceptioin occured", ex);
                    return(-3);
                }
                else
                {
                    LOGGER.Info($"Youtube upload should be started from the beginning");
                    return(-1);
                }
            }

            Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(answer);

            if (Status.QuotaReached)
            {
                return(-4);
            }

            answer = answer.Substring("bytes=".Length);

            long lastbyte;

            try
            {
                lastbyte = Convert.ToInt64(answer.Split('-')[1]);
                LOGGER.Info($"Upload can be continued from byte {lastbyte} onwards");
            }
            catch (Exception exc)
            {
                LOGGER.Error($"lastbyte could not be parsed - youtube upload should be started from the beginning", exc);
                return(-1);
            }

            return(lastbyte);
        }
Exemple #5
0
        internal override void Run()
        {
            LOGGER.Info($"Uploading video file '{Video.Path}' to youtube");

            // Initialisieren
            GenerateInitUri();
            var request = CreateRequest();

            if (request == null && !Status.QuotaReached)
            {
                LOGGER.Warn($"Upload could not be continued or old upload address was not valid anymore - restarting upload");
                LOGGER.Debug($"Old upload address: '{Status.UploadAddress}'");

                // evtl. vorhandene UploadUri hat nicht geklappt => Versuch, den Upload von vorne zu beginnen.
                Status.UploadAddress = null;
                GenerateInitUri();
                request = CreateRequest();
            }

            // Hochladen
            if (!Status.QuotaReached && request != null && File.Exists(Video.Path))
            {
                LOGGER.Info($"Uploading video file to '{request.Method} {request.RequestUri}'");

                Upload(Video.Path, request);

                if (FinishedSuccessful)
                {
                    request.Headers.Set("Authorization", $"Bearer {Account.GetActiveToken()}");
                    string result = WebService.Communicate(request);

                    Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(result);

                    if (!Status.QuotaReached)
                    {
                        Video.Id = JsonConvert.DeserializeObject <SerializableYoutubeVideo>(result).id;

                        LOGGER.Info($"Upload finished successful - video is now on youtube with id {Video.Id} - url: https://youtube.com/watch?v={Video.Id}");

                        // Status entfernen, damit nicht erneut an die selbe Adresse hochgeladen wird.
                        Status.UploadAddress = null;
                    }
                }
                else
                {
                    LOGGER.Error($"Upload did not finish successful");
                }
            }

            OnStepFinished();
        }
Exemple #6
0
        private void GenerateInitUri()
        {
            if (Status.UploadAddress == null && !Status.QuotaReached)
            {
                LOGGER.Info($"Creating upload uri");
                string result = InitializeUploadOnYoutube();

                Status.QuotaReached = QuotaProblemHandler.IsQuotaLimitReached(result);

                Uri uri = null;
                if (!Status.QuotaReached && Uri.TryCreate(result, UriKind.Absolute, out uri))
                {
                    LOGGER.Info($"Upload uri was created: '{uri}'");
                    Status.UploadAddress = uri;
                }
            }
        }