创建一个新对象

时间:2017-03-09 15:19:12

标签: java swing object twitter twitter4j

目前,我的程序有一个带有大约15种不同输入的Jpanel。这些输入被创建为一个名为" searchInput"。

的长字符串

" searchInput"然后将其放入一个名为的查询中,然后逐个获取有关推文结果的元数据,例如createdAt,user和text。这些结果将打印到名为tweetsResult的textArea。

createdAt,用户和文本只是每条推文背后的部分元数据,更多的是favouriteCount和retweetCount。

因为Twitter有最大数量的推文它可以使用API​​传递给你,我觉得最好是创建一个使用searchInput的新类,然后获得所需数量的Tweets所有元数据到一个东西。然后从主类中,我只会使用我想要的特定元数据调用结果。

这是正确的方法吗?如果是这样,我怎么能创建这个新对象,大概是使用getter和setter?

我希望你们清楚这一点,非常感谢你的帮助!

    List<Status> tweets = new ArrayList<Status>();

    //Standard Twitter and Twitter4j authentication
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(consumerKey);
    builder.setOAuthConsumerSecret(consumerSecret);
    Configuration configuration = builder.build();
    TwitterFactory twitterFactory = new TwitterFactory(configuration);
    Twitter twitter = twitterFactory.getInstance();
    twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));




    //The query is split up into 5 sections
    //All the text boxes are read in the seperate methods
    String searchWordInput = getWordSearchQuery();
    String searchPeopleInput = getPeopleSearchQuery();
    String filtersSearchInput = getFiltersSearchQuery();
    String dateAndLocationSearchInput = getDateAndLocationSearchQuery();
    String radiusAndLocationSearchInput = getRadiusAndLocationSearchQuery();
    String searchInput = searchWordInput + searchPeopleInput + filtersSearchInput + dateAndLocationSearchInput + radiusAndLocationSearchInput;
    //One big search String called "searchInput" is taken to be sent to Twitter
    //Increases maximum amount of tweets in one search
    int wantedTweets = 100; //CHANGE THIS FOR DIFFERENT AMOUNT OF TWEETS
    long lastSearchID = Long.MAX_VALUE;
    int remainingTweets = wantedTweets;
    Query query = new Query(searchInput);
    tweetsResultInput.append(searchInput);
    try {
        while (remainingTweets > 0) {
            remainingTweets = wantedTweets - tweets.size();
            if (remainingTweets > 100) {
                query.count(100);
            } else {
                query.count(remainingTweets);
            }
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());
            Status s = tweets.get(tweets.size() - 1);
            lastSearchID = s.getId();
            query.setMaxId(lastSearchID);
            remainingTweets = wantedTweets - tweets.size();
        }
        //Increases maximum amount of tweets in ones search


        tweetsResult.append(searchInput);
        tweetsResult.append("\n");
        //For the query tweets, using "searchInput", get every tweet in the format; when Tweet was sent, who it was sent by, and the Tweets text and write to tweetsResults
        for (Status tweet : tweets) {
            tweetsResult.append(tweet.getCreatedAt() + ":\t@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            tweetsResult.append("\n");

        }
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }

0 个答案:

没有答案