Skip to content

Commit

Permalink
TFJ-687 implement GET trends/closest : getClosestTrends(GeoLocation)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke committed Nov 28, 2012
1 parent e53303f commit a61235f
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 1 deletion.
27 changes: 27 additions & 0 deletions twitter4j-async/src/main/java/twitter4j/AsyncTwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,33 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
});
}

/**
* {@inheritDoc}
*/
@Override
public void getAvailableTrends(GeoLocation location) {
getClosestTrends(location);
}

/**
* {@inheritDoc}
*/
@Override
public void getClosestTrends(final GeoLocation location) {
getDispatcher().invokeLater(new AsyncTask(CLOSEST_TRENDS, listeners) {
@Override
public void invoke(List<TwitterListener> listeners) throws TwitterException {
ResponseList<Location> locations = twitter.getClosestTrends(location);
for (TwitterListener listener : listeners) {
try {
listener.gotClosestTrends(locations);
} catch (Exception ignore) {
}
}
}
});
}

/* Spam Reporting Resources */
/**
* {@inheritDoc}
Expand Down
88 changes: 88 additions & 0 deletions twitter4j-async/src/main/java/twitter4j/TwitterAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,121 +32,209 @@
*/
public class TwitterAdapter implements TwitterListener {
/* Timelines Resources */
@Override
public void gotMentions(ResponseList<Status> statuses){}
@Override
public void gotHomeTimeline(ResponseList<Status> statuses){}
@Override
public void gotUserTimeline(ResponseList<Status> statuses){}
@Override
public void gotRetweetsOfMe(ResponseList<Status> statuses){}

/* Tweets Resources */
@Override
public void gotRetweets(ResponseList<Status> retweets){}
@Override
public void gotShowStatus(Status status){}
@Override
public void destroyedStatus(Status destroyedStatus){}
@Override
public void updatedStatus(Status status){}
@Override
public void retweetedStatus(Status retweetedStatus){}

/* Search Resources */
@Override
public void searched(QueryResult queryResult){}

/* Direct Messages Resources */
@Override
public void gotDirectMessages(ResponseList<DirectMessage> messages){}
@Override
public void gotSentDirectMessages(ResponseList<DirectMessage> messages){}
@Override
public void gotDirectMessage(DirectMessage message){}
@Override
public void destroyedDirectMessage(DirectMessage message){}
@Override
public void sentDirectMessage(DirectMessage message){}

/* Friends & Followers Resources */
@Override
public void gotFriendsIDs(IDs ids){}
@Override
public void gotFollowersIDs(IDs ids){}
@Override
public void lookedUpFriendships(ResponseList<Friendship> friendships){}
@Override
public void gotIncomingFriendships(IDs ids){}
@Override
public void gotOutgoingFriendships(IDs ids){}
@Override
public void createdFriendship(User user){}
@Override
public void destroyedFriendship(User user){}
@Override
public void updatedFriendship(Relationship relationship){}
@Override
public void gotShowFriendship(Relationship relationship){}

/* Users Resources */
@Override
public void gotAccountSettings(AccountSettings settings){}
@Override
public void verifiedCredentials(User user){}
@Override
public void updatedAccountSettings(AccountSettings settings){}
// updatedDeliveryDevice
@Override
public void updatedProfile(User user){}
@Override
public void updatedProfileBackgroundImage(User user){}
@Override
public void updatedProfileColors(User user){}
@Override
public void updatedProfileImage(User user){}
@Override
public void gotBlocksList(ResponseList<User> blockingUsers){}
@Override
public void gotBlockIDs(IDs blockingUsersIDs){}
@Override
public void createdBlock(User user){}
@Override
public void destroyedBlock(User user){}
@Override
public void lookedupUsers(ResponseList<User> users){}
@Override
public void gotUserDetail(User user){}
@Override
public void searchedUser(ResponseList<User> userList){}
@Override
public void gotContributees(ResponseList<User> users){}
@Override
public void gotContributors(ResponseList<User> users){}
@Override
public void removedProfileBanner() {}
@Override
public void updatedProfileBanner() {}

/* Suggested Users Resources */
@Override
public void gotUserSuggestions(ResponseList<User> users){}
@Override
public void gotSuggestedUserCategories(ResponseList<Category> category){}
@Override
public void gotMemberSuggestions(ResponseList<User> users){}

/* Favorites Resources */
@Override
public void gotFavorites(ResponseList<Status> statuses){}
@Override
public void createdFavorite(Status status){}
@Override
public void destroyedFavorite(Status status){}

/* Lists Resources */
@Override
public void gotUserLists(ResponseList<UserList> userLists){}
@Override
public void gotUserListStatuses(ResponseList<Status> statuses){}
@Override
public void destroyedUserListMember(UserList userList){}
@Override
public void gotUserListMemberships(PagableResponseList<UserList> userLists){}
@Override
public void gotUserListSubscribers(PagableResponseList<User> users){}
@Override
public void subscribedUserList(UserList userList){}
@Override
public void checkedUserListSubscription(User user){}
@Override
public void unsubscribedUserList(UserList userList){}
@Override
public void createdUserListMembers(UserList userList){}
@Override
public void checkedUserListMembership(User users){}
@Override
public void createdUserListMember(UserList userList){}
@Override
public void destroyedUserList(UserList userList){}
@Override
public void updatedUserList(UserList userList){}
@Override
public void createdUserList(UserList userList){}
@Override
public void gotShowUserList(UserList userList){}
@Override
public void gotUserListSubscriptions(PagableResponseList<UserList> userLists){}
@Override
public void gotUserListMembers(PagableResponseList<User> users){}

/* Saved Searches Resources */
@Override
public void gotSavedSearches(ResponseList<SavedSearch> savedSearches){}
@Override
public void gotSavedSearch(SavedSearch savedSearch){}
@Override
public void createdSavedSearch(SavedSearch savedSearch){}
@Override
public void destroyedSavedSearch(SavedSearch savedSearch){}

/* Places & Geo Resources */
@Override
public void gotGeoDetails(Place place){}
@Override
public void gotReverseGeoCode(ResponseList<Place> places){}
@Override
public void searchedPlaces(ResponseList<Place> places){}
@Override
public void gotSimilarPlaces(SimilarPlaces places){}
@Override
public void createdPlace(Place place){}

/* Trends Resources */
@Override
public void gotPlaceTrends(Trends trends) {}
@Override
public void gotAvailableTrends(ResponseList<Location> locations){}
@Override
public void gotClosestTrends(ResponseList<Location> locations) {}

/* Spam Reporting Resources */
@Override
public void reportedSpam(User reportedSpammer){}

/* OAuth Resources */
@Override
public void gotOAuthRequestToken(RequestToken token){}
@Override
public void gotOAuthAccessToken(AccessToken token){}

/* Help Resources */
@Override
public void gotAPIConfiguration(TwitterAPIConfiguration conf){}
@Override
public void gotLanguages(ResponseList<HelpResources.Language> languages){}
@Override
public void gotPrivacyPolicy(String privacyPolicy){}
@Override
public void gotTermsOfService(String tof){}
@Override
public void gotRateLimitStatus(Map<String, RateLimitStatus> rateLimitStatus){}
@Override
public void onException(TwitterException te, TwitterMethod method){}

/* Undocumented Resources */
@Override
public void gotRelatedResults(RelatedResults relatedResults){}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public interface TwitterListener {
/* Trends Resources */
void gotPlaceTrends(Trends trends);
void gotAvailableTrends(ResponseList<Location> locations);
void gotClosestTrends(ResponseList<Location> locations);

/* Spam Reporting Resources */
void reportedSpam(User reportedSpammer);
Expand Down
1 change: 1 addition & 0 deletions twitter4j-async/src/main/java/twitter4j/TwitterMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public enum TwitterMethod {
/* Trends Resources */
PLACE_TRENDS,
AVAILABLE_TRENDS,
CLOSEST_TRENDS,

/* Spam Reporting Resources */
REPORT_SPAM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package twitter4j.api;

import twitter4j.GeoLocation;
import twitter4j.TwitterException;

/**
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.1.3
Expand Down Expand Up @@ -55,4 +58,27 @@ public interface TrendsResourcesAsync {
* @since Twitter4J 2.1.1
*/
void getAvailableTrends();

/**
* Retrieves the sorted locations that Twitter has trending topic information for. The response is an array of &quot;locations&quot; that encode the location's WOEID (a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>) and some other human-readable information such as a canonical name and country the location belongs in.
* <br>This method calls http://api.twitter.com/1/trends/available.json
*
* @param location the available trend locations will be sorted by distance to the lat and long passed in. The sort is nearest to furthest.
* @see <a href="https://dev.twitter.com/docs/api/1/get/trends/available">GET trends/available | Twitter Developers</a>
* @since Twitter4J 2.1.1
* @deprecated use {@link #getClosestTrends(twitter4j.GeoLocation)} instead
*/
void getAvailableTrends(GeoLocation location);

/**
* Returns the locations that Twitter has trending topic information for, closest to a specified location.<br>
* 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.<br>
* A WOEID is a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>.
* <br>This method calls http://api.twitter.com/1.1/trends/closest.json
*
* @param location the available trend locations will be sorted by distance to the lat and long passed in. The sort is nearest to furthest.
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/closest">GET trends/closest | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
void getClosestTrends(GeoLocation location) throws TwitterException;
}
6 changes: 6 additions & 0 deletions twitter4j-async/src/test/java/twitter4j/AsyncTwitterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ public void gotAvailableTrends(ResponseList<Location> locations) {
notifyResponse();
}

@Override
public void gotClosestTrends(ResponseList<Location> locations) {
this.locations = locations;
notifyResponse();
}

/*Geo Methods*/
@Override
public void searchedPlaces(ResponseList<Place> places) {
Expand Down
20 changes: 20 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,26 @@ public ResponseList<Location> getAvailableTrends() throws TwitterException {
+ "trends/available.json"));
}

/**
* {@inheritDoc}
*/
@Override
public ResponseList<Location> getAvailableTrends(GeoLocation location) throws TwitterException {
return getClosestTrends(location);
}

/**
* {@inheritDoc}
*/
@Override
public ResponseList<Location> getClosestTrends(GeoLocation location) throws TwitterException {
return factory.createLocationList(get(conf.getRestBaseURL()
+ "trends/closest.json",
new HttpParameter[]{new HttpParameter("lat", location.getLatitude())
, new HttpParameter("long", location.getLongitude())
}));
}

/* Spam Reporting Resources */
/**
* {@inheritDoc}
Expand Down
27 changes: 27 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/api/TrendsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,31 @@ public interface TrendsResources {
* @since Twitter4J 2.1.1
*/
ResponseList<Location> getAvailableTrends() throws TwitterException;

/**
* Returns the sorted locations that Twitter has trending topic information for. The response is an array of &quot;locations&quot; that encode the location's WOEID (a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>) and some other human-readable information such as a canonical name and country the location belongs in.
* <br>This method calls http://api.twitter.com/1/trends/available.json
*
* @param location the available trend locations will be sorted by distance to the lat and long passed in. The sort is nearest to furthest.
* @return the locations
* @throws twitter4j.TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1/get/trends/available">GET trends/available | Twitter Developers</a>
* @since Twitter4J 2.1.1
* @deprecated use {@link #getClosestTrends(twitter4j.GeoLocation)} instead
*/
ResponseList<Location> getAvailableTrends(GeoLocation location) throws TwitterException;

/**
* Returns the locations that Twitter has trending topic information for, closest to a specified location.<br>
* 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.<br>
* A WOEID is a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>.
* <br>This method calls http://api.twitter.com/1.1/trends/closest.json
*
* @param location the available trend locations will be sorted by distance to the lat and long passed in. The sort is nearest to furthest.
* @return the locations
* @throws twitter4j.TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/closest">GET trends/closest | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
ResponseList<Location> getClosestTrends(GeoLocation location) throws TwitterException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ protected void tearDown() throws Exception {

public void testLocalTrendsMethods() throws Exception {
Trends trends = twitter2.getPlaceTrends(1);
assertEquals("Worldwide",trends.getLocation().getName());
assertEquals("Worldwide", trends.getLocation().getName());
ResponseList<Location> locations;
locations = twitter1.getAvailableTrends();
assertNotNull(DataObjectFactory.getRawJSON(locations));
assertEquals(locations.get(0), DataObjectFactory.createLocation(DataObjectFactory.getRawJSON(locations.get(0))));
assertTrue(locations.size() > 0);

locations = twitter2.getClosestTrends(new GeoLocation(35.677248D, 139.72911D));
assertEquals("Tokyo", locations.get(0).getName());
}
}

0 comments on commit a61235f

Please sign in to comment.