ShareX/UploadersLib/ImageUploaders/TwitSnapsUploader.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
2013-11-03 23:53:49 +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)
using HelpersLib;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using UploadersLib.HelperClasses;
namespace UploadersLib.ImageUploaders
{
public sealed class TwitSnapsUploader : ImageUploader
{
public OAuthInfo AuthInfo { get; set; }
private const string APIURL = "http://twitsnaps.com/dev/image/upload.xml";
private string APIKey;
public TwitSnapsUploader(string apiKey, OAuthInfo oauth)
{
APIKey = apiKey;
AuthInfo = oauth;
}
public override UploadResult Upload(Stream stream, string fileName)
{
2014-06-15 22:29:09 +12:00
using (TwitterMsg msgBox = new TwitterMsg())
2013-11-03 23:53:49 +13:00
{
msgBox.ShowDialog();
return Upload(stream, fileName, msgBox.Message);
}
}
private UploadResult Upload(Stream stream, string fileName, string msg)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("appKey", APIKey);
args.Add("consumerKey", AuthInfo.ConsumerKey);
args.Add("consumerSecret", AuthInfo.ConsumerSecret);
args.Add("oauthToken", AuthInfo.UserToken);
args.Add("oauthSecret", AuthInfo.UserSecret);
if (!string.IsNullOrEmpty(msg))
{
args.Add("message", msg);
}
UploadResult result = UploadData(stream, APIURL, fileName, "media", args);
return ParseResult(result);
}
private UploadResult ParseResult(UploadResult result)
{
if (result.IsSuccess)
{
XDocument xd = XDocument.Parse(result.Response);
XElement xe = xd.Element("image");
if (xe != null)
{
string id = xe.GetElementValue("id");
result.URL = "http://twitsnaps.com/snap/" + id;
result.ThumbnailURL = "http://twitsnaps.com/thumb/" + id;
}
else
{
xe = xd.Element("error");
if (xe != null)
{
Errors.Add("Error: " + xe.GetElementValue("description"));
}
}
}
return result;
}
}
}