/// <summary> /// Begins publishing a video. /// </summary> /// <param name="filePath">Physical path of the video file.</param> /// <param name="parameters">RPC parameters.</param> /// <returns></returns> public override async Task <VideoPublishResult> PublishAsync(string filePath, IDictionary <string, string> parameters) { dynamic response = null; VideoPublishResult ret = null; dynamic publishParameters = new ExpandoObject(); var client = new FacebookClient(parameters["AccessToken"]); Log.Info(string.Format("Sending {0} to Facebook...", filePath)); publishParameters.title = parameters["Title"]; publishParameters.access_token = parameters["AccessToken"]; publishParameters.source = new FacebookMediaStream { ContentType = "video/mp4", FileName = "video.mp4" }.SetValue(File.OpenRead(filePath)); response = await client.PostTaskAsync("/me/videos", publishParameters); if (response != null) { ret = new VideoPublishResult() { Url = string.Format("https://www.facebook.com/video.php?v={0}", response.id) }; } return(ret); }
/// <summary> /// Runs publisher. /// </summary> /// <param name="destination">Destination (e.g. "YouTube").</param> /// <param name="physicalPath">Physical path of the video.</param> /// <param name="parameters">Publisher parameters.</param> /// <returns>A task.</returns> private static async Task <VideoPublish> RunPublisherAsync(string destination, string physicalPath, NameValueCollection parameters) { VideoPublish ret = null; VideoPublishResult result = null; IDictionary <string, string> rpcParameters = new Dictionary <string, string>(); if (parameters != null) { foreach (string key in parameters.AllKeys) { if (!rpcParameters.ContainsKey(key)) { rpcParameters.Add(key, parameters[key]); } } } if (string.Compare(destination, "youtube", true) == 0) { ret = new YouTubeVideoPublish() { Log = _log } } ; else if (string.Compare(destination, "facebook", true) == 0) { ret = new FacebookVideoPublish() { Log = _log } } ; if (ret == null) { await Task.Delay(1); } else { result = await ret.PublishAsync(physicalPath, rpcParameters); // Deleting the file - we don't need it anymore (not served to the user). if (System.IO.File.Exists(physicalPath)) { try { System.IO.File.Delete(physicalPath); } catch (System.IO.IOException) { } } // Letting the client know that we're done - placing a text file with the result. // In other words, we're saying "I'm not here, but here's an information on where to find me". System.IO.File.WriteAllText(physicalPath.Replace("_pub.mp4", string.Empty) + ".txt", result != null ? Newtonsoft.Json.JsonConvert.SerializeObject(result) : string.Empty); } return(ret); } }
/// <summary> /// Begins publishing a video. /// </summary> /// <param name="filePath">Physical path of the video file.</param> /// <param name="parameters">RPC parameters.</param> /// <returns></returns> public override async Task <VideoPublishResult> PublishAsync(string filePath, IDictionary <string, string> parameters) { VideoPublishResult ret = null; Log.Info(string.Format("Sending {0} to YouTube...", filePath)); if (System.IO.File.Exists(filePath) && parameters != null) { var credential = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() { ClientSecrets = new ClientSecrets() { ClientId = "732432655752-sc8rnb18i0os9s6gg9ru3fnvbdghc6mf.apps.googleusercontent.com", ClientSecret = "rK7K4jw-5InCOPmPzoE8iLIM" }, Scopes = new string[] { "https://www.googleapis.com/auth/youtube.upload" } }), parameters["UserId"], new Google.Apis.Auth.OAuth2.Responses.TokenResponse() { AccessToken = parameters["AccessToken"], Issued = DateTime.UtcNow, ExpiresInSeconds = 300, Scope = "https://www.googleapis.com/auth/youtube.upload" }); var youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = Assembly.GetExecutingAssembly().GetName().Name }); var video = new Video(); video.Snippet = new VideoSnippet(); video.Snippet.Title = parameters["Title"]; video.Status = new VideoStatus(); video.Status.PrivacyStatus = "unlisted"; using (var fileStream = new FileStream(filePath, FileMode.Open)) { var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*"); videosInsertRequest.ProgressChanged += p => { switch (p.Status) { case UploadStatus.Failed: Log.Error(string.Format("Error uploading {0} to YouTube.", filePath), p.Exception); break; case UploadStatus.Completed: Log.Info(string.Format("Successfully uploaded {0} bytes to YouTube for {1}.", p.BytesSent, filePath)); break; } }; videosInsertRequest.ResponseReceived += v => { if (!string.IsNullOrEmpty(v.Id)) { Log.Info(string.Format("Upload to YouTube of {0} is finished.", filePath)); ret = new VideoPublishResult() { Url = "https://www.youtube.com/my_videos" }; } }; await videosInsertRequest.UploadAsync(); } } return(ret); }