Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

In this page you can find the example usage for twitter4j TwitterFactory TwitterFactory.

Prototype

public TwitterFactory() 

Source Link

Document

Creates a TwitterFactory with the root configuration.

Usage

From source file:com.twitstreet.twitter.AnnouncerMgrImpl.java

License:Open Source License

public void loadAnnouncers() {
    Connection connection = null;
    PreparedStatement ps = null;//from   w ww  .j  a v a2 s  .c om
    ResultSet rs = null;
    try {
        connection = dbMgr.getConnection();
        ps = connection.prepareStatement(LOAD_ANNOUNCER);
        rs = ps.executeQuery();
        while (rs.next()) {
            Announcer announcer = new Announcer();
            announcer.getDataFromResultSet(rs);
            announcerDataList.add(announcer);
            if (TWITSTREET_GAME.equals(announcer.getName())) {
                twitstreetGame = new TwitterFactory().getInstance();
                twitstreetGame.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret());
                twitstreetGame.setOAuthAccessToken(
                        new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret()));
            } else {
                Twitter twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret());
                twitter.setOAuthAccessToken(
                        new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret()));
                announcerList.add(twitter);
            }
        }
        logger.debug(DBConstants.QUERY_EXECUTION_SUCC + ps.toString());
    } catch (SQLException ex) {
        logger.error(DBConstants.QUERY_EXECUTION_FAIL + ps.toString(), ex);

    } finally {
        dbMgr.closeResources(connection, ps, rs);
    }

}

From source file:com.twitstreet.twitter.TwitstreetAnnouncerImpl.java

License:Open Source License

@Override
public boolean mention(Stock stock, String message) {

    if (addStockIntoAnnouncement(stock.getId())) {

        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(configMgr.getAnnouncerConsumerKey(), configMgr.getAnnouncerConsumerSecret());

        twitter.setOAuthAccessToken(/*from  w  ww .  j  av  a2s  . co  m*/
                new AccessToken(configMgr.getAnnouncerAccessToken(), configMgr.getAnnouncerAccessSecret()));

        try {
            twitter.updateStatus("@" + stock.getName() + " " + message);

        } catch (TwitterException e) {
            logger.error("sendMessage:" + stock.getName() + " " + message);
        }

        return true;
    }

    return false;
}

From source file:com.twitstreet.twitter.TwitterProxyImpl.java

License:Open Source License

@Inject
public TwitterProxyImpl(ConfigMgr configMgr, @Assisted("oauthToken") String oauthToken,
        @Assisted("oauthTokenSecret") String oauthTokenSecret) {
    this.configMgr = configMgr;
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(configMgr.getConsumerKey(), configMgr.getConsumerSecret());
    accessToken = new AccessToken(oauthToken, oauthTokenSecret);

    oToken = oauthToken;// w  w w .  j ava 2 s  . c om
    oSecret = oauthTokenSecret;

    twitter.setOAuthAccessToken(accessToken);
    this.setTwitter(twitter);

    //top
    woiedMap.put("Worldwide", 1);
    //1
    woiedMap.put("Argentina", 23424747);
    woiedMap.put("Australia", 23424748);
    woiedMap.put("Brazil", 23424768);
    woiedMap.put("Canada", 23424775);
    woiedMap.put("Chile", 23424782);
    woiedMap.put("Colombia", 23424787);
    woiedMap.put("Dominican Republic", 23424800);
    woiedMap.put("Ecuador", 23424801);
    woiedMap.put("France", 23424819);
    woiedMap.put("Germany", 23424829);
    woiedMap.put("Guatemala", 23424834);
    woiedMap.put("India", 23424848);
    //2
    woiedMap.put("Indonesia", 23424846);
    woiedMap.put("Ireland", 23424803);
    woiedMap.put("Italy", 23424853);
    woiedMap.put("Japan", 23424856);
    woiedMap.put("Malaysia", 23424901);
    woiedMap.put("Mexico", 23424900);
    woiedMap.put("Netherlands", 23424909);
    woiedMap.put("New Zeland", 23424916);
    woiedMap.put("Nigeria", 23424908);
    woiedMap.put("Pakistan", 23424922);
    woiedMap.put("Peru", 23424919);
    woiedMap.put("Philippines", 23424934);
    //3
    woiedMap.put("Russia", 23424936);
    woiedMap.put("Singapore", 23424948);
    woiedMap.put("South Africa", 23424942);
    woiedMap.put("Spain", 23424950);
    woiedMap.put("Sweden", 23424954);
    woiedMap.put("Turkey", 23424969);
    woiedMap.put("United Arab Emirates", 23424738);
    woiedMap.put("United Kingdom", 23424975);
    woiedMap.put("United States", 23424977);
    woiedMap.put("Venezuela", 23424982);
}

