private async Task <Video> AddLocalizationToVideo(Video video, AppVideoLocalizeRequest body, string localizationCountHash) { TranslationServiceClient service = await translateServiceAccessor.InitializeServiceAsync(); string[] languages = body.TranslationLanguageCollection; video.Snippet.DefaultLanguage ??= "en"; // TODO replace with language detection if (body.NullifyVideoLocalizations || video.Localizations == null) { video.Localizations = new Dictionary <string, VideoLocalization>(); } Task[] tasks = new Task[languages.Length]; for (int i = 0; i < languages.Length; i++) { string language = languages[i]; tasks[i] = AddLocalizationToVideoTask(service, video, language, localizationCountHash); } await Task.WhenAll(tasks); // wait for all localizations to be added return(video); }
public async Task <ActionResult <string> > LocalizeVideo([Required, FromBody] AppVideoLocalizeRequest body) { string localizationCountHash = Guid.NewGuid().ToString(); intStore[localizationCountHash] = 0; string[] videos = body.MineChannelVideoUploadCollection; Task <Video>[] tasks = new Task <Video> [videos.Length]; string userId = User.GetLocalAuthorityNameIdentifier(); YouTubeService service = await youtubeServiceAccessor.InitializeServiceAsync(userId); VideosResource.ListRequest request = service.Videos.List(VIDEO_LOCALIZE_PART); request.Id = string.Join(',', videos); int i = 0; do { VideoListResponse response = await request.ExecuteAsync(); IList <Video> items = response.Items; foreach (Video item in items) { tasks[i++] = LocalizeVideoTask(item, body, localizationCountHash, service); // All other tasks begin their life cycle in a hot state. } request.PageToken = response.NextPageToken; } while (request.PageToken != null); _ = Task.WhenAll(tasks); // errors will not be caught because no await return(new ActionResult <string>(localizationCountHash)); }
private async Task <Video> LocalizeVideoTask(Video video, AppVideoLocalizeRequest body, string localizationCountHash, YouTubeService service) { video = await AddLocalizationToVideo(video, body, localizationCountHash); // wait for all localizations to be added video = await UpdateVideo(video, VIDEO_LOCALIZE_PART, service); // update the video return(video); }