ShareX/ShareX.UploadersLib/FileUploaders/YouTube.cs

173 lines
5.5 KiB
C#
Raw Normal View History

2018-04-10 12:49:40 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2021-07-29 15:22:51 +12:00
Copyright (c) 2007-2021 ShareX Team
2018-04-10 12:49:40 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.UploadersLib.Properties;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace ShareX.UploadersLib.FileUploaders
{
public class YouTubeFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.YouTube;
public override Icon ServiceIcon => Resources.YouTube;
public override bool CheckConfig(UploadersConfig config)
{
return OAuth2Info.CheckOAuth(config.YouTubeOAuth2Info);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new YouTube(config.YouTubeOAuth2Info)
{
2018-04-12 22:09:35 +12:00
PrivacyType = config.YouTubePrivacyType,
UseShortenedLink = config.YouTubeUseShortenedLink
2018-04-10 12:49:40 +12:00
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpYouTube;
}
public sealed class YouTube : FileUploader, IOAuth2
{
2018-04-12 22:09:35 +12:00
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
2018-04-10 12:49:40 +12:00
public YouTubeVideoPrivacy PrivacyType { get; set; }
2018-04-12 22:09:35 +12:00
public bool UseShortenedLink { get; set; }
private GoogleOAuth2 googleAuth;
2018-04-10 12:49:40 +12:00
public YouTube(OAuth2Info oauth)
{
2018-04-12 22:09:35 +12:00
googleAuth = new GoogleOAuth2(oauth, this)
2018-04-10 12:49:40 +12:00
{
Scope = "https://www.googleapis.com/auth/youtube.upload"
2018-04-10 12:49:40 +12:00
};
}
public bool RefreshAccessToken()
{
2018-04-12 22:09:35 +12:00
return googleAuth.RefreshAccessToken();
2018-04-10 12:49:40 +12:00
}
public bool CheckAuthorization()
{
2018-04-12 22:09:35 +12:00
return googleAuth.CheckAuthorization();
2018-04-10 12:49:40 +12:00
}
public string GetAuthorizationURL()
{
2018-04-12 22:09:35 +12:00
return googleAuth.GetAuthorizationURL();
2018-04-10 12:49:40 +12:00
}
public bool GetAccessToken(string code)
{
2018-04-12 22:09:35 +12:00
return googleAuth.GetAccessToken(code);
2018-04-10 12:49:40 +12:00
}
private string GetMetadata(string title)
{
2018-04-12 22:09:35 +12:00
object metadata = new
2018-04-10 12:49:40 +12:00
{
snippet = new
{
title = title
},
status = new
{
2018-04-12 22:09:35 +12:00
privacyStatus = PrivacyType.ToString()
2018-04-10 12:49:40 +12:00
}
};
return JsonConvert.SerializeObject(metadata);
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (!CheckAuthorization()) return null;
string title = Path.GetFileNameWithoutExtension(fileName);
string metadata = GetMetadata(title);
2018-04-10 12:49:40 +12:00
2018-11-29 04:15:58 +13:00
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName, "file",
2019-01-25 14:37:27 +13:00
headers: googleAuth.GetAuthHeaders(), relatedData: metadata);
2018-04-10 12:49:40 +12:00
if (!string.IsNullOrEmpty(result.Response))
{
2018-04-12 22:09:35 +12:00
YouTubeVideo video = JsonConvert.DeserializeObject<YouTubeVideo>(result.Response);
2018-04-10 12:49:40 +12:00
2018-04-12 22:09:35 +12:00
if (video != null)
2018-04-10 12:49:40 +12:00
{
2018-04-12 22:09:35 +12:00
if (UseShortenedLink)
{
result.URL = $"https://youtu.be/{video.id}";
}
else
{
result.URL = $"https://www.youtube.com/watch?v={video.id}";
}
2018-04-10 12:49:40 +12:00
2018-04-12 22:09:35 +12:00
switch (video.status.uploadStatus)
2018-04-10 12:49:40 +12:00
{
case YouTubeVideoStatus.UploadFailed:
2018-04-12 22:09:35 +12:00
Errors.Add("Upload failed: " + video.status.failureReason);
2018-04-10 12:49:40 +12:00
break;
case YouTubeVideoStatus.UploadRejected:
2018-04-12 22:09:35 +12:00
Errors.Add("Upload rejected: " + video.status.rejectionReason);
2018-04-10 12:49:40 +12:00
break;
}
}
}
return result;
}
}
public class YouTubeVideo
{
public string id { get; set; }
public YouTubeVideoSnippet snippet { get; set; }
public YouTubeVideoStatus status { get; set; }
}
public class YouTubeVideoSnippet
{
public string title { get; set; }
}
public class YouTubeVideoStatus
{
public const string UploadFailed = "failed";
public const string UploadRejected = "rejected";
public YouTubeVideoPrivacy privacyStatus { get; set; }
public string uploadStatus { get; set; }
public string failureReason { get; set; }
public string rejectionReason { get; set; }
}
}