Added Twitter image uploader

This commit is contained in:
Jaex 2014-06-15 13:29:09 +03:00
parent 5f4f3e4077
commit 18fe663f02
16 changed files with 602 additions and 446 deletions

View file

@ -87,7 +87,7 @@ public override void CheckUpdate()
}
catch (Exception e)
{
DebugHelper.WriteException(e, "Update check failed");
DebugHelper.WriteException(e, "GitHub update check failed");
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;

View file

@ -94,7 +94,7 @@ public override void CheckUpdate()
}
catch (Exception e)
{
DebugHelper.WriteException(e, "Update check failed");
DebugHelper.WriteException(e, "XML update check failed");
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;

View file

@ -570,17 +570,7 @@ private void DoAfterUploadJobs()
if (Info.TaskSettings.AfterUploadJob.HasFlag(AfterUploadTasks.ShareURLToSocialNetworkingService))
{
OAuthInfo twitterOAuth = Program.UploadersConfig.TwitterOAuthInfoList.ReturnIfValidIndex(Program.UploadersConfig.TwitterSelectedAccount);
if (twitterOAuth != null)
{
using (TwitterMsg twitter = new TwitterMsg(twitterOAuth))
{
twitter.Message = Info.Result.ToString();
twitter.Config = Program.UploadersConfig.TwitterClientConfig;
twitter.ShowDialog();
}
}
DoSocialNetworkingService();
}
if (Info.TaskSettings.AfterUploadJob.HasFlag(AfterUploadTasks.SendURLWithEmail))
@ -686,6 +676,10 @@ public UploadResult UploadImage(Stream stream, string fileName)
AlbumID = Program.UploadersConfig.PicasaAlbumID
};
break;
case ImageDestination.Twitter:
OAuthInfo twitterOAuth = Program.UploadersConfig.TwitterOAuthInfoList.ReturnIfValidIndex(Program.UploadersConfig.TwitterSelectedAccount);
imageUploader = new TwitterUploader(twitterOAuth);
break;
case ImageDestination.Twitpic:
int indexTwitpic = Program.UploadersConfig.TwitterSelectedAccount;
@ -760,9 +754,8 @@ public UploadResult UploadText(Stream stream, string fileName)
textUploader = new Paste_ee(Program.UploadersConfig.Paste_eeUserAPIKey);
break;
case TextDestination.Gist:
textUploader = Program.UploadersConfig.GistAnonymousLogin
? new Gist(Program.UploadersConfig.GistPublishPublic)
: new Gist(Program.UploadersConfig.GistPublishPublic, Program.UploadersConfig.GistOAuth2Info);
textUploader = Program.UploadersConfig.GistAnonymousLogin ? new Gist(Program.UploadersConfig.GistPublishPublic) :
new Gist(Program.UploadersConfig.GistPublishPublic, Program.UploadersConfig.GistOAuth2Info);
break;
case TextDestination.Upaste:
textUploader = new Upaste(Program.UploadersConfig.UpasteUserKey)
@ -1024,6 +1017,26 @@ public UploadResult ShortenURL(string url)
return null;
}
public void DoSocialNetworkingService()
{
switch (Info.TaskSettings.SocialNetworkingServiceDestination)
{
case SocialNetworkingService.Twitter:
OAuthInfo twitterOAuth = Program.UploadersConfig.TwitterOAuthInfoList.ReturnIfValidIndex(Program.UploadersConfig.TwitterSelectedAccount);
if (twitterOAuth != null)
{
using (TwitterMsg twitter = new TwitterMsg(twitterOAuth))
{
twitter.Message = Info.Result.ToString();
twitter.Config = Program.UploadersConfig.TwitterClientConfig;
twitter.ShowDialog();
}
}
break;
}
}
private void ThreadCompleted()
{
OnUploadCompleted();

View file

@ -42,6 +42,8 @@ public enum ImageDestination
Photobucket,
[Description("picasaweb.google.com")]
Picasa,
[Description("twitter.com")]
Twitter,
[Description("twitpic.com")]
Twitpic,
[Description("twitsnaps.com")]

View file

@ -31,23 +31,23 @@ private void InitializeComponent()
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.txtTweet = new System.Windows.Forms.TextBox();
this.lblCount = new System.Windows.Forms.Label();
this.lblTweetLength = new System.Windows.Forms.Label();
this.lbUsers = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(368, 232);
this.btnOK.Location = new System.Drawing.Point(368, 208);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(81, 24);
this.btnOK.Size = new System.Drawing.Size(80, 24);
this.btnOK.TabIndex = 2;
this.btnOK.Text = "&Tweet It";
this.btnOK.Text = "&Tweet";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(456, 232);
this.btnCancel.Location = new System.Drawing.Point(456, 208);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(80, 24);
this.btnCancel.TabIndex = 3;
@ -59,32 +59,32 @@ private void InitializeComponent()
//
this.txtTweet.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtTweet.Font = new System.Drawing.Font("Calibri", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
this.txtTweet.Location = new System.Drawing.Point(120, 8);
this.txtTweet.MaxLength = 140;
this.txtTweet.Location = new System.Drawing.Point(8, 8);
this.txtTweet.Multiline = true;
this.txtTweet.Name = "txtTweet";
this.txtTweet.Size = new System.Drawing.Size(416, 216);
this.txtTweet.TabIndex = 1;
this.txtTweet.Size = new System.Drawing.Size(416, 192);
this.txtTweet.TabIndex = 0;
this.txtTweet.TextChanged += new System.EventHandler(this.txtTweet_TextChanged);
//
// lblCount
// lblTweetLength
//
this.lblCount.AutoSize = true;
this.lblCount.Location = new System.Drawing.Point(120, 232);
this.lblCount.Name = "lblCount";
this.lblCount.Size = new System.Drawing.Size(25, 13);
this.lblCount.TabIndex = 1;
this.lblCount.Text = "140";
this.lblTweetLength.AutoSize = true;
this.lblTweetLength.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
this.lblTweetLength.Location = new System.Drawing.Point(8, 208);
this.lblTweetLength.Name = "lblTweetLength";
this.lblTweetLength.Size = new System.Drawing.Size(36, 20);
this.lblTweetLength.TabIndex = 2;
this.lblTweetLength.Text = "140";
//
// lbUsers
//
this.lbUsers.FormattingEnabled = true;
this.lbUsers.IntegralHeight = false;
this.lbUsers.Location = new System.Drawing.Point(8, 8);
this.lbUsers.Location = new System.Drawing.Point(432, 8);
this.lbUsers.Name = "lbUsers";
this.lbUsers.Size = new System.Drawing.Size(104, 216);
this.lbUsers.Size = new System.Drawing.Size(104, 192);
this.lbUsers.Sorted = true;
this.lbUsers.TabIndex = 0;
this.lbUsers.TabIndex = 1;
this.lbUsers.SelectedIndexChanged += new System.EventHandler(this.lbUsers_SelectedIndexChanged);
this.lbUsers.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lbUsers_KeyDown);
//
@ -93,21 +93,20 @@ private void InitializeComponent()
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(544, 263);
this.ClientSize = new System.Drawing.Size(544, 240);
this.Controls.Add(this.txtTweet);
this.Controls.Add(this.lbUsers);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.lblCount);
this.Controls.Add(this.lblTweetLength);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "TwitterMsg";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Description";
this.Text = "Update Twitter status";
this.TopMost = true;
this.Load += new System.EventHandler(this.TwitterMsg_Load);
this.Shown += new System.EventHandler(this.TwitterMsg_Shown);
this.ResumeLayout(false);
this.PerformLayout();
@ -117,7 +116,7 @@ private void InitializeComponent()
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Label lblCount;
private System.Windows.Forms.Label lblTweetLength;
private System.Windows.Forms.ListBox lbUsers;
private System.Windows.Forms.TextBox txtTweet;
}

