Last active
August 29, 2015 14:22
-
-
Save kashmervil/2567ff630d13cfb46069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Text; | |
using TweetSharp; | |
namespace CsharpPoster | |
{ | |
static class TwitterSettings | |
{ | |
public static string CredentialsPath = ".twitter_credentials"; | |
public static string ConsumerKey = ""; | |
public static string ConsumerKeySecret = ""; | |
public static string AccessToken = ""; | |
public static string AccessTokenSecret = ""; | |
public static string[] CredentialsLines | |
{ | |
get { return new []{ConsumerKey, ConsumerKeySecret, AccessToken, AccessTokenSecret}; } | |
set | |
{ | |
ConsumerKey = value[0]; | |
ConsumerKeySecret = value[1]; | |
AccessToken = value[2]; | |
AccessTokenSecret = value[3]; | |
} | |
} | |
} | |
class Uploader | |
{ | |
static void PostImage(string filePath, string postText) | |
{ | |
var service = new TwitterService(TwitterSettings.ConsumerKey | |
, TwitterSettings.ConsumerKeySecret | |
, TwitterSettings.AccessToken | |
, TwitterSettings.AccessTokenSecret); | |
var options = new SendTweetWithMediaOptions | |
{ | |
Status = postText, | |
Images = | |
new Dictionary<string, Stream> | |
{ | |
{"1", new FileStream(filePath, FileMode.Open, FileAccess.Read)} | |
} | |
}; | |
try | |
{ | |
var status = service.SendTweetWithMedia(options); | |
Console.WriteLine("Tweet Succeeded!\n Author {0}, Date {1}", status.Author, status.CreatedDate); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Error: Got an Exception while making tweet.\n {0}", ex.Message); | |
} | |
} | |
static void PrintUsage() | |
{ | |
var usage = new StringBuilder().Append("TwitterUploader usage:\n") | |
.Append(" TwitterUploader.exe -auth consumerKey consumerSecret\n") | |
.Append(" TwitterUploader.exe -post \"path_to_photo\" \"tweet text\"\n") | |
.AppendFormat("\nCredentials file '{0}' should be placed in the same folder as TwitterUploader.exe\n", | |
TwitterSettings.CredentialsPath) | |
.Append( | |
"Credentials file format: \n ConsumerKey\n ConsumerKeySecret\n AccessToken\n AccessTokenSecret"); | |
Console.WriteLine(usage.ToString()); | |
} | |
static void Main(string[] args) | |
{ | |
if (args.Length != 3) | |
{ | |
PrintUsage(); | |
return; | |
} | |
if (args[0] == "-auth") | |
{ | |
TwitterSettings.ConsumerKey = args[1]; | |
TwitterSettings.ConsumerKeySecret = args[2]; | |
var service = new TwitterService(TwitterSettings.ConsumerKey, TwitterSettings.ConsumerKeySecret); | |
var requestToken = service.GetRequestToken(); | |
var uri = service.GetAuthorizationUri(requestToken); | |
Console.WriteLine("Follow this URL to get Verification code {0}", uri); | |
if (Environment.OSVersion.VersionString.Contains("Win")) | |
{ | |
Process.Start(uri.ToString()); | |
} | |
Console.WriteLine("Enter Verification code"); | |
var code = Console.ReadLine(); | |
var access = service.GetAccessToken(requestToken, code); | |
TwitterSettings.AccessToken = access.Token; | |
TwitterSettings.AccessTokenSecret = access.TokenSecret; | |
service.AuthenticateWith(TwitterSettings.AccessToken, TwitterSettings.AccessTokenSecret); | |
File.WriteAllLines(TwitterSettings.CredentialsPath, TwitterSettings.CredentialsLines); | |
Console.WriteLine("\n Authenticated successfully. Use TwitterUploader.exe -post \"path_to_photo\" \"tweet text\"\n"); | |
return; | |
} | |
if (args[0] == "-post") | |
{ | |
if (!File.Exists(TwitterSettings.CredentialsPath)) | |
{ | |
File.CreateText(TwitterSettings.CredentialsPath); | |
Console.WriteLine("Error: Credentials file {0} is empty. Unable to authenticate"); | |
return; | |
} | |
var credentials = File.ReadAllLines(TwitterSettings.CredentialsPath); | |
if (credentials.Length != 4) | |
{ | |
Console.WriteLine("Error: Credentials file {0} is in invalid format.", TwitterSettings.CredentialsPath); | |
PrintUsage(); | |
return; | |
} | |
TwitterSettings.CredentialsLines = File.ReadAllLines(TwitterSettings.CredentialsPath); | |
var photoPath = args[1]; | |
if (!File.Exists(photoPath)) | |
{ | |
Console.WriteLine("Error: PhotoPath doesn't exist"); | |
PrintUsage(); | |
return; | |
} | |
PostImage(photoPath, args[2]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment