Pushbullet is now file uploader therefore can be used with image & text upload

This commit is contained in:
Jaex 2014-03-30 12:16:20 +03:00
parent d276c03771
commit da2843355a
10 changed files with 161 additions and 196 deletions

View file

@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC")]
[assembly: AssemblyVersion("8.7.0")]
[assembly: AssemblyFileVersion("8.7.0")]
[assembly: AssemblyVersion("8.6.1")]
[assembly: AssemblyFileVersion("8.6.1")]

View file

@ -675,9 +675,6 @@ public UploadResult UploadText(Stream stream, string fileName)
IsPublic = Program.UploadersConfig.UpasteIsPublic
};
break;
case TextDestination.Pushbullet:
textUploader = new Pushbullet(Program.UploadersConfig.PushbulletSettings);
break;
case TextDestination.CustomTextUploader:
if (Program.UploadersConfig.CustomUploadersList.IsValidIndex(Program.UploadersConfig.CustomTextUploaderSelected))
{
@ -844,6 +841,9 @@ public UploadResult UploadFile(Stream stream, string fileName)
case FileDestination.AmazonS3:
fileUploader = new AmazonS3(Program.UploadersConfig.AmazonS3Settings);
break;
case FileDestination.Pushbullet:
fileUploader = new Pushbullet(Program.UploadersConfig.PushbulletSettings);
break;
}
if (fileUploader != null)

View file

@ -50,8 +50,6 @@ public enum ImageDestination
yFrog,
[Description("mediacru.sh")]
MediaCrush,
//[Description("pushbullet.com")]
//Pushbullet,
[Description("Custom image uploader")]
CustomImageUploader,
[Description("File uploader")]
@ -77,8 +75,6 @@ public enum TextDestination
Gist,
[Description("upaste.me")]
Upaste,
[Description("pushbullet.com")]
Pushbullet,
[Description("Custom text uploader")]
CustomTextUploader,
[Description("File uploader")]
@ -96,6 +92,8 @@ public enum FileDestination
Mega,
[Description("s3.amazon.com")]
AmazonS3,
[Description("pushbullet.com")]
Pushbullet,
[Description("drive.google.com")]
GoogleDrive,
[Description("rapidshare.com")]
@ -116,8 +114,6 @@ public enum FileDestination
Email,
[Description("Jira")]
Jira,
//[Description("pushbullet.com")]
//Pushbullet,
[Description("Custom file uploader")]
CustomFileUploader
}
@ -137,8 +133,6 @@ public enum UrlShortenerType
TURL,
[Description("yourls.org")]
YOURLS,
//[Description("pushbullet.com")]
//Pushbullet,
[Description("Custom URL shortener")]
CustomURLShortener
}

View file

@ -23,6 +23,8 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
// Credits: https://github.com/alanedwardes
using HelpersLib;
using Newtonsoft.Json;
using System;

View file

@ -0,0 +1,147 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2014 ShareX Developers
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/BallisticLingonberries
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Text;
namespace UploadersLib.FileUploaders
{
public sealed class Pushbullet : FileUploader
{
public PushbulletSettings Config { get; private set; }
public Pushbullet(PushbulletSettings config)
{
Config = config;
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (string.IsNullOrEmpty(Config.UserAPIKey)) throw new Exception("Missing API Key.");
if (Config.CurrentDevice == null) throw new Exception("No device set to push to.");
if (string.IsNullOrEmpty(Config.CurrentDevice.Key)) throw new Exception("Device key is empty.");
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("device_iden", Config.CurrentDevice.Key);
args.Add("type", "file");
UploadResult result = UploadData(stream, "https://api.pushbullet.com/api/pushes", fileName, arguments: args, headers: headers);
PushbulletResponsePush response = JsonConvert.DeserializeObject<PushbulletResponsePush>(result.Response);
if (response != null)
{
if (Config.ReturnPushURL)
{
result.URL = "https://www.pushbullet.com/pushes?push_iden=" + response.iden;
}
else
{
result.IsURLExpected = false;
}
}
return result;
}
public List<PushbulletDevice> GetDeviceList()
{
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
string response = SendGetRequest("https://api.pushbullet.com/api/devices", 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 new List<PushbulletDevice>();
}
}
public class PushbulletDevice
{
public string Key { get; set; }
public string Name { get; set; }
}
public class PushbulletSettings
{
public string UserAPIKey = string.Empty;
public bool ReturnPushURL = true;
public List<PushbulletDevice> DeviceList = new List<PushbulletDevice>();
public PushbulletDevice CurrentDevice = null; // TODO: Use int SelectedDevice
}
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; }
}
}

View file

@ -1090,11 +1090,8 @@ public void PushbulletGetDevices()
cboPushbulletDevices.Enabled = true;
lblPushbulletDevices.Enabled = true;
string strDevices = "Devices:";
Config.PushbulletSettings.DeviceList.ForEach(pbDevice =>
{
strDevices += "\n\t" + pbDevice.ToString();
cboPushbulletDevices.Items.Add(pbDevice.Name);
});

View file

@ -23,6 +23,8 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
// Credits: https://github.com/SirCmpwn
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

View file

@ -1,177 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2014 ShareX Developers
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.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Text;
namespace UploadersLib.TextUploaders
{
public sealed class Pushbullet : TextUploader
{
public PushbulletSettings Config { get; private set; }
public Pushbullet(PushbulletSettings config)
{
Config = config;
}
public override UploadResult UploadText(string text, string fileName)
{
if (string.IsNullOrEmpty(Config.UserAPIKey)) throw new Exception("Missing API Key.");
if (string.IsNullOrEmpty(text)) throw new Exception("Nothing to push.");
if (Config.CurrentDevice == null) throw new Exception("No device set to push to.");
string strDeviceKey = Config.CurrentDevice.Key;
if (string.IsNullOrEmpty(strDeviceKey)) throw new Exception("For some reason, the device key was empty.");
UploadResult result = new UploadResult();
try
{
WebRequest request = WebRequest.Create("https://api.pushbullet.com/api/pushes");
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
request.Credentials = new NetworkCredential(Config.UserAPIKey, "");
request.Method = "POST";
string pushData = "";
pushData += "device_iden=" + strDeviceKey + "&";
pushData += "type=note&"; // For now set it to 'note' until it's dynamic w/ ShareX...
pushData += "title=ShareX&";
pushData += "body=" + text;
byte[] postBytes = Encoding.UTF8.GetBytes(pushData);
request.ContentLength = postBytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(postBytes, 0, postBytes.Length);
}
JObject jObject = null;
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string strResponse = reader.ReadLine();
result.Response = strResponse;
if (!string.IsNullOrEmpty(strResponse) && strResponse.StartsWith("error"))
{
throw new WebException(strResponse);
}
jObject = JObject.Parse(strResponse);
}
if (Config.ReturnPushURL)
{
result.URL = "https://www.pushbullet.com/pushes?push_iden=" + jObject["iden"];
}
else
{
result.IsURLExpected = false;
}
result.IsSuccess = true;
return result;
}
catch (Exception ex)
{
Errors.Add(ex.ToString());
result.IsSuccess = false;
}
return null;
}
public List<PushbulletDevice> GetDeviceList()
{
try
{
WebRequest request = WebRequest.Create("https://api.pushbullet.com/api/devices");
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
request.Credentials = new NetworkCredential(Config.UserAPIKey, "");
request.Method = "GET";
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string strResponse = reader.ReadLine();
if (!string.IsNullOrEmpty(strResponse) && strResponse.StartsWith("error"))
{
Errors.Add(strResponse);
return null;
}
JObject jObject = JObject.Parse(strResponse);
List<PushbulletDevice> deviceList = new List<PushbulletDevice>();
foreach (JObject device in jObject["devices"])
{
PushbulletDevice currDevice = new PushbulletDevice();
currDevice.Key = device["iden"].ToString();
currDevice.Name = device["extras"]["nickname"].ToString();
deviceList.Add(currDevice);
}
return deviceList;
}
}
catch (Exception ex)
{
Errors.Add(ex.ToString());
return null;
}
}
}
public class PushbulletDevice
{
public string Name = string.Empty;
public string Key = string.Empty;
}
public class PushbulletSettings
{
public string UserAPIKey = string.Empty;
public bool ReturnPushURL = true;
public List<PushbulletDevice> DeviceList = new List<PushbulletDevice>();
public PushbulletDevice CurrentDevice = null; // TODO: Use int SelectedDevice
}
}

View file

@ -314,8 +314,6 @@ public bool IsActive(TextDestination destination)
{
switch (destination)
{
case TextDestination.Pushbullet:
return (PushbulletSettings != null) && !string.IsNullOrEmpty(PushbulletSettings.UserAPIKey) && (PushbulletSettings.DeviceList != null);
case TextDestination.CustomTextUploader:
return CustomUploadersList != null && CustomUploadersList.IsValidIndex(CustomTextUploaderSelected);
default:
@ -355,6 +353,8 @@ public bool IsActive(FileDestination destination)
return OAuthInfo.CheckOAuth(JiraOAuthInfo);
case FileDestination.Mega:
return MegaAuthInfos != null && MegaAuthInfos.Email != null && MegaAuthInfos.Hash != null && MegaAuthInfos.PasswordAesKey != null;
case FileDestination.Pushbullet:
return PushbulletSettings != null && !string.IsNullOrEmpty(PushbulletSettings.UserAPIKey) && PushbulletSettings.DeviceList != null;
default:
return true;
}

View file

@ -185,7 +185,7 @@
<Compile Include="GUI\UploadersConfigForm.Designer.cs">
<DependentUpon>UploadersConfigForm.cs</DependentUpon>
</Compile>
<Compile Include="TextUploaders\Pushbullet.cs" />
<Compile Include="FileUploaders\Pushbullet.cs" />
<Compile Include="TextUploaders\Upaste.cs" />
<Compile Include="UploadersConfig.cs" />
<Compile Include="GUI\UserPassBox.cs">