View file

@ -35,8 +35,6 @@ namespace UploadersLib
{
public partial class TwitterMsg : Form
{
public string ActiveAccountName { get; set; }
public string Message
{
get
@ -49,41 +47,66 @@ public string Message
}
}
private int length;
public int Length
{
get
{
return length;
}
set
{
length = value;
UpdateLength();
}
}
public bool IsValidMessage
{
get
{
return !string.IsNullOrEmpty(Message) && Message.Length <= Length;
}
}
public OAuthInfo AuthInfo { get; set; }
public TwitterClientSettings Config { get; set; }
public TwitterMsg(OAuthInfo oauth, string title)
public TwitterMsg(OAuthInfo oauth)
{
InitializeComponent();
AuthInfo = oauth;
Text = title;
Icon = Resources.Twitter;
Length = Twitter.MessageLimit;
Config = new TwitterClientSettings();
}
public TwitterMsg(OAuthInfo oauth)
: this(oauth, "Update Twitter status")
public TwitterMsg()
: this(null)
{
}
public TwitterMsg(string title)
: this(null, title)
private void UpdateLength()
{
lblTweetLength.Text = (Length - Message.Length).ToString();
btnOK.Enabled = IsValidMessage;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtTweet.Text))
if (IsValidMessage)
{
DialogResult = DialogResult.OK;
if (AuthInfo != null && !string.IsNullOrEmpty(Message))
if (AuthInfo != null)
{
Hide();
try
{
TweetStatus status = new Twitter(AuthInfo).TweetMessage(Message);
TwitterStatusResponse status = new Twitter(AuthInfo).TweetMessage(Message);
if (status != null && !string.IsNullOrEmpty(status.in_reply_to_screen_name))
{
Config.AddUser(status.in_reply_to_screen_name);
@ -105,9 +128,17 @@ private void btnCancel_Click(object sender, EventArgs e)
Close();
}
private void TwitterMsg_Load(object sender, EventArgs e)
{
foreach (string user in Config.Addressees)
{
lbUsers.Items.Add(user);
}
}
private void txtTweet_TextChanged(object sender, EventArgs e)
{
lblCount.Text = (140 - txtTweet.Text.Length).ToString();
UpdateLength();
}
private void lbUsers_SelectedIndexChanged(object sender, EventArgs e)
@ -120,14 +151,6 @@ private void lbUsers_SelectedIndexChanged(object sender, EventArgs e)
}
}
private void TwitterMsg_Load(object sender, EventArgs e)
{
foreach (string user in Config.Addressees)
{
lbUsers.Items.Add(user);
}
}
private void lbUsers_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
@ -155,11 +178,6 @@ private void lbUsers_KeyDown(object sender, KeyEventArgs e)
}
}
}
private void TwitterMsg_Shown(object sender, EventArgs e)
{
txtTweet.Focus();
}
}
public class TwitterClientSettings

