mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-06 05:05:42 +13:00
Merge pull request #261 from BallisticLingonberries/master
Updated Pushbullet to use PB API v2, and added PB as a URL sharing service
This commit is contained in:
commit
2537048a3c
4 changed files with 137 additions and 61 deletions
|
@ -1129,6 +1129,9 @@ public void ShareURL(string url)
|
|||
case URLSharingServices.VK:
|
||||
URLHelpers.OpenURL("http://vk.com/share.php?url=" + encodedUrl);
|
||||
break;
|
||||
case URLSharingServices.Pushbullet:
|
||||
new Pushbullet(Program.UploadersConfig.PushbulletSettings).PushLink(url, "ShareX: URL Share");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -171,7 +171,9 @@ public enum URLSharingServices
|
|||
[Description("Delicious")]
|
||||
Delicious,
|
||||
[Description("VK")]
|
||||
VK
|
||||
VK,
|
||||
[Description("Pushbullet")]
|
||||
Pushbullet
|
||||
}
|
||||
|
||||
public enum HttpMethod
|
||||
|
|
|
@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
// Credits: https://github.com/BallisticLingonberries
|
||||
// API Information: https://docs.pushbullet.com/http/
|
||||
|
||||
using HelpersLib;
|
||||
using Newtonsoft.Json;
|
||||
|
@ -44,24 +45,61 @@ public Pushbullet(PushbulletSettings config)
|
|||
Config = config;
|
||||
}
|
||||
|
||||
private const string
|
||||
wwwPushesURL = "https://www.pushbullet.com/pushes",
|
||||
apiURL = "https://api.pushbullet.com/v2";
|
||||
|
||||
private readonly string
|
||||
apiGetDevicesURL = apiURL + "/devices",
|
||||
apiSendPushURL = apiURL + "/pushes",
|
||||
apiRequestFileUploadURL = apiURL + "/upload-request";
|
||||
|
||||
public UploadResult PushFile(Stream stream, string fileName)
|
||||
{
|
||||
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
|
||||
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("device_iden", Config.CurrentDevice.Key);
|
||||
args.Add("type", "file");
|
||||
Dictionary<string, string> pushArgs, upArgs = new Dictionary<string, string>();
|
||||
|
||||
UploadResult result = UploadData(stream, "https://api.pushbullet.com/api/pushes", fileName, "file", args, headers);
|
||||
upArgs.Add("file_name", fileName);
|
||||
|
||||
PushbulletResponsePush push = JsonConvert.DeserializeObject<PushbulletResponsePush>(result.Response);
|
||||
string uploadRequest = SendRequest(HttpMethod.POST, apiRequestFileUploadURL, upArgs, headers);
|
||||
|
||||
if (uploadRequest == null) return null;
|
||||
|
||||
PushbulletResponseFileUpload fileInfo = JsonConvert.DeserializeObject<PushbulletResponseFileUpload>(uploadRequest);
|
||||
|
||||
if (fileInfo == null) return null;
|
||||
|
||||
pushArgs = upArgs;
|
||||
|
||||
upArgs = new Dictionary<string, string>();
|
||||
|
||||
upArgs.Add("awsaccesskeyid", fileInfo.data.awsaccesskeyid);
|
||||
upArgs.Add("acl", fileInfo.data.acl);
|
||||
upArgs.Add("key", fileInfo.data.key);
|
||||
upArgs.Add("signature", fileInfo.data.signature);
|
||||
upArgs.Add("policy", fileInfo.data.policy);
|
||||
upArgs.Add("content-type", fileInfo.data.content_type);
|
||||
|
||||
UploadResult uploadResult = UploadData(stream, fileInfo.upload_url, fileName, "file", upArgs);
|
||||
|
||||
if (uploadResult == null) return null;
|
||||
|
||||
pushArgs.Add("device_iden", Config.CurrentDevice.Key);
|
||||
pushArgs.Add("type", "file");
|
||||
pushArgs.Add("file_url", fileInfo.file_url);
|
||||
pushArgs.Add("body", "Sent via ShareX");
|
||||
|
||||
string pushResult = SendRequest(HttpMethod.POST, apiSendPushURL, pushArgs, headers);
|
||||
|
||||
if (pushResult == null) return null;
|
||||
|
||||
PushbulletResponsePush push = JsonConvert.DeserializeObject<PushbulletResponsePush>(pushResult);
|
||||
|
||||
if (push != null)
|
||||
{
|
||||
result.URL = "https://www.pushbullet.com/pushes?push_iden=" + push.iden;
|
||||
}
|
||||
uploadResult.URL = wwwPushesURL + "?push_iden=" + push.iden;
|
||||
|
||||
return result;
|
||||
return uploadResult;
|
||||
}
|
||||
|
||||
private string Push(string pushType, string valueType, string value, string title)
|
||||
|
@ -74,14 +112,22 @@ private string Push(string pushType, string valueType, string value, string titl
|
|||
args.Add("title", title);
|
||||
args.Add(valueType, value);
|
||||
|
||||
string response = SendRequest(HttpMethod.POST, "https://api.pushbullet.com/api/pushes", args, headers);
|
||||
if (valueType != "body")
|
||||
{
|
||||
if (pushType == "link")
|
||||
args.Add("body", value);
|
||||
else
|
||||
args.Add("body", "Sent via ShareX");
|
||||
}
|
||||
|
||||
string response = SendRequest(HttpMethod.POST, apiSendPushURL, args, headers);
|
||||
|
||||
if (response == null) return null;
|
||||
|
||||
PushbulletResponsePush push = JsonConvert.DeserializeObject<PushbulletResponsePush>(response);
|
||||
|
||||
if (push != null)
|
||||
{
|
||||
return "https://www.pushbullet.com/pushes?push_iden=" + push.iden;
|
||||
}
|
||||
return wwwPushesURL + "?push_iden=" + push.iden;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -102,29 +148,89 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
if (Config.CurrentDevice == null) throw new Exception("No device set to push to.");
|
||||
if (string.IsNullOrEmpty(Config.CurrentDevice.Key)) throw new Exception("Missing device key.");
|
||||
|
||||
return PushFile(stream, fileName);
|
||||
return PushFile(stream, fileName);//, fileType);
|
||||
}
|
||||
|
||||
public List<PushbulletDevice> GetDeviceList()
|
||||
{
|
||||
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
|
||||
|
||||
string response = SendRequest(HttpMethod.GET, "https://api.pushbullet.com/api/devices", headers: headers);
|
||||
string response = SendRequest(HttpMethod.GET, apiGetDevicesURL, headers: headers);
|
||||
|
||||
PushbulletResponseDevices devicesResponse = JsonConvert.DeserializeObject<PushbulletResponseDevices>(response);
|
||||
|
||||
if (devicesResponse != null && devicesResponse.devices != null)
|
||||
{
|
||||
return devicesResponse.devices.Select(x => new PushbulletDevice { Key = x.iden, Name = x.extras.nickname }).ToList();
|
||||
}
|
||||
return devicesResponse.devices.Select(x => new PushbulletDevice { Key = x.iden, Name = x.nickname }).ToList();
|
||||
|
||||
return new List<PushbulletDevice>();
|
||||
}
|
||||
|
||||
private class PushbulletResponseDevices
|
||||
{
|
||||
public List<PushbulletResponseDevice> devices { get; set; }
|
||||
}
|
||||
|
||||
private class PushbulletResponseDevice
|
||||
{
|
||||
public string iden { get; set; }
|
||||
|
||||
public string nickname { get; set; }
|
||||
}
|
||||
|
||||
private class PushbulletResponsePush
|
||||
{
|
||||
public string iden { get; set; }
|
||||
|
||||
public string device_iden { get; set; }
|
||||
|
||||
public PushbulletResponsePushData data { get; set; }
|
||||
|
||||
public long created { get; set; }
|
||||
}
|
||||
|
||||
private class PushbulletResponsePushData
|
||||
{
|
||||
public string type { get; set; }
|
||||
|
||||
public string title { get; set; }
|
||||
|
||||
public string body { get; set; }
|
||||
}
|
||||
|
||||
private class PushbulletResponseFileUpload
|
||||
{
|
||||
public string file_type { get; set; }
|
||||
|
||||
public string file_name { get; set; }
|
||||
|
||||
public string file_url { get; set; }
|
||||
|
||||
public string upload_url { get; set; }
|
||||
|
||||
public PushbulletResponseFileUploadData data { get; set; }
|
||||
}
|
||||
|
||||
private class PushbulletResponseFileUploadData
|
||||
{
|
||||
public string awsaccesskeyid { get; set; }
|
||||
|
||||
public string acl { get; set; }
|
||||
|
||||
public string key { get; set; }
|
||||
|
||||
public string signature { get; set; }
|
||||
|
||||
public string policy { get; set; }
|
||||
|
||||
[JsonProperty("content-type")]
|
||||
public string content_type { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class PushbulletDevice
|
||||
{
|
||||
public string Key { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
|
@ -139,48 +245,10 @@ public PushbulletDevice CurrentDevice
|
|||
get
|
||||
{
|
||||
if (DeviceList.IsValidIndex(SelectedDevice))
|
||||
{
|
||||
return DeviceList[SelectedDevice];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PushbulletResponseDevices
|
||||
{
|
||||
public List<PushbulletResponseDevice> devices { get; set; }
|
||||
}
|
||||
|
||||
public class PushbulletResponseDevice
|
||||
{
|
||||
public string iden { get; set; }
|
||||
public PushbulletResponseDeviceExtras extras { get; set; }
|
||||
}
|
||||
|
||||
public class PushbulletResponseDeviceExtras
|
||||
{
|
||||
public string manufacturer { get; set; }
|
||||
public string model { get; set; }
|
||||
public string android_version { get; set; }
|
||||
public string sdk_version { get; set; }
|
||||
public string app_version { get; set; }
|
||||
public string nickname { get; set; }
|
||||
}
|
||||
|
||||
public class PushbulletResponsePush
|
||||
{
|
||||
public string iden { get; set; }
|
||||
public string device_iden { get; set; }
|
||||
public PushbulletResponsePushData data { get; set; }
|
||||
public long created { get; set; }
|
||||
}
|
||||
|
||||
public class PushbulletResponsePushData
|
||||
{
|
||||
public string type { get; set; }
|
||||
public string title { get; set; }
|
||||
public string body { get; set; }
|
||||
}
|
||||
}
|
|
@ -97,10 +97,6 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
|
|||
public string UpasteUserKey = string.Empty;
|
||||
public bool UpasteIsPublic = false;
|
||||
|
||||
// Pushbullet
|
||||
|
||||
public PushbulletSettings PushbulletSettings = new PushbulletSettings();
|
||||
|
||||
#endregion Text uploaders
|
||||
|
||||
#region File uploaders
|
||||
|
@ -226,6 +222,10 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
|
|||
public string MediaFirePath = "";
|
||||
public bool MediaFireUseLongLink = false;
|
||||
|
||||
// Pushbullet
|
||||
|
||||
public PushbulletSettings PushbulletSettings = new PushbulletSettings();
|
||||
|
||||
// MediaCrush
|
||||
|
||||
public bool MediaCrushDirectLink = false;
|
||||
|
@ -421,6 +421,9 @@ public bool IsValid(URLSharingServices destination)
|
|||
return !string.IsNullOrEmpty(EmailSmtpServer) && EmailSmtpPort > 0 && !string.IsNullOrEmpty(EmailFrom) && !string.IsNullOrEmpty(EmailPassword);
|
||||
case URLSharingServices.Twitter:
|
||||
return TwitterOAuthInfoList != null && TwitterOAuthInfoList.IsValidIndex(TwitterSelectedAccount) && OAuthInfo.CheckOAuth(TwitterOAuthInfoList[TwitterSelectedAccount]);
|
||||
case URLSharingServices.Pushbullet:
|
||||
return PushbulletSettings != null && !string.IsNullOrEmpty(PushbulletSettings.UserAPIKey) && PushbulletSettings.DeviceList != null &&
|
||||
PushbulletSettings.DeviceList.IsValidIndex(PushbulletSettings.SelectedDevice);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
Loading…
Reference in a new issue