From source file:com.twitter.android.TwitterAuthDialog.java

License:Apache License

/**
 * Create a new dialog box.//  w ww . j a v  a  2 s.  co  m
 * @param context - Context to use.
 * @param consumerKey - Required by OAuth. Register you app in twitter to get this value.
 * @param consumerSecret - Required by OAuth. Register you app in twitter to get this value.
 * @param listener - Listen to various events.
 */
public TwitterAuthDialog(Context context, String consumerKey, String consumerSecret,
        TwitterDialogListener listener) {
    super(context);

    mTwitter = new TwitterFactory().getInstance();
    mTwitter.setOAuthConsumer(consumerKey, consumerSecret);

    mListener = listener;
}

From source file:com.vti.SplashScreen.java

License:Apache License

/**
 * Create authorization request and launch login url in the browser
 * //w  w w.ja va 2 s. c  o  m
 * @param button
 */
void createAuthorizationRequests(final ImageButton button) {
    // Set on click listener
    button.setOnClickListener(new OnClickListener() {
        public void onClick(final View view) {
            String authUrl = null;
            try {
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

                requestToken = twitter.getOAuthRequestToken(Constants.CALLBACK_URL);
                System.err.println("twitterUrl==" + requestToken.getAuthorizationURL());
                /**
                 * @Documented
                 * Warning:  getOAuthRequestToken method can only be invoked once before getting accessToken, otherwise 
                * Twitter returns 401 error code;
                 */
                //final URI twitterUrl = new URI(twitter.getOAuthRequestToken(Constants.CALLBACK_URL).getAuthorizationURL());
                final URI twitterUrl = new URI(requestToken.getAuthorizationURL());
                authUrl = twitterUrl.toString();
                Log.d(TAG, authUrl);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
            } catch (final Exception e) {
                Log.d(TAG, Log.stack2string(e));
                Log.d(TAG, "Caught exception in createAuthorizationRequests" + e.getMessage());
            }
        }
    });
}

From source file:com.vuze.client.plugins.twitter.TwitterPlugin.java

License:Open Source License

