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
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 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,
|
2021-11-20 14:35:47 +13:00
|
|
|
|
UseShortenedLink = config.YouTubeUseShortenedLink,
|
|
|
|
|
ShowDialog = config.YouTubeShowDialog
|
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; }
|
2021-11-20 14:35:47 +13:00
|
|
|
|
public bool ShowDialog { get; set; }
|
2018-04-12 22:09:35 +12:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-04-11 16:28:01 +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
|
|
|
|
}
|
|
|
|
|
|
2021-11-20 14:35:47 +13:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
if (!CheckAuthorization()) return null;
|
|
|
|
|
|
|
|
|
|
string title = Path.GetFileNameWithoutExtension(fileName);
|
|
|
|
|
string description = "";
|
|
|
|
|
YouTubeVideoPrivacy visibility = PrivacyType;
|
|
|
|
|
|
|
|
|
|
if (ShowDialog)
|
|
|
|
|
{
|
|
|
|
|
using (YouTubeVideoOptionsForm form = new YouTubeVideoOptionsForm(title, description, visibility))
|
|
|
|
|
{
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
title = form.Title;
|
|
|
|
|
description = form.Description;
|
|
|
|
|
visibility = form.Visibility;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
YouTubeVideoUpload uploadVideo = new YouTubeVideoUpload()
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
snippet = new YouTubeVideoSnippet()
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
title = title,
|
|
|
|
|
description = description
|
2018-04-10 12:49:40 +12:00
|
|
|
|
},
|
2021-11-20 14:35:47 +13:00
|
|
|
|
status = new YouTubeVideoStatusUpload()
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
privacyStatus = visibility
|
2018-04-10 12:49:40 +12:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-20 14:35:47 +13:00
|
|
|
|
string metadata = JsonConvert.SerializeObject(uploadVideo);
|
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))
|
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
YouTubeVideoResponse responseVideo = JsonConvert.DeserializeObject<YouTubeVideoResponse>(result.Response);
|
2018-04-10 12:49:40 +12:00
|
|
|
|
|
2021-11-20 14:35:47 +13:00
|
|
|
|
if (responseVideo != null)
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
2018-04-12 22:09:35 +12:00
|
|
|
|
if (UseShortenedLink)
|
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
result.URL = $"https://youtu.be/{responseVideo.id}";
|
2018-04-12 22:09:35 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-20 14:35:47 +13:00
|
|
|
|
result.URL = $"https://www.youtube.com/watch?v={responseVideo.id}";
|
2018-04-12 22:09:35 +12:00
|
|
|
|
}
|
2018-04-10 12:49:40 +12:00
|
|
|
|
|
2021-11-20 14:35:47 +13:00
|
|
|
|
switch (responseVideo.status.uploadStatus)
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
|
|
|
|
case YouTubeVideoStatus.UploadFailed:
|
2021-11-20 14:35:47 +13:00
|
|
|
|
Errors.Add("Upload failed: " + responseVideo.status.failureReason);
|
2018-04-10 12:49:40 +12:00
|
|
|
|
break;
|
|
|
|
|
case YouTubeVideoStatus.UploadRejected:
|
2021-11-20 14:35:47 +13:00
|
|
|
|
Errors.Add("Upload rejected: " + responseVideo.status.rejectionReason);
|
2018-04-10 12:49:40 +12:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-20 14:35:47 +13:00
|
|
|
|
public class YouTubeVideoUpload
|
|
|
|
|
{
|
|
|
|
|
public YouTubeVideoSnippet snippet { get; set; }
|
|
|
|
|
public YouTubeVideoStatusUpload status { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class YouTubeVideoResponse
|
2018-04-10 12:49:40 +12:00
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public YouTubeVideoSnippet snippet { get; set; }
|
|
|
|
|
public YouTubeVideoStatus status { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class YouTubeVideoSnippet
|
|
|
|
|
{
|
|
|
|
|
public string title { get; set; }
|
2021-11-20 14:35:47 +13:00
|
|
|
|
public string description { get; set; }
|
|
|
|
|
public string[] tags { get; set; }
|
2018-04-10 12:49:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
2021-11-20 14:35:47 +13:00
|
|
|
|
|
|
|
|
|
public class YouTubeVideoStatusUpload
|
|
|
|
|
{
|
|
|
|
|
public YouTubeVideoPrivacy privacyStatus { get; set; }
|
|
|
|
|
}
|
2018-04-10 12:49:40 +12:00
|
|
|
|
}
|