View file

@ -112,9 +112,9 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

File diff suppressed because it is too large Load diff

View file

@ -45,7 +45,7 @@ public UploadersConfigForm(UploadersConfig uploadersConfig)
{
Config = uploadersConfig;
InitializeComponent();
string title = "ShareX - Outputs Configuration";
string title = "ShareX - Destination settings";
if (!string.IsNullOrEmpty(Config.FilePath))
{
title += " - " + Config.FilePath;

View file

@ -75,7 +75,7 @@ public override UploadResult Upload(Stream stream, string fileName)
case TwitPicUploadType.UPLOAD_IMAGE_ONLY:
return Upload(stream, fileName, UploadLink);
case TwitPicUploadType.UPLOAD_IMAGE_AND_TWITTER:
using (TwitterMsg msgBox = new TwitterMsg("Update Twitter Status"))
using (TwitterMsg msgBox = new TwitterMsg())
{
msgBox.ShowDialog();
return Upload(stream, fileName, UploadAndPostLink, msgBox.Message);

View file

@ -47,7 +47,7 @@ public TwitSnapsUploader(string apiKey, OAuthInfo oauth)
public override UploadResult Upload(Stream stream, string fileName)
{
using (TwitterMsg msgBox = new TwitterMsg("Update Twitter Status"))
using (TwitterMsg msgBox = new TwitterMsg())
{
msgBox.ShowDialog();
return Upload(stream, fileName, msgBox.Message);

View file

@ -0,0 +1,62 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UploadersLib.HelperClasses;
using UploadersLib.SocialServices;
namespace UploadersLib.ImageUploaders
{
public sealed class TwitterUploader : ImageUploader
{
public OAuthInfo AuthInfo { get; set; }
public TwitterUploader(OAuthInfo oauth)
{
AuthInfo = oauth;
}
public override UploadResult Upload(Stream stream, string fileName)
{
using (TwitterMsg twitterMsg = new TwitterMsg())
{
twitterMsg.Length = Twitter.MessageMediaLimit;
if (twitterMsg.ShowDialog() == DialogResult.OK)
{
Twitter twitter = new Twitter(AuthInfo);
return twitter.TweetMessageWithMedia(twitterMsg.Message, stream, fileName);
}
}
return new UploadResult() { IsURLExpected = false };
}
}
}

View file

@ -85,7 +85,7 @@ public override UploadResult Upload(Stream stream, string fileName)
case YfrogUploadType.UPLOAD_IMAGE_ONLY:
return Upload(stream, fileName, "");
case YfrogUploadType.UPLOAD_IMAGE_AND_TWITTER:
using (TwitterMsg msgBox = new TwitterMsg("Update Twitter Status"))
using (TwitterMsg msgBox = new TwitterMsg())
{
msgBox.ShowDialog();
return Upload(stream, fileName, msgBox.Message);

View file

@ -25,6 +25,8 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using UploadersLib.HelperClasses;
namespace UploadersLib.SocialServices
@ -32,10 +34,10 @@ namespace UploadersLib.SocialServices
public class Twitter : Uploader, IOAuth
{
private const string APIVersion = "1.1";
private const string URLRequestToken = "https://api.twitter.com/oauth/request_token";
private const string URLAuthorize = "https://api.twitter.com/oauth/authorize";
private const string URLAccessToken = "https://api.twitter.com/oauth/access_token";
private const string URLTweet = "https://api.twitter.com/" + APIVersion + "/statuses/update.json";
private const int characters_reserved_per_media = 23;
public const int MessageLimit = 140;
public const int MessageMediaLimit = MessageLimit - characters_reserved_per_media;
public OAuthInfo AuthInfo { get; set; }
@ -46,37 +48,94 @@ public Twitter(OAuthInfo oauth)
public string GetAuthorizationURL()
{
return GetAuthorizationURL(URLRequestToken, URLAuthorize, AuthInfo);
return GetAuthorizationURL("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/authorize", AuthInfo);
}
public bool GetAccessToken(string verificationCode)
{
AuthInfo.AuthVerifier = verificationCode;
return GetAccessToken(URLAccessToken, AuthInfo);
return GetAccessToken("https://api.twitter.com/oauth/access_token", AuthInfo);
}
public TweetStatus TweetMessage(string message)
public TwitterStatusResponse TweetMessage(string message)
{
if (message.Length > MessageLimit)
{
message = message.Remove(MessageLimit);
}
string url = string.Format("https://api.twitter.com/{0}/statuses/update.json", APIVersion);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("status", message);
string query = OAuthManager.GenerateQuery(URLTweet, args, HttpMethod.POST, AuthInfo);
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
if (!string.IsNullOrEmpty(response))
{
return JsonConvert.DeserializeObject<TweetStatus>(response);
return JsonConvert.DeserializeObject<TwitterStatusResponse>(response);
}
return null;
}
public UploadResult TweetMessageWithMedia(string message, Stream stream, string fileName)
{
if (message.Length > MessageMediaLimit)
{
message = message.Remove(MessageMediaLimit);
}
string url = string.Format("https://api.twitter.com/{0}/statuses/update_with_media.json", APIVersion);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("status", message);
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo);
UploadResult result = UploadData(stream, query, fileName, "media[]");
if (!string.IsNullOrEmpty(result.Response))
{
TwitterStatusResponse status = JsonConvert.DeserializeObject<TwitterStatusResponse>(result.Response);
if (status != null && status.user != null)
{
result.URL = status.GetTweetURL();
}
}
return result;
}
public string GetConfiguration()
{
string url = string.Format("https://api.twitter.com/{0}/help/configuration.json", APIVersion);
string query = OAuthManager.GenerateQuery(url, null, HttpMethod.GET, AuthInfo);
string response = SendRequest(HttpMethod.GET, query);
return response;
}
}
public class TweetStatus
public class TwitterStatusResponse
{
public long id { get; set; }
public string text { get; set; }
public string in_reply_to_screen_name { get; set; }
public TwitterStatusUser user { get; set; }
public string GetTweetURL()
{
return string.Format("https://twitter.com/{0}/status/{1}", user.screen_name, id);
}
}
public class TwitterStatusUser
{
public long id { get; set; }
public string name { get; set; }
public string screen_name { get; set; }
}
}

View file

@ -304,6 +304,8 @@ public bool IsActive(ImageDestination destination)
return PhotobucketAccountInfo != null && OAuthInfo.CheckOAuth(PhotobucketOAuthInfo);
case ImageDestination.Picasa:
return OAuth2Info.CheckOAuth(PicasaOAuth2Info);
case ImageDestination.Twitter:
return TwitterOAuthInfoList != null && TwitterOAuthInfoList.IsValidIndex(TwitterSelectedAccount);
case ImageDestination.Twitpic:
case ImageDestination.Twitsnaps:
return TwitterOAuthInfoList != null && TwitterOAuthInfoList.IsValidIndex(TwitterSelectedAccount);

View file

@ -178,6 +178,7 @@
<Compile Include="FileUploaders\Pushbullet.cs" />
<Compile Include="FileUploaders\GfycatUploader.cs" />
<Compile Include="HelperClasses\OAuth\IOAuthBase.cs" />
<Compile Include="ImageUploaders\TwitterUploader.cs" />
<Compile Include="TextUploaders\Upaste.cs" />
<Compile Include="UploadersConfig.cs" />
<Compile Include="GUI\UserPassBox.cs">