public void initialize(PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;

    String ac_str = plugin_interface.getPluginconfig().getPluginStringParameter("twitter.access.token", "");
    String acs_str = plugin_interface.getPluginconfig().getPluginStringParameter("twitter.access.token.secret",
            "");//ww w  .  j  a v a2s. c o  m

    if (ac_str.length() > 0 && acs_str.length() > 0) {

        access_token = new AccessToken(ac_str, acs_str);
    }

    ta_tweets = plugin_interface.getTorrentManager().getPluginAttribute("tweets");

    final LocaleUtilities loc_utils = plugin_interface.getUtilities().getLocaleUtilities();

    log = plugin_interface.getLogger().getChannel("Twitter");

    UIManager ui_manager = plugin_interface.getUIManager();

    config_model = ui_manager.createBasicPluginConfigModel("plugins", "twitter.name");

    enable = config_model.addBooleanParameter2(CONFIG_ENABLE, "twitter.enable", CONFIG_ENABLE_DEFAULT);

    LabelParameter tweet_info = config_model.addLabelParameter2("twitter.tweet.info");

    tweet_text = config_model.addStringParameter2(CONFIG_TWEET_ADDED, CONFIG_TWEET_ADDED,
            loc_utils.getLocalisedMessageText(CONFIG_TWEET_ADDED_DEFAULT));

    ParameterGroup tweet_group = config_model.createGroup("twitter.tweet.group",
            new Parameter[] { tweet_info, tweet_text });

    // twitter_user       = config_model.addStringParameter2( CONFIG_TWITTER_USER, "twitter.user", "" );
    // twitter_password    = config_model.addPasswordParameter2( CONFIG_TWITTER_PASSWORD, "twitter.password", PasswordParameter.ET_PLAIN, new byte[0] );

    LabelParameter oauth_info = config_model.addLabelParameter2("twitter.oauth.info");

    final ActionParameter oauth_setup = config_model.addActionParameter2("twitter.oauth.start.label",
            "twitter.oauth.start.button");

    final RequestToken[] request_token = { null };

    final StringParameter oauth_pin = config_model.addStringParameter2("twitter.oauth.pin", "twitter.oauth.pin",
            "");

    oauth_pin.setValue("");

    final ActionParameter oauth_done = config_model.addActionParameter2("twitter.oauth.done.label",
            "twitter.oauth.done.button");

    oauth_setup.addConfigParameterListener(new ConfigParameterListener() {
        public void configParameterChanged(ConfigParameter param) {
            oauth_setup.setEnabled(false);

            plugin_interface.getUtilities().createThread("Twitter Setup", new Runnable() {
                public void run() {
                    Twitter twitter = new TwitterFactory().getInstance();

                    try {
                        RequestToken requestToken = twitter.getOAuthRequestToken();

                        request_token[0] = requestToken;

                        oauth_done.setEnabled(true);

                        log.log("OAuth URL: " + requestToken.getAuthorizationURL());

                        plugin_interface.getUIManager().openURL(new URL(requestToken.getAuthorizationURL()));

                        Thread.sleep(5000);

                    } catch (Throwable e) {

                        log.log("OAuth setup failed", e);

                        plugin_interface.getUIManager().showMessageBox("twitter.oauth.error.title",
                                "twitter.oauth.error.details", UIManagerEvent.MT_OK);
                    } finally {

                        oauth_setup.setEnabled(true);
                    }
                }
            });
        }
    });

    oauth_done.addConfigParameterListener(new ConfigParameterListener() {
        public void configParameterChanged(ConfigParameter param) {
            plugin_interface.getUtilities().createThread("Twitter Setup", new Runnable() {
                public void run() {
                    try {
                        Twitter twitter = new TwitterFactory().getInstance();

                        AccessToken at = twitter.getOAuthAccessToken(request_token[0], oauth_pin.getValue());

                        access_token = at;

                        String token = at.getToken();
                        String token_secret = at.getTokenSecret();

                        plugin_interface.getPluginconfig().setPluginParameter("twitter.access.token", token);
                        plugin_interface.getPluginconfig().setPluginParameter("twitter.access.token.secret",
                                token_secret);

                        plugin_interface.getPluginconfig().save();

                        log.log("OAuth setup successful - token saved");

                        plugin_interface.getUIManager().showMessageBox("twitter.oauth.ok.title",
                                "twitter.oauth.ok.details", UIManagerEvent.MT_OK);

                    } catch (Throwable e) {

                        log.log("OAuth setup failed", e);

                        plugin_interface.getUIManager().showMessageBox("twitter.oauth.error.title",
                                "twitter.oauth.error.details", UIManagerEvent.MT_OK);
                    }
                }
            });
        }
    });

    oauth_done.setEnabled(false);

    ParameterGroup auth_group = config_model.createGroup("twitter.oauth.group",
            new Parameter[] { oauth_info, oauth_setup, oauth_pin, oauth_done });

    ActionParameter action = config_model.addActionParameter2("twitter.tweet_test", "twitter.send");

    action.addConfigParameterListener(new ConfigParameterListener() {
        public void configParameterChanged(ConfigParameter param) {
            plugin_interface.getUtilities().createThread("Twitter Test", new Runnable() {
                public void run() {
                    Map<String, String> params = new HashMap<String, String>();

                    params.put("%t", "<test_torrent_name>");
                    params.put("%m", "<magnet_uri>");

                    TwitterResult result = sendTweet(params);

                    if (result.isOK()) {

                        plugin_interface.getUIManager().showMessageBox("twitter.testok.title",
                                "twitter.testok.details", UIManagerEvent.MT_OK);
                    } else {

                        plugin_interface.getUIManager()
                                .showMessageBox("twitter.testfail.title",
                                        "!" + loc_utils.getLocalisedMessageText("twitter.testfail.details",
                                                new String[] { result.getError() }) + "!",
                                        UIManagerEvent.MT_OK);
                    }
                }
            });
        }
    });

    view_model = ui_manager.createBasicPluginViewModel(loc_utils.getLocalisedMessageText("twitter.name"));

    view_model.getActivity().setVisible(false);
    view_model.getProgress().setVisible(false);

    log.addListener(new LoggerChannelListener() {
        public void messageLogged(int type, String content) {
            view_model.getLogArea().appendText(content + "\n");
        }

        public void messageLogged(String str, Throwable error) {
            view_model.getLogArea().appendText(str + "\n");
            view_model.getLogArea().appendText(error.toString() + "\n");
        }
    });

    view_model.getStatus().setText(enable.getValue() ? "Enabled" : "Disabled");

    enable.addListener(new ParameterListener() {
        public void parameterChanged(Parameter p) {
            view_model.getStatus().setText(enable.getValue() ? "Enabled" : "Disabled");

            checkEnabled();
        }
    });

    enable.addEnabledOnSelection(tweet_info);
    enable.addEnabledOnSelection(tweet_text);
    enable.addEnabledOnSelection(oauth_info);
    enable.addEnabledOnSelection(oauth_setup);
    enable.addEnabledOnSelection(oauth_pin);
    enable.addEnabledOnSelection(oauth_done);

    enable.addEnabledOnSelection(action);

    plugin_interface.getUIManager().addUIListener(new UIManagerListener() {
        public void UIAttached(UIInstance instance) {
            if (instance instanceof UISWTInstance) {

                checkEnabled();
            }
        }

        public void UIDetached(UIInstance instance) {
        }
    });

    timer = plugin_interface.getUtilities().createTimer("refresher");

    timer_event = timer.addPeriodicEvent(60 * 1000, this);

    plugin_interface.getDownloadManager().addListener(this);
}

