繁体   English   中英

Twitter4j Search API,基于位置和时间间隔的推文

[英]Twitter4j Search API , tweets based on location and time Interval

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

1)即时通讯使用搜索API,例如我要搜索“ Iphone6”

2)明智的地区,即如果我指定每个城市的纬度和经度,我会得到结果,是否有可能在国家/地区获取所有结果(如代码中我推测印度的纬度和经度不起作用!)

3)我应该在什么时间间隔运行我的应用程序,以便获得新更新的tweets,而不获取以前获取的tweets。

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();
        }

    }


}

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

您是否尝试过使用FilterQuery? 下面的代码段将为您提供连续的鸣叫。 根据我的理解,您想从印度获取有关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有一个警告,它使用位置或trackList来获取数据。 为了解决这个问题,您可以在onStatus()方法中放置内容过滤器逻辑。

希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM