I code. I write. I draw. I learn. I love.
As a break from learning, during the weekend, I try to make something instead of absorbing new information. I was a bit bored, so I decided to try my hands at twitting from Java.
The first step was signing up as a developer in Twitter’s API page. It was a very interesting experience to call myself “developer” even if it was for learning purposes.
I found many variations of the API’s code structure. Most of it outdated. It took a few combined ones and the Twitter documentation to figure out the correct implementation. In the end, I got it to work by looking directly at twitter4j documentation. I would not have been able to understand it a few months ago. I really feel my brain took a great leap after months of practice.
After 2 hours of working on and off in between games with my toddler, I got it to work! It was a great, simple exercise to figure things out myself. This, considering I have no knowledge about REST API’s yet, though it is in my to-learn list for later this year.
You can find my code in my repository.
This is the code:
package twitter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collector;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class MainTwitter {
public static ConfigurationBuilder configT() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("xxx")
.setOAuthConsumerSecret("xxx")
.setOAuthAccessToken("xxx")
.setOAuthAccessTokenSecret("xxx").setTweetModeExtended(true);
return cb;
}
public static void main(String[] args) throws Exception {
createTweet();
}
public static void getTweets() {
TwitterFactory tf = new TwitterFactory(configT().build());
Twitter twitter = tf.getInstance();
Scanner scan = new Scanner(System.in);
int pageno = 1;
System.out.println("type an username");
String user = scan.nextLine();
List statuses = new ArrayList();
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 10);
statuses.addAll(twitter.getUserTimeline(user, page));
System.out.println("***********************************************");
System.out.println("Gathered " + twitter.getUserTimeline(user, page).size() + " tweets");
for (Status status : twitter.getUserTimeline(user, page)) {
System.out.println();
System.out.println(status.getCreatedAt() + " : " + status.getText());
System.out.println();
}
if (statuses.size() == size)
break;
} catch (TwitterException e) {
e.printStackTrace();
}
}
System.out.println("Total: " + statuses.size());
scan.close();
}
public static String createTweet() throws TwitterException {
TwitterFactory tf = new TwitterFactory(configT().build());
Twitter twitter = tf.getInstance();
Scanner scan = new Scanner(System.in);
System.out.println("Type your tweet!");
String statusS = scan.nextLine();
Status status = twitter.updateStatus(statusS);
System.out.println(status.getText());
System.out.println("Tweet was succesful!");
return status.getText();
}
}
Leave a Reply