From source file:com.zisal.twit.crawl.core.Example.java

public static void main(String[] args) {
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(ApplicationConstant.TwitterKey.CUSTOMER_KEY,
            ApplicationConstant.TwitterKey.CUSTOMER_SECRET);
    twitter.setOAuthAccessToken(//  w  w w.j av  a 2s. com
            new AccessToken(ApplicationConstant.TwitterKey.TOKEN, ApplicationConstant.TwitterKey.TOKEN_SECRET));
    /*try{
    ResponseList<Status> responseList = twitter.getUserTimeline(new Paging(1, 5));
    for(Status s: responseList){
        System.out.println("Response List ".concat(s.getText()));
    }
    }catch(Exception e){
    e.printStackTrace();
    }
            
    long cursor = -1;
    IDs ids = null;
    System.out.println("Listing followers's ids.");
    do {
    try {
        ids = twitter.getFollowersIDs(ApplicationConstant.Twitter.SCREEN_NAME, cursor);
        for (long id : ids.getIDs()) {
            System.out.println(id);
            User user = twitter.showUser(id);
            System.out.println(user.getName());
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
    } while ((cursor = ids != null ? ids.getNextCursor() : 0) != 0);*/

    long cursor = -1;
    PagableResponseList<User> pagableFollowings = null;
    List<User> listFriends = new ArrayList<>();
    do {
        try {
            pagableFollowings = twitter.getFriendsList(18211861, cursor);
            for (User user : pagableFollowings) {
                listFriends.add(user);
                logger.info(ApplicationConstant.LogTag.ZUNA_INFO, "friend #1st level " + user.getName());
                /*PagableResponseList<User> _2ndLevelPageableFollowings = twitter.getFriendsList(user.getId(), cursor);
                for(User _2ndLevelFriend : _2ndLevelPageableFollowings){
                logger.info(ApplicationConstant.LogTag.ZUNA_INFO, "added friend #2nd level "+ _2ndLevelFriend.getName());
                listFriends.add(_2ndLevelFriend);
                }*/
            }
        } catch (TwitterException e) {
            e.printStackTrace();
        }

    } while ((cursor = pagableFollowings.getNextCursor()) != 0);

    logger.info(ApplicationConstant.LogTag.ZUNA_INFO, "friend total : " + listFriends.size());
    for (User user : listFriends) {
        logger.info(ApplicationConstant.LogTag.ZUNA_INFO, "friend : " + user.getName());
    }

    /*
            cursor = -1;
            PagableResponseList<User> pagableFollowers = null;
            List<User> listFollowers = new ArrayList<>();
            do {
    try {
        pagableFollowers = twitter.getFollowersList(twitter.getId(), cursor);
        for (User user : pagableFollowers) {
            listFollowers.add(user); // ArrayList<User>
            System.out.println(user.getName());
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
            
            } while ((cursor = pagableFollowers.getNextCursor()) != 0);
    */

}

From source file:controllers.modules.CorpusModule.java

License:Open Source License

public static Result update(UUID corpus) {
    OpinionCorpus corpusObj = null;//from ww w.ja va2s  .  c o m
    if (corpus != null) {
        corpusObj = fetchResource(corpus, OpinionCorpus.class);
    }
    OpinionCorpusFactory corpusFactory = null;

    MultipartFormData formData = request().body().asMultipartFormData();
    if (formData != null) {
        // if we have a multi-part form with a file.
        if (formData.getFiles() != null) {
            // get either the file named "file" or the first one.
            FilePart filePart = ObjectUtils.defaultIfNull(formData.getFile("file"),
                    Iterables.getFirst(formData.getFiles(), null));
            if (filePart != null) {
                corpusFactory = (OpinionCorpusFactory) new OpinionCorpusFactory().setFile(filePart.getFile())
                        .setFormat(FilenameUtils.getExtension(filePart.getFilename()));
            }
        }
    } else {
        // otherwise try as a json body.
        JsonNode json = request().body().asJson();
        if (json != null) {
            OpinionCorpusFactoryModel optionsVM = Json.fromJson(json, OpinionCorpusFactoryModel.class);
            if (optionsVM != null) {
                corpusFactory = optionsVM.toFactory();
            } else {
                throw new IllegalArgumentException();
            }

            if (optionsVM.grabbers != null) {
                if (optionsVM.grabbers.twitter != null) {
                    if (StringUtils.isNotBlank(optionsVM.grabbers.twitter.query)) {
                        TwitterFactory tFactory = new TwitterFactory();
                        Twitter twitter = tFactory.getInstance();
                        twitter.setOAuthConsumer(
                                Play.application().configuration().getString("twitter4j.oauth.consumerKey"),
                                Play.application().configuration().getString("twitter4j.oauth.consumerSecret"));
                        twitter.setOAuthAccessToken(new AccessToken(
                                Play.application().configuration().getString("twitter4j.oauth.accessToken"),
                                Play.application().configuration()
                                        .getString("twitter4j.oauth.accessTokenSecret")));

                        Query query = new Query(optionsVM.grabbers.twitter.query);
                        query.count(ObjectUtils.defaultIfNull(optionsVM.grabbers.twitter.limit, 10));
                        query.resultType(Query.RECENT);
                        if (StringUtils.isNotEmpty(corpusFactory.getLanguage())) {
                            query.lang(corpusFactory.getLanguage());
                        } else if (corpusObj != null) {
                            query.lang(corpusObj.getLanguage());
                        }

                        QueryResult qr;
                        try {
                            qr = twitter.search(query);
                        } catch (TwitterException e) {
                            throw new IllegalArgumentException();
                        }

                        StringBuilder tweets = new StringBuilder();
                        for (twitter4j.Status status : qr.getTweets()) {
                            // quote for csv, normalize space, and remove higher unicode characters. 
                            String text = StringEscapeUtils.escapeCsv(StringUtils
                                    .normalizeSpace(status.getText().replaceAll("[^\\u0000-\uFFFF]", "")));
                            tweets.append(text + System.lineSeparator());
                        }

                        corpusFactory.setContent(tweets.toString());
                        corpusFactory.setFormat("txt");
                    }
                }
            }
        } else {
            // if not json, then just create empty.
            corpusFactory = new OpinionCorpusFactory();
        }
    }

    if (corpusFactory == null) {
        throw new IllegalArgumentException();
    }

    if (corpus == null && StringUtils.isEmpty(corpusFactory.getTitle())) {
        corpusFactory.setTitle("Untitled corpus");
    }

    corpusFactory.setOwnerId(SessionedAction.getUsername(ctx())).setExistingId(corpus).setEm(em());

    DocumentCorpusModel corpusVM = null;
    corpusObj = corpusFactory.create();
    if (!em().contains(corpusObj)) {
        em().persist(corpusObj);

        corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
        corpusVM.populateSize(em(), corpusObj);
        return created(corpusVM.asJson());
    }

    for (PersistentObject obj : corpusObj.getDocuments()) {
        if (em().contains(obj)) {
            em().merge(obj);
        } else {
            em().persist(obj);
        }
    }
    em().merge(corpusObj);

    corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
    corpusVM.populateSize(em(), corpusObj);
    return ok(corpusVM.asJson());
}

