Made YouTube shortened link optional

This commit is contained in:
Jaex 2018-04-12 13:09:35 +03:00
parent 1bb564e66b
commit 0d8bd4e908
6 changed files with 8554 additions and 1955 deletions

View file

@ -345,4 +345,11 @@ public enum URLType
ThumbnailURL,
DeletionURL
}
public enum YouTubeVideoPrivacy // Localized
{
Public,
Unlisted,
Private
}
}

View file

@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Drawing;
@ -48,7 +47,8 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
{
return new YouTube(config.YouTubeOAuth2Info)
{
PrivacyType = config.YouTubePrivacyType
PrivacyType = config.YouTubePrivacyType,
UseShortenedLink = config.YouTubeUseShortenedLink
};
}
@ -57,44 +57,43 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public sealed class YouTube : FileUploader, IOAuth2
{
private GoogleOAuth2 GoogleAuth { get; set; }
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
public YouTubeVideoPrivacy PrivacyType { get; set; }
public bool UseShortenedLink { get; set; }
private GoogleOAuth2 googleAuth;
public YouTube(OAuth2Info oauth)
{
GoogleAuth = new GoogleOAuth2(oauth, this)
googleAuth = new GoogleOAuth2(oauth, this)
{
Scope = "https://www.googleapis.com/auth/youtube.upload"
};
}
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
public bool RefreshAccessToken()
{
return GoogleAuth.RefreshAccessToken();
return googleAuth.RefreshAccessToken();
}
public bool CheckAuthorization()
{
return GoogleAuth.CheckAuthorization();
return googleAuth.CheckAuthorization();
}
public string GetAuthorizationURL()
{
return GoogleAuth.GetAuthorizationURL();
return googleAuth.GetAuthorizationURL();
}
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
return googleAuth.GetAccessToken(code);
}
private string GetMetadata(string title)
{
object metadata;
metadata = new
object metadata = new
{
snippet = new
{
@ -102,7 +101,7 @@ private string GetMetadata(string title)
},
status = new
{
privacyStatus = PrivacyType
privacyStatus = PrivacyType.ToString()
}
};
@ -115,32 +114,37 @@ public override UploadResult Upload(Stream stream, string fileName)
if (!Helpers.IsVideoFile(fileName))
{
Errors.Add("YouTube only supports video files");
Errors.Add("YouTube only supports video files.");
return null;
}
string metadata = GetMetadata(fileName);
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName,
headers: GoogleAuth.GetAuthHeaders(), metadata: metadata);
headers: googleAuth.GetAuthHeaders(), metadata: metadata);
if (!string.IsNullOrEmpty(result.Response))
{
YouTubeVideo upload = JsonConvert.DeserializeObject<YouTubeVideo>(result.Response);
YouTubeVideo video = JsonConvert.DeserializeObject<YouTubeVideo>(result.Response);
if (upload != null)
if (video != null)
{
AllowReportProgress = false;
if (UseShortenedLink)
{
result.URL = $"https://youtu.be/{video.id}";
}
else
{
result.URL = $"https://www.youtube.com/watch?v={video.id}";
}
result.URL = "https://youtu.be/" + upload.id;
switch (upload.status.uploadStatus)
switch (video.status.uploadStatus)
{
case YouTubeVideoStatus.UploadFailed:
Errors.Add("Upload failed: " + upload.status.failureReason);
Errors.Add("Upload failed: " + video.status.failureReason);
break;
case YouTubeVideoStatus.UploadRejected:
Errors.Add("Upload rejected: " + upload.status.rejectionReason);
Errors.Add("Upload rejected: " + video.status.rejectionReason);
break;
}
}
@ -172,12 +176,4 @@ public class YouTubeVideoStatus
public string failureReason { get; set; }
public string rejectionReason { get; set; }
}
[JsonConverter(typeof(StringEnumConverter))]
public enum YouTubeVideoPrivacy // Localized
{
Public,
Unlisted,
Private
}
}

View file

@ -639,6 +639,7 @@ private void InitializeComponent()
this.lblWidthHint = new System.Windows.Forms.Label();
this.ttlvMain = new ShareX.HelpersLib.TabToListView();
this.actRapidShareAccountType = new ShareX.UploadersLib.AccountTypeControl();
this.cbYouTubeUseShortenedLink = new System.Windows.Forms.CheckBox();
this.tpOtherUploaders.SuspendLayout();
this.tcOtherUploaders.SuspendLayout();
this.tpTwitter.SuspendLayout();
@ -4072,6 +4073,7 @@ private void InitializeComponent()
//
// tpYouTube
//
this.tpYouTube.Controls.Add(this.cbYouTubeUseShortenedLink);
this.tpYouTube.Controls.Add(this.oauth2YouTube);
this.tpYouTube.Controls.Add(this.cbYouTubePrivacyType);
this.tpYouTube.Controls.Add(this.lblYouTubePrivacyType);
@ -5212,6 +5214,13 @@ private void InitializeComponent()
this.actRapidShareAccountType.Name = "actRapidShareAccountType";
this.actRapidShareAccountType.SelectedAccountType = ShareX.UploadersLib.AccountType.Anonymous;
//
// cbYouTubeUseShortenedLink
//
resources.ApplyResources(this.cbYouTubeUseShortenedLink, "cbYouTubeUseShortenedLink");
this.cbYouTubeUseShortenedLink.Name = "cbYouTubeUseShortenedLink";
this.cbYouTubeUseShortenedLink.UseVisualStyleBackColor = true;
this.cbYouTubeUseShortenedLink.CheckedChanged += new System.EventHandler(this.cbYouTubeUseShortenedLink_CheckedChanged);
//
// UploadersConfigForm
//
resources.ApplyResources(this, "$this");
@ -6000,5 +6009,6 @@ private void InitializeComponent()
private System.Windows.Forms.ComboBox cbYouTubePrivacyType;
private System.Windows.Forms.Label lblYouTubePrivacyType;
internal System.Windows.Forms.TabPage tpYouTube;
private System.Windows.Forms.CheckBox cbYouTubeUseShortenedLink;
}
}

View file

@ -728,6 +728,7 @@ public void LoadSettings()
cbYouTubePrivacyType.Items.Clear();
cbYouTubePrivacyType.Items.AddRange(Helpers.GetLocalizedEnumDescriptions<YouTubeVideoPrivacy>());
cbYouTubePrivacyType.SelectedIndex = (int)Config.YouTubePrivacyType;
cbYouTubeUseShortenedLink.Checked = Config.YouTubeUseShortenedLink;
#endregion YouTube
@ -3035,6 +3036,11 @@ private void cbYouTubePrivacyType_SelectedIndexChanged(object sender, EventArgs
Config.YouTubePrivacyType = (YouTubeVideoPrivacy)cbYouTubePrivacyType.SelectedIndex;
}
private void cbYouTubeUseShortenedLink_CheckedChanged(object sender, EventArgs e)
{
Config.YouTubeUseShortenedLink = cbYouTubeUseShortenedLink.Checked;
}
#endregion YouTube
#endregion File uploaders

File diff suppressed because it is too large Load diff

View file

@ -385,6 +385,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
public OAuth2Info YouTubeOAuth2Info = null;
public YouTubeVideoPrivacy YouTubePrivacyType = YouTubeVideoPrivacy.Public;
public bool YouTubeUseShortenedLink = false;
#endregion YouTube