forked from MathewSachin/Captura
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7297fd
commit c2eed2f
Showing
20 changed files
with
609 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net461</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="System.Drawing" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.36.1.1226" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Captura.Base\Captura.Base.csproj" /> | ||
<ProjectReference Include="..\Screna\Screna.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Captura | ||
{ | ||
public interface IYouTubeApiKeys | ||
{ | ||
string YouTubeClientId { get; } | ||
|
||
string YouTubeClientSecret { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Captura | ||
{ | ||
public enum YouTubePrivacyStatus | ||
{ | ||
Public, | ||
Unlisted, | ||
Private | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Google.Apis.Upload; | ||
using Google.Apis.YouTube.v3; | ||
using Google.Apis.YouTube.v3.Data; | ||
|
||
namespace Captura | ||
{ | ||
public class YouTubeUploadRequest : IDisposable | ||
{ | ||
readonly VideosResource.InsertMediaUpload _videoInsertRequest; | ||
readonly Stream _dataStream; | ||
|
||
internal YouTubeUploadRequest(string FileName, | ||
YouTubeService YouTubeService, | ||
Video Video) | ||
{ | ||
_dataStream = new FileStream(FileName, FileMode.Open); | ||
_videoInsertRequest = YouTubeService.Videos.Insert(Video, "snippet,status", _dataStream, "video/*"); | ||
|
||
_videoInsertRequest.ProgressChanged += VideosInsertRequest_ProgressChanged; | ||
_videoInsertRequest.ResponseReceived += VideosInsertRequest_ResponseReceived; | ||
} | ||
|
||
void VideosInsertRequest_ProgressChanged(IUploadProgress Progress) | ||
{ | ||
BytesSent?.Invoke(Progress.BytesSent); | ||
} | ||
|
||
void VideosInsertRequest_ResponseReceived(Video Video) | ||
{ | ||
Uploaded?.Invoke($"https://youtube.com/watch?v={Video.Id}"); | ||
} | ||
|
||
public async Task<IUploadProgress> Upload(CancellationToken CancellationToken) | ||
{ | ||
return await _videoInsertRequest.UploadAsync(CancellationToken); | ||
} | ||
|
||
public async Task<IUploadProgress> Resume(CancellationToken CancellationToken) | ||
{ | ||
return await _videoInsertRequest.ResumeAsync(CancellationToken); | ||
} | ||
|
||
public event Action<long> BytesSent; | ||
|
||
public event Action<string> Uploaded; | ||
|
||
public void Dispose() | ||
{ | ||
_dataStream.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Google.Apis.Auth.OAuth2; | ||
using Google.Apis.Services; | ||
using Google.Apis.YouTube.v3; | ||
using Google.Apis.YouTube.v3.Data; | ||
|
||
namespace Captura | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public class YouTubeUploader | ||
{ | ||
readonly IYouTubeApiKeys _apiKeys; | ||
readonly ProxySettings _proxySettings; | ||
|
||
YouTubeService _youtubeService; | ||
|
||
public YouTubeUploader(IYouTubeApiKeys ApiKeys, | ||
ProxySettings ProxySettings) | ||
{ | ||
_apiKeys = ApiKeys; | ||
_proxySettings = ProxySettings; | ||
} | ||
|
||
static string GetPrivacyStatus(YouTubePrivacyStatus PrivacyStatus) | ||
{ | ||
return PrivacyStatus.ToString().ToLower(); | ||
} | ||
|
||
public async Task<YouTubeUploadRequest> CreateUploadRequest(string FileName, | ||
string Title, | ||
string Description, | ||
string[] Tags = null, | ||
YouTubePrivacyStatus PrivacyStatus = YouTubePrivacyStatus.Unlisted) | ||
{ | ||
if (_youtubeService == null) | ||
await Init(); | ||
|
||
var video = new Video | ||
{ | ||
Snippet = new VideoSnippet | ||
{ | ||
Title = Title, | ||
Description = Description, | ||
Tags = Tags ?? new string[0], | ||
CategoryId = "22" | ||
}, | ||
Status = new VideoStatus { PrivacyStatus = GetPrivacyStatus(PrivacyStatus) } | ||
}; | ||
|
||
return new YouTubeUploadRequest(FileName, _youtubeService, video); | ||
} | ||
|
||
async Task Init() | ||
{ | ||
WebRequest.DefaultWebProxy = _proxySettings.GetWebProxy(); | ||
|
||
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync | ||
( | ||
new ClientSecrets | ||
{ | ||
ClientId = _apiKeys.YouTubeClientId, | ||
ClientSecret = _apiKeys.YouTubeClientSecret | ||
}, | ||
// This OAuth 2.0 access scope allows an application to upload files to the | ||
// authenticated user's YouTube channel, but doesn't allow other types of access. | ||
new[] { YouTubeService.Scope.YoutubeUpload }, | ||
"user", | ||
CancellationToken.None | ||
); | ||
|
||
_youtubeService = new YouTubeService(new BaseClientService.Initializer | ||
{ | ||
HttpClientInitializer = credential, | ||
ApplicationName = nameof(Captura) | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.