private async Task <ResourceId> FindYoutubeDraft(string databaseIndex) { var strippedName = UploaderUtils.GetYoutubeStrippedName(databaseIndex); var req = Service.Search.List("snippet"); req.Q = strippedName; req.ForMine = true; req.MaxResults = 3; //max results to 3, just in case there might be a couple of duplicates req.Type = "video"; var resp = await req.ExecuteAsync(); //duplicate videos do not have a snippet, so filter them out return(resp.Items.FirstOrDefault(item => item.Snippet != null && strippedName == item.Snippet.Title)?.Id); }
protected override async Task <bool> UploadItem(PendingUpload upload) { var matchData = await DB.GetData <MatchData>(upload.DataName); var videoResource = await FindYoutubeDraft(matchData); if (videoResource is null) { upload.LastException = "Could not find associated youtube video"; return(false); } var draftVideoData = await UploaderUtils.GetVideoData(Service, videoResource.VideoId); //ignore videos that haven't been fully uploaded/processed yet if (draftVideoData is null || draftVideoData.ProcessingDetails.ProcessingStatus != "succeeded") { upload.LastException = "Video is still processing or is a duplicate"; return(false); } //now override the match data to publish the item try { matchData.YoutubeUrl = videoResource.VideoId; var matchVideoData = await UploaderUtils.GetVideoDataForDatabaseItem(DB, matchData); await UploaderUtils.UpdateVideoData(Service, videoResource.VideoId, matchVideoData); await DB.SaveData(matchData); //also update the youtubeurl on all the linked rounds await DB.IterateOver <RoundData>(async ( roundData ) => { if (roundData.VideoType == VideoType.MergedVideoLink && string.IsNullOrEmpty(roundData.YoutubeUrl)) { roundData.YoutubeUrl = videoResource.VideoId; await DB.SaveData(roundData); } return(true); }, matchData.Rounds); //now delete the file in the temp folder and in the normal path if (File.Exists(DB.SharedSettings.GetMatchVideoPath(matchData.DatabaseIndex))) { File.Delete(DB.SharedSettings.GetMatchVideoPath(matchData.DatabaseIndex)); } //now check the temp folder too if (File.Exists(Path.Combine(Path.GetTempPath(), "MatchUploader", $"{matchData.DatabaseIndex}.mp4"))) { File.Delete(Path.Combine(Path.GetTempPath(), "MatchUploader", $"{matchData.DatabaseIndex}.mp4")); } return(true); } catch (Exception e) { upload.LastException = e.ToString(); } return(false); }