DevoxxFR 2024 Reproducible Builds with Apache Maven
Development of Twitter Application #6 - Trends
1. Linked Data &
Semantic Web
Technology
Development of
Twitter Applications
Part 6. Trends
Dr. Myungjin Lee
2. Linked Data & Semantic Web Technology
Trends API
• Trends REST API
– to explore what's trending on Twitter
– Trends are determined by an algorithm and are
tailored for you based on who you follow and your
location.
2
3. Linked Data & Semantic Web Technology
REST API related to Trends
3
Resource Description
GET
trends/place
Returns the top 10 trending topics for a specific WOEID, if
trending information is available for it. The response is an array
of "trend" objects that encode the name of the trending topic, the
query parameter that can be used to search for the topic on
Twitter Search, and the Twitter Search URL....
GET
trends/available
Returns the locations that Twitter has trending topic information
for. The response is an array of "locations" that encode the
location's WOEID and some other human-readable information
such as a canonical name and country the location belongs in. A
WOEID is a Yahoo! Where On Earth ID.
GET
trends/closest
Returns the locations that Twitter has trending topic information
for, closest to a specified location. The response is an array of
"locations" that encode the location's WOEID and some other
human-readable information such as a canonical name and
country the location belongs in. A WOEID is a Yahoo...
4. Linked Data & Semantic Web Technology
GET trends/available
• Resource URL
– https://api.twitter.com/1.1/trends/available.json
• Other Information
– Requests per rate limit window: 15/user, 15/app
– Authentication: Required
– Response
• The response is an array of "locations" that encode the
location's WOEID and some other human-readable
information.
• A WOEID is a Yahoo! Where On Earth ID.
– API Version: v1.1
4
6. Linked Data & Semantic Web Technology
Getting Locations for Available Trends
1. import twitter4j.Location;
2. import twitter4j.ResponseList;
3. import twitter4j.Twitter;
4. import twitter4j.TwitterException;
5. import twitter4j.TwitterFactory;
6. public class TwitterTrends {
7. Twitter twitter = null;
8. public TwitterTrends() {
9. this.twitter = TwitterFactory.getSingleton();
10. this.twitter.setOAuthConsumer(TwitterAccessToken.consumerKey,
11. TwitterAccessToken.consumerSecret);
12. this.twitter.setOAuthAccessToken(TwitterAccessToken.loadAccessToken());
13. }
14. public static void main(String args[]) throws TwitterException {
15. TwitterTrends tt = new TwitterTrends();
16. ResponseList<Location> locations = tt.twitter.getAvailableTrends();
17. for (int i = 0; i < locations.size(); i++) {
18. Location location = locations.get(i);
19. System.out.println(location.getName() + ", "
20. + location.getCountryName() + ": " + location.getWoeid());
21. }
22. }
23. }
6
7. Linked Data & Semantic Web Technology
GET trends/place
• Resource URL
– https://api.twitter.com/1.1/trends/place.json
• Parameters
• Other Information
– Requests per rate limit window: 15/user, 15/app
– Authentication: Required
– API Version: v1.1
id
required
The Yahoo! Where On Earth ID of the location to return trending
information for. Global information is available by using 1 as the WOEID.
exclude
optional
Setting this equal to hashtags will remove all hashtags from the trends list.
7
8. Linked Data & Semantic Web Technology
Getting Trends
1. public static void main(String args[]) throws TwitterException {
2. TwitterTrends tt = new TwitterTrends();
3. Trends trends = tt.twitter.getPlaceTrends(1);
4. Trend[] trend = trends.getTrends();
5. for(int i = 0; i < trend.length; i++) {
6. System.out.println(i + ". " + trend[i].getName());
7. }
8. }
8