Last active
December 14, 2015 16:19
-
-
Save loadedsith/5113946 to your computer and use it in GitHub Desktop.
processing twitter sender
This file contains 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
/* | |
Just a simple Processing and Twitter thingy majiggy | |
RobotGrrl.com | |
Code licensed under: | |
CC-BY | |
*/ | |
// First step is to register your Twitter application at dev.twitter.com | |
// Once registered, you will have the info for the OAuth tokens | |
// You can get the Access token info by clicking on the button on the | |
// right on your twitter app's page | |
// Good luck, and have fun! | |
// This is where you enter your Oauth info | |
static String OAuthConsumerKey = "xxx"; | |
static String OAuthConsumerSecret = "xxx"; | |
// This is where you enter your Access Token info | |
static String AccessToken = "xxx-xxx"; | |
static String AccessTokenSecret = "xxx"; | |
// Just some random variables kicking around | |
String myTimeline; | |
java.util.List statuses = null; | |
User[] friends; | |
TwitterFactory twitterFactory; | |
Twitter twitter; | |
RequestToken requestToken; | |
String[] theSearchTweets = new String[11]; | |
void setup() { | |
size(100, 100); | |
background(0); | |
connectTwitter(); | |
sendTweet("Hey from Simple Processing woop woop #loadedsith #robotgirl"); | |
} | |
void draw() { | |
background(0); | |
} | |
// Initial connection | |
void connectTwitter() { | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setOAuthConsumerKey(OAuthConsumerKey); | |
cb.setOAuthConsumerSecret(OAuthConsumerSecret); | |
cb.setOAuthAccessToken(AccessToken); | |
cb.setOAuthAccessTokenSecret(AccessTokenSecret); | |
twitterFactory = new TwitterFactory(cb.build()); | |
twitter = twitterFactory.getInstance(); | |
println("connected"); | |
} | |
// Sending a tweet | |
void sendTweet(String t) { | |
try { | |
Status status = twitter.updateStatus(t); | |
println("Successfully updated the status to [" + status.getText() + "]."); | |
} catch(TwitterException e) { | |
println("Send tweet: " + e + " Status code: " + e.getStatusCode()); | |
} | |
} | |
// Loading up the access token | |
private static AccessToken loadAccessToken(){ | |
return new AccessToken(AccessToken, AccessTokenSecret); | |
} | |
// Get your tweets | |
void getTimeline() { | |
try { | |
statuses = twitter.getUserTimeline(); | |
} catch(TwitterException e) { | |
println("Get timeline: " + e + " Status code: " + e.getStatusCode()); | |
} | |
for (int i=0; i < statuses.size(); i++) { | |
Status status = (Status)statuses.get(i); | |
println(status.getUser().getName() + ": " + status.getText()); | |
} | |
} | |
// Search for tweets | |
/*void getSearchTweets() { | |
String queryStr = "@RobotGrrl"; | |
try { | |
Query query = new Query("source:twitter4j yusukey"); | |
//query.setRpp(10); // Get 10 of the 100 search results | |
QueryResult result = twitter.search(query); | |
for (Status status : result.getStatus()) { | |
println("@" + status.getUser().getScreenName() + ":" + status.getText()); | |
} | |
} catch (TwitterException e) { | |
println("Search tweets: " + e); | |
} | |
}*/ |
Thank you very much for the code. It worked perfectly.
My pleasure. :) I've made some edits, but just to whitespaces.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much, this demo just saved the day!! Not a lot of documentation out there yet for twitter4j 3.0.3 so I am so glad I found this. Much appreciated... and I will make sure you're credited :-)