2014-03-30 22:16:20 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 ShareX Team
|
2014-03-30 22:16:20 +13: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)
|
|
|
|
|
|
|
|
|
|
// Credits: https://github.com/BallisticLingonberries
|
2014-09-04 21:18:46 +12:00
|
|
|
|
// API Information: https://docs.pushbullet.com/http/
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2014-03-30 22:16:20 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2014-03-30 22:16:20 +13:00
|
|
|
|
{
|
|
|
|
|
public sealed class Pushbullet : FileUploader
|
|
|
|
|
{
|
|
|
|
|
public PushbulletSettings Config { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Pushbullet(PushbulletSettings config)
|
|
|
|
|
{
|
|
|
|
|
Config = config;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
private const string
|
|
|
|
|
wwwPushesURL = "https://www.pushbullet.com/pushes",
|
2014-09-05 07:58:44 +12:00
|
|
|
|
apiURL = "https://api.pushbullet.com/v2",
|
2014-09-04 21:18:46 +12:00
|
|
|
|
apiGetDevicesURL = apiURL + "/devices",
|
|
|
|
|
apiSendPushURL = apiURL + "/pushes",
|
|
|
|
|
apiRequestFileUploadURL = apiURL + "/upload-request";
|
|
|
|
|
|
2014-03-30 22:49:43 +13:00
|
|
|
|
public UploadResult PushFile(Stream stream, string fileName)
|
2014-03-30 22:16:20 +13:00
|
|
|
|
{
|
|
|
|
|
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
|
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
Dictionary<string, string> pushArgs, upArgs = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
upArgs.Add("file_name", fileName);
|
|
|
|
|
|
|
|
|
|
string uploadRequest = SendRequest(HttpMethod.POST, apiRequestFileUploadURL, upArgs, headers);
|
|
|
|
|
|
|
|
|
|
if (uploadRequest == null) return null;
|
|
|
|
|
|
|
|
|
|
PushbulletResponseFileUpload fileInfo = JsonConvert.DeserializeObject<PushbulletResponseFileUpload>(uploadRequest);
|
|
|
|
|
|
|
|
|
|
if (fileInfo == null) return null;
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
pushArgs = upArgs;
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
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);
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
2014-03-30 22:49:43 +13:00
|
|
|
|
if (push != null)
|
2014-09-04 21:18:46 +12:00
|
|
|
|
uploadResult.URL = wwwPushesURL + "?push_iden=" + push.iden;
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
return uploadResult;
|
2014-03-30 22:16:20 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 22:49:43 +13:00
|
|
|
|
private string Push(string pushType, string valueType, string value, string title)
|
|
|
|
|
{
|
|
|
|
|
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("device_iden", Config.CurrentDevice.Key);
|
|
|
|
|
args.Add("type", pushType);
|
|
|
|
|
args.Add("title", title);
|
|
|
|
|
args.Add(valueType, value);
|
|
|
|
|
|
2014-09-04 23:11:42 +12:00
|
|
|
|
if (valueType != "body")
|
|
|
|
|
{
|
|
|
|
|
if (pushType == "link")
|
|
|
|
|
args.Add("body", value);
|
|
|
|
|
else
|
|
|
|
|
args.Add("body", "Sent via ShareX");
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, apiSendPushURL, args, headers);
|
|
|
|
|
|
|
|
|
|
if (response == null) return null;
|
2014-03-30 22:49:43 +13:00
|
|
|
|
|
|
|
|
|
PushbulletResponsePush push = JsonConvert.DeserializeObject<PushbulletResponsePush>(response);
|
|
|
|
|
|
|
|
|
|
if (push != null)
|
2014-09-04 21:18:46 +12:00
|
|
|
|
return wwwPushesURL + "?push_iden=" + push.iden;
|
2014-03-30 22:49:43 +13:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string PushNote(string note, string title)
|
|
|
|
|
{
|
|
|
|
|
return Push("note", "body", note, title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string PushLink(string link, string title)
|
|
|
|
|
{
|
|
|
|
|
return Push("link", "url", link, title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("Missing device key.");
|
|
|
|
|
|
2014-09-05 07:58:44 +12:00
|
|
|
|
return PushFile(stream, fileName);
|
2014-03-30 22:49:43 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 22:16:20 +13:00
|
|
|
|
public List<PushbulletDevice> GetDeviceList()
|
|
|
|
|
{
|
|
|
|
|
NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");
|
|
|
|
|
|
2014-09-04 21:18:46 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.GET, apiGetDevicesURL, headers: headers);
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
|
|
|
|
PushbulletResponseDevices devicesResponse = JsonConvert.DeserializeObject<PushbulletResponseDevices>(response);
|
|
|
|
|
|
|
|
|
|
if (devicesResponse != null && devicesResponse.devices != null)
|
2014-09-04 21:18:46 +12:00
|
|
|
|
return devicesResponse.devices.Select(x => new PushbulletDevice { Key = x.iden, Name = x.nickname }).ToList();
|
2014-03-30 22:16:20 +13:00
|
|
|
|
|
|
|
|
|
return new List<PushbulletDevice>();
|
|
|
|
|
}
|
2014-09-04 21:18:46 +12:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 23:11:42 +12:00
|
|
|
|
private class PushbulletResponseFileUpload
|
2014-09-04 21:18:46 +12:00
|
|
|
|
{
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 23:11:42 +12:00
|
|
|
|
private class PushbulletResponseFileUploadData
|
2014-09-04 21:18:46 +12:00
|
|
|
|
{
|
|
|
|
|
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; }
|
|
|
|
|
}
|
2014-03-30 22:16:20 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PushbulletDevice
|
|
|
|
|
{
|
|
|
|
|
public string Key { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PushbulletSettings
|
|
|
|
|
{
|
|
|
|
|
public string UserAPIKey = string.Empty;
|
|
|
|
|
public List<PushbulletDevice> DeviceList = new List<PushbulletDevice>();
|
2014-03-31 00:37:38 +13:00
|
|
|
|
public int SelectedDevice = 0;
|
|
|
|
|
|
|
|
|
|
public PushbulletDevice CurrentDevice
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (DeviceList.IsValidIndex(SelectedDevice))
|
|
|
|
|
return DeviceList[SelectedDevice];
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-30 22:16:20 +13:00
|
|
|
|
}
|
|
|
|
|
}
|