【问题标题】:Twitter4j Search API , tweets based on location and time IntervalTwitter4j 搜索 API,基于位置和时间间隔的推文
【发布时间】:2015-01-23 05:09:04
【问题描述】:

我有一个具体要求,我想根据以下参数收集所有推文

1) 我正在使用搜索 API,例如我想搜索“Iphone6”

2) 区域明智,即如果我根据城市指定纬度和经度,我得到结果,是否可以在国家/地区明智地获取所有结果(如我指定印度的纬度和经度时的代码中所示 它不起作用!)

3) 我应该以什么时间间隔运行我的应用程序,这样我才能获得新更新的推文,而不会获得以前获取的推文。

This is the code that I have written 


  public final class twitterdate {


    public static void main(String[] args)throws Exception {

        double res;
        double lat,lon;

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("MyKEY")
                .setOAuthConsumerSecret("MySecret")
                .setOAuthAccessToken("MyAccesstoken")

                .setOAuthAccessTokenSecret("MyTokenSecret").setHttpConnectionTimeout(100000);




        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        lat=18.9750;  // THis works , but it doenst work when I specify latitude and longitude of India
        lon=72.8258;
        res=1;

        try {





           QueryResult result=twitter.search(new Query("iphone").since("2014-11-19").until("2014-11-22").geoCode(new GeoLocation(lat, lon), res,"1mi"));
           // Since and untill doesnt work as expected sometimes it fetches the date tweets specified on "since" method sometimes fetches the tweets specified on the date of until method
           // Also since and until doesnt work when i specify a time stamp.
           List<Status> qrTweets = result.getTweets();
              System.out.println("hi");

            for (Status tweet : qrTweets )  
            {   
                 System.out.println( tweet.getId() + " " + "@" + tweet.getUser().getScreenName()  + " : " + tweet.getText() + " :::" + tweet.getCreatedAt() );  
            }

        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

如果有人可以帮助我满足我的要求,我会非常感激,因为我已经用谷歌搜索了很多但找不到任何合适的解决方案。 提前致谢!

【问题讨论】:

    标签: twitter twitter4j twitter-search


    【解决方案1】:

    您是否尝试过使用 FilterQuery?下面的代码 sn-p 将为您提供连续的推文流。 据我了解,您想从印度获取与 iphone6 相关的推文。 使用搜索查询,您最终可能会一遍又一遍地获得相同的推文集。

    您可以在下面尝试类似的操作,我不确定您使用什么坐标从印度获取推文!您必须进行一些试验和错误并微调您想要定位推文的坐标周围。

        StatusListener listener = new StatusListener(){
            public void onStatus(Status status) {
                //if (status.getText().contains)
                if(status.getUser().getLang().equalsIgnoreCase("en")
                        || status.getUser().getLang().equalsIgnoreCase("en_US")) {
                    System.out.println(status.getUser().getName() + " :: " + status.getText() + " :: " + status.getGeoLocation());
                }
            }
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
            public void onScrubGeo(long arg0, long arg1) {
    
            }
            public void onStallWarning(StallWarning arg0) {
    
            }
        };
    
        ConfigurationBuilder config = new ConfigurationBuilder();
        config.setOAuthConsumerKey("");
        config.setOAuthConsumerSecret("");
        config.setOAuthAccessToken("");
        config.setOAuthAccessTokenSecret("");
    
        TwitterStream twitterStream = new TwitterStreamFactory(config.build()).getInstance();
        twitterStream.addListener(listener);
        FilterQuery query = new FilterQuery();
        // New Delhi India
        double lat = 28.6;
        double lon = 77.2;
    
        double lon1 = lon - .5;
        double lon2 = lon + .5;
        double lat1 = lat - .5;
        double lat2 = lat + .5;
    
        double box[][] = {{lon1, lat1}, {lon2, lat2}};
    
        query.locations(box);
    
        String[] trackArray = {"iphone"}; 
        query.track(trackArray);
        twitterStream.filter(query);
    

    但是,FilterQuery 有一个警告,它使用 location 或 trackList 来获取数据。为了解决这个问题,您可以在 onStatus() 方法中添加内容过滤器逻辑。

    希望对你有所帮助。

    【讨论】:

    • 您好 mbaxi,我设法更正了项目设置,但是在运行应用程序时我收到此错误 -“线程“main”中的异常 java.lang.NoSuchMethodError: twitter4j.conf.Configuration.getRequestHeaders( )Ljava/util/Map;"有什么线索吗?
    • 嗨,似乎这些罐子存在一些兼容性问题,所以我使用了以下罐子 twitter4j-stream-3.0.5.jar 和 twitter4j-core-3.0.5.jar 并且它有效:) Thx又是你的救生员:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多