From source file:crawling.GetUserTimelineByFile.java

License:Apache License

public static void main(String[] args) {

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.GetUserTimelineByFile UserFile");
        System.exit(-1);//w ww  . j a va 2  s.com
    }
    userFile = args[0];

    try {
        FileWriter outFileWhole = new FileWriter("CrawlTweets" + ".txt", true);
        out = new PrintWriter(outFileWhole);

        // out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File inFile = new File(userFile);

    if (!inFile.isFile()) {
        System.out.println("Parameter is not an existing file");
        return;
    }

    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(userFile));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    int count = 0;
    int totalCount = 0;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
    Date start = null;
    try {
        start = sdf.parse("2000, 1, 1");
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    List<Status> statuses = null;
    // String user = "Porter_Anderson";
    // user = "pqtad";
    // user = "paradunaa6";
    // user = "palifarous";

    ArrayList<Long> users = new ArrayList<Long>();

    String line = null;

    //Read from the original file and write to the new
    //unless content matches data to be removed.
    try {
        while ((line = br.readLine()) != null)
            users.add(Long.parseLong(line));
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //users.add(584928891L);
    //users.add(700425265L);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("Start at: " + dateFormat.format(date));

    Long sinceID =
            //218903304682471424L; // 2012/06/29
            238893053518151680L; // 2012/08/24
    Long maxID = 250409135600975873L; // 2012/09/24
    //238893053518151680L;   // 2012/08/24
    // 227659323180974080L;   // 2012/07/24

    boolean overflow = false;
    for (Long usr : users) {
        System.out.println("%" + usr);
        out.println("%" + usr);
        out.println("%" + usr);
        Paging paging = null;
        count = 0;
        for (int i = 1; i < 21; i++) {
            //paging = new Paging(i, 200);
            paging = new Paging(i, 200, sinceID, maxID);

            overflow = false;
            try {
                // statuses = twitter.getUserTimeline(user, paging);
                statuses = twitter.getUserTimeline(usr, paging);
                Thread.currentThread();
                try {
                    Thread.sleep(10500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to get timeline: " + te.getMessage());
                //System.exit(-1);
            }
            if (statuses.isEmpty()) {
                if (i > 16)
                    overflow = true;
                break;
            }
            for (Status status : statuses) {
                String record = "";
                record += status.getId();
                record += "::" + status.getInReplyToStatusId();
                record += "::" + status.getInReplyToUserId();
                record += "::" + status.getRetweetCount();
                if (status.getRetweetedStatus() != null)
                    record += "::" + status.getRetweetedStatus().getId();
                else
                    record += "::" + "-1";
                //record += "::" + status.isRetweet();
                int len = status.getUserMentionEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        UserMentionEntity ent = status.getUserMentionEntities()[l];
                        record += "," + ent.getId();
                    }
                } else
                    record += "::" + "-1";
                len = status.getURLEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        URLEntity ent = status.getURLEntities()[l];
                        record += "," + ent.getURL() + "|"
                        //+ ent.getDisplayURL() + "|"
                                + ent.getExpandedURL();
                    }
                } else
                    record += "::" + "-1";
                record += "::" + cleanText(status.getText());
                record += "::" +
                // status.getCreatedAt();
                        (status.getCreatedAt().getTime() - start.getTime()) / 1000;

                record += "::" + getSource(status.getSource());
                //System.out.println(record);
                out.println(record);
            }

            count += statuses.size();
            out.flush();
        }
        totalCount += count;
        out.println("%" + usr + ", " + count + ", " + overflow);
        System.out.println("%" + usr + ", " + count + ", " + overflow);
        out.println("------------------------------------------");
    }
    System.out.println("Total status count is " + totalCount);
    out.println("#" + totalCount);
    out.close();
    date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("End at: " + dateFormat.format(date));
}

