Created
April 12, 2011 21:43
-
-
Save kvorion/916489 to your computer and use it in GitHub Desktop.
TwitterNetwork.java
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
package TwitterGraph; | |
import java.io.BufferedWriter; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Properties; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.User; | |
import twitter4j.auth.AccessToken; | |
import twitter4j.json.DataObjectFactory; | |
public class TwitterNetwork { | |
public Twitter twitterInstance = null; | |
public Properties properties = null; | |
/* | |
* Creates a new instance of twitter client | |
*/ | |
public TwitterNetwork() | |
{ | |
this.properties = new Properties(); | |
try | |
{ | |
properties.load(new FileReader("twitter4j.properties")); | |
TwitterFactory twitterFactory = new TwitterFactory(); | |
AccessToken accessToken = new AccessToken(properties.getProperty("oauth.token"), | |
properties.getProperty("oauth.tokenSecret")); | |
this.twitterInstance = twitterFactory.getInstance(accessToken); | |
} | |
catch (FileNotFoundException e) | |
{ | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
TwitterNetwork tn = new TwitterNetwork(); | |
//tn.GetFriends(); | |
tn.GetFollowers(); | |
} | |
/* | |
* Gets my timeline on twitter...just for kicks.. | |
*/ | |
public void GetTimeLine() | |
{ | |
try { | |
List<Status> statuses = this.twitterInstance.getFriendsTimeline(); | |
for (Status status: statuses) | |
{ | |
System.out.println(status.getUser().getName() + "\t" + status.getText()); | |
} | |
} catch (TwitterException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* Get a list of followers | |
*/ | |
public void GetFollowers() | |
{ | |
try { | |
long[] longids = this.twitterInstance.getFollowersIDs(-1).getIDs(); | |
for (long longid:longids) | |
{ | |
User user = this.twitterInstance.showUser(longid); | |
BufferedWriter writer = new BufferedWriter(new FileWriter("followers/" + longid + "follower.txt")); | |
String output = DataObjectFactory.getRawJSON(user); | |
writer.write(output + "\n"); | |
writer.flush(); | |
writer.close(); | |
} | |
} catch (TwitterException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public void GetFriends() | |
{ | |
try { | |
long[] longids = this.twitterInstance.getFriendsIDs(-1).getIDs(); | |
for (long longid:longids) | |
{ | |
User user = this.twitterInstance.showUser(longid); | |
BufferedWriter writer = new BufferedWriter(new FileWriter("friends/" + longid + "friend.txt")); | |
String output = DataObjectFactory.getRawJSON(user); | |
System.out.println(user.toString()); | |
writer.write(output + "\n"); | |
writer.flush(); | |
writer.close(); | |
} | |
} catch (TwitterException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment