ShareX/ShareX.UploadersLib/FileUploaders/GfycatUploader.cs

333 lines
12 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2019-01-02 20:43:52 +13:00
Copyright (c) 2007-2019 ShareX Team
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)
// Credits: https://github.com/Dinnerbone
using Newtonsoft.Json;
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
2017-03-28 13:46:05 +13:00
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
2017-03-28 12:05:28 +13:00
using System.Collections.Specialized;
2017-03-28 13:46:05 +13:00
using System.Drawing;
using System.IO;
using System.Threading;
2017-03-28 12:05:28 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
{
public class GfycatFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Gfycat;
2017-03-28 13:46:05 +13:00
public override Image ServiceImage => Resources.Gfycat;
2017-03-28 12:05:28 +13:00
public override bool CheckConfig(UploadersConfig config)
{
return config.GfycatAccountType == AccountType.Anonymous || OAuth2Info.CheckOAuth(config.GfycatOAuth2Info);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
2017-03-28 12:05:28 +13:00
if (config.GfycatOAuth2Info == null)
{
config.GfycatOAuth2Info = new OAuth2Info(APIKeys.GfycatClientID, APIKeys.GfycatClientSecret);
}
2017-03-28 13:46:05 +13:00
2017-03-28 12:05:28 +13:00
return new GfycatUploader(config.GfycatOAuth2Info)
{
UploadMethod = config.GfycatAccountType,
2019-03-12 20:49:23 +13:00
Private = !config.GfycatIsPublic,
KeepAudio = config.GfycatKeepAudio
2017-03-28 12:05:28 +13:00
};
}
2017-03-28 12:05:28 +13:00
2017-03-28 13:46:05 +13:00
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGfycat;
}
2017-03-28 12:05:28 +13:00
public class GfycatUploader : FileUploader, IOAuth2
{
2017-03-28 12:05:28 +13:00
public OAuth2Info AuthInfo { get; set; }
public AccountType UploadMethod { get; set; }
public OAuth2Token AnonymousToken { get; set; }
public bool NoResize { get; set; } = true;
public bool IgnoreExisting { get; set; } = true;
public bool Private { get; set; } = true;
public bool KeepAudio { get; set; } = true;
2017-03-28 12:05:28 +13:00
private const string URL_AUTHORIZE = "https://gfycat.com/oauth/authorize";
private const string URL_UPLOAD = "https://filedrop.gfycat.com";
private const string URL_API = "https://api.gfycat.com/v1";
private const string URL_API_TOKEN = URL_API + "/oauth/token";
private const string URL_API_CREATE_GFY = URL_API + "/gfycats";
private const string URL_API_STATUS = URL_API + "/gfycats/fetch/status/";
public GfycatUploader(OAuth2Info oauth)
{
2017-03-28 12:05:28 +13:00
AuthInfo = oauth;
}
2017-03-28 12:05:28 +13:00
public string GetAuthorizationURL()
{
Dictionary<string, string> args = new Dictionary<string, string>();
2017-03-28 12:05:28 +13:00
args.Add("client_id", AuthInfo.Client_ID);
args.Add("scope", "all");
2017-03-28 13:46:05 +13:00
args.Add("state", "ShareX");
2017-03-28 12:05:28 +13:00
args.Add("response_type", "code");
args.Add("redirect_uri", Links.URL_CALLBACK);
return URLHelpers.CreateQueryString(URL_AUTHORIZE, args);
2017-03-28 12:05:28 +13:00
}
public bool GetAccessToken(string code)
{
string request = JsonConvert.SerializeObject(new
{
client_id = AuthInfo.Client_ID,
client_secret = AuthInfo.Client_Secret,
grant_type = "authorization_code",
redirect_uri = Links.URL_CALLBACK,
2019-03-12 20:49:23 +13:00
code = code
2017-03-28 12:05:28 +13:00
});
2018-10-18 05:06:06 +13:00
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
2017-03-28 12:05:28 +13:00
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
return false;
}
public bool RefreshAccessToken()
{
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
string request = JsonConvert.SerializeObject(new
{
refresh_token = AuthInfo.Token.refresh_token,
client_id = AuthInfo.Client_ID,
client_secret = AuthInfo.Client_Secret,
2019-03-12 20:49:23 +13:00
grant_type = "refresh"
2017-03-28 12:05:28 +13:00
});
2018-10-18 05:06:06 +13:00
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
2017-03-28 12:05:28 +13:00
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
}
return false;
}
public bool CheckAuthorization()
{
if (OAuth2Info.CheckOAuth(AuthInfo))
{
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
{
Errors.Add("Refresh access token failed.");
return false;
}
}
else
{
Errors.Add("Gfycat login is required.");
return false;
}
2017-03-28 12:05:28 +13:00
return true;
}
2017-03-28 12:05:28 +13:00
public override UploadResult Upload(Stream stream, string fileName)
{
OAuth2Token token = GetOrCreateToken();
if (token == null)
{
return null;
}
2017-03-28 13:46:05 +13:00
2017-03-28 12:05:28 +13:00
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + token.access_token);
2017-03-28 13:46:05 +13:00
2017-03-28 12:05:28 +13:00
GfycatCreateResponse gfy = CreateGfycat(headers);
if (gfy == null)
{
return null;
}
2017-03-28 13:46:05 +13:00
2017-03-28 12:05:28 +13:00
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("key", gfy.GfyName);
UploadResult result = SendRequestFile(URL_UPLOAD, stream, fileName, "file", args);
if (!result.IsError)
{
2017-03-28 12:05:28 +13:00
WaitForTranscode(gfy.GfyName, result);
}
return result;
}
2017-03-28 12:05:28 +13:00
private void WaitForTranscode(string name, UploadResult result)
{
2017-03-28 12:05:28 +13:00
ProgressManager progress = new ProgressManager(10000);
2017-03-28 12:05:28 +13:00
if (AllowReportProgress)
{
OnProgressChanged(progress);
}
2017-03-28 12:05:28 +13:00
int iterations = 0;
2017-03-28 12:05:28 +13:00
while (!StopUploadRequested)
{
2017-03-28 12:05:28 +13:00
string statusJson = SendRequest(HttpMethod.GET, URL_API_STATUS + name);
GfycatStatusResponse response = JsonConvert.DeserializeObject<GfycatStatusResponse>(statusJson);
2017-03-28 12:05:28 +13:00
if (response.Error != null)
{
2017-03-28 12:05:28 +13:00
Errors.Add(response.Error);
result.IsSuccess = false;
break;
}
else if (response.Task.Equals("error", StringComparison.InvariantCultureIgnoreCase))
{
Errors.Add(response.Description);
result.IsSuccess = false;
break;
}
2017-03-28 12:05:28 +13:00
else if (response.GfyName != null)
{
result.IsSuccess = true;
result.URL = "https://gfycat.com/" + response.GfyName;
break;
2017-03-28 13:46:05 +13:00
}
else if ((response.Task.Equals("NotFoundo", StringComparison.InvariantCultureIgnoreCase) ||
response.Task.Equals("NotFound", StringComparison.InvariantCultureIgnoreCase)) && iterations > 5)
2017-03-28 12:05:28 +13:00
{
Errors.Add("Gfy not found");
result.IsSuccess = false;
break;
}
2017-03-28 12:05:28 +13:00
if (AllowReportProgress && progress.UpdateProgress((progress.Length - progress.Position) / response.Time))
{
2017-03-28 12:05:28 +13:00
OnProgressChanged(progress);
}
2014-06-03 03:24:24 +12:00
Thread.Sleep(500);
2017-03-28 12:05:28 +13:00
iterations++;
}
}
2017-03-28 12:05:28 +13:00
private GfycatCreateResponse CreateGfycat(NameValueCollection headers)
{
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("private", Private);
args.Add("noResize", NoResize);
args.Add("noMd5", IgnoreExisting);
2019-03-07 18:05:42 +13:00
args.Add("keepAudio", KeepAudio);
2017-03-28 13:46:05 +13:00
string json = JsonConvert.SerializeObject(args);
2018-10-18 05:06:06 +13:00
string response = SendRequest(HttpMethod.POST, URL_API_CREATE_GFY, json, UploadHelpers.ContentTypeJSON, null, headers);
2017-03-28 12:05:28 +13:00
if (!string.IsNullOrEmpty(response))
{
return JsonConvert.DeserializeObject<GfycatCreateResponse>(response);
}
return null;
}
2017-03-28 12:05:28 +13:00
private OAuth2Token GetOrCreateToken()
{
if (UploadMethod == AccountType.User)
{
if (!CheckAuthorization())
{
return null;
}
2017-03-28 12:05:28 +13:00
return AuthInfo.Token;
}
else
{
2017-03-28 12:05:28 +13:00
if (AnonymousToken == null || AnonymousToken.IsExpired)
{
string request = JsonConvert.SerializeObject(new
{
client_id = AuthInfo.Client_ID,
client_secret = AuthInfo.Client_Secret,
grant_type = "client_credentials",
});
2018-10-18 05:06:06 +13:00
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
2017-03-28 12:05:28 +13:00
if (!string.IsNullOrEmpty(response))
{
AnonymousToken = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (AnonymousToken != null && !string.IsNullOrEmpty(AnonymousToken.access_token))
{
AnonymousToken.UpdateExpireDate();
}
}
}
2017-03-28 13:46:05 +13:00
2017-03-28 12:05:28 +13:00
return AnonymousToken;
}
}
}
2017-03-28 12:05:28 +13:00
public class GfycatCreateResponse
{
2017-03-28 12:05:28 +13:00
public string GfyName { get; set; }
public string Secret { get; set; }
}
public class GfycatStatusResponse
{
public string Task { get; set; }
public int Time { get; set; }
public string GfyName { get; set; }
public string Error { get; set; }
public string Description { get; set; }
}
}