From source file:crawling.GetUserTimelineByFileDiff.java

License:Apache License

public static void main(String[] args) {

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.GetUserTimelineByFile UserFile");
        System.exit(-1);/*from   w  w  w  .  ja  v a  2  s.  c om*/
    }
    userFile = args[0];

    try {
        FileWriter outFileWhole = new FileWriter("CrawlTweets" + ".txt", true);
        outWhole = new PrintWriter(outFileWhole);

        FileWriter outFileInter = new FileWriter("CrawlInter" + ".txt", true);
        outInter = new PrintWriter(outFileInter);
        // out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File inFile = new File(userFile);

    if (!inFile.isFile()) {
        System.out.println("Parameter is not an existing file");
        return;
    }

    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(userFile));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    int count = 0;
    int countInter = 0;
    int totalCount = 0;
    int totalCountInter = 0;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
    Date start = null;
    try {
        start = sdf.parse("2000, 1, 1");
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    List<Status> statuses = null;
    // String user = "Porter_Anderson";
    // user = "pqtad";
    // user = "paradunaa6";
    // user = "palifarous";
    // statuses = twitter.getUserTimeline(user);
    //String usersS = "490495670,210462629,731570426,174646182,381163846,308812526,595154838,47356666,590795627,466854690,560386951,406747522,767819150,193105498,725874775,381257304,486727765,39469575,68239634,187739283,27048882,172979072,16075589,618047540,841958365,707631710,76274554,334451278,573978436,782711790,498017142,764236789,88185343,293950091,726606902,221410697,566098451,32987449,73497385,223182159,571182929,769441794,16225570,404748449,249953519,713693096,727692930,178473099,366957548,255171527,411892240,370333418,753509292,765928471,49389074,422867487,185298515,112841821,524731824,132207254,431289858,35602356,747638244,76398937,276388822,221106768,608597994,532423674,509070257,235773945,263995811,61575477,48606725,518136068,450781132,631504150,842236698,840479664,306291201,59829276,541212815,264436593,262466303,302157661,135587748,399229428,265344815,225280446,540254332,218019401,260453139,187424025,177051847,351065900,406684867,293234971,219356963,280763734,59290083,214750688";

    //String[] splits = usersS.split(",");
    ArrayList<Long> users = new ArrayList<Long>();

    String line = null;

    //Read from the original file and write to the new
    //unless content matches data to be removed.
    try {
        while ((line = br.readLine()) != null) {

            users.add(Long.parseLong(line));
        }
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //users.add(584928891L);
    //users.add(700425265L);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("Start at: " + dateFormat.format(date));

    Long sinceID =
            //218903304682471424L; // 2012/06/29
            238893053518151680L; // 2012/08/24
    Long maxID = 250409135600975873L; // 2012/09/24
    //238893053518151680L;   // 2012/08/24
    // 227659323180974080L;   // 2012/07/24

    boolean overflow = false;
    for (Long usr : users) {
        System.out.println("%" + usr);
        outWhole.println("%" + usr);
        outInter.println("%" + usr);
        Paging paging = null;
        count = 0;
        countInter = 0;
        for (int i = 1; i < 21; i++) {
            //paging = new Paging(i, 200);
            paging = new Paging(i, 200, sinceID, maxID);

            overflow = false;
            try {
                // statuses = twitter.getUserTimeline(user, paging);
                statuses = twitter.getUserTimeline(usr, paging);
                Thread.currentThread();
                try {
                    Thread.sleep(10500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to get timeline: " + te.getMessage());
                //System.exit(-1);
            }
            if (statuses.isEmpty()) {
                if (i > 16)
                    overflow = true;
                break;
            }

            for (Status status : statuses) {
                boolean inter = false;
                //countRT += status.getRetweetCount();
                String record = "";
                String recordInter = "";
                record += status.getId();

                // For reply
                Long replyId = status.getInReplyToStatusId();
                if (replyId == -1)
                    record += "::-1::-1";
                else {
                    record += "::" + replyId;
                    record += "::" + status.getInReplyToUserId();
                    inter = true;
                }

                // For retweeting
                Long retweets = (long) status.getRetweetCount();
                record += "::" + retweets;
                if (retweets > 0) {
                    inter = true;

                    if (status.getRetweetedStatus() != null)
                        record += "::" + status.getRetweetedStatus().getId();
                    else
                        record += "::" + "-1"; // The source of retweeting
                }
                //record += "::" + status.isRetweet();
                // For mentions
                int len = status.getUserMentionEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        UserMentionEntity ent = status.getUserMentionEntities()[l];
                        record += "," + ent.getId();
                    }
                    inter = true;
                } else
                    record += "::" + "-1";

                recordInter += record;

                // For URL
                len = status.getURLEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        URLEntity ent = status.getURLEntities()[l];
                        record += "," + ent.getURL() + "|"
                        //+ ent.getDisplayURL() + "|"
                                + ent.getExpandedURL();
                    }
                } else
                    record += "::" + "-1";

                // For text
                record += "::" + cleanText(status.getText());

                // For creating time
                // record += "::" + status.getCreatedAt().toString();
                Long seconds =
                        // status.getCreatedAt();
                        (status.getCreatedAt().getTime() - start.getTime()) / 1000;
                record += "::" + seconds;
                recordInter += "::" + seconds;

                // For publishing source
                record += "::" + getSource(status.getSource());
                //System.out.println(record);
                outWhole.println(record);

                if (inter) {
                    outInter.println(recordInter);
                    countInter++;
                }
            }

            count += statuses.size();
            outWhole.flush();
            outInter.flush();
        }
        totalCount += count;
        totalCountInter += countInter;
        outWhole.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        outInter.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        System.out.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        outWhole.println("------------------------------------------");
        outInter.println("------------------------------------------");
    }
    System.out.println("Total status count is " + totalCount + "," + totalCountInter);
    outWhole.println("#" + totalCount + "," + totalCountInter);
    outInter.println("#" + totalCount + "," + totalCountInter);
    outWhole.close();
    outInter.close();
    date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("End at: " + dateFormat.format(date));
}