44import org .slf4j .LoggerFactory ;
55import org .springframework .beans .factory .annotation .Autowired ;
66import org .springframework .core .env .Environment ;
7+ import org .springframework .social .RateLimitExceededException ;
78import org .springframework .social .ResourceNotFoundException ;
89import org .springframework .social .twitter .api .CursoredList ;
910import org .springframework .social .twitter .api .Tweet ;
2122/**
2223 * Created by tw on 19.06.17.
2324 */
24-
2525@ Component
2626public class TwitterApiServiceImpl implements TwitterApiService {
2727
28-
2928 @ Override
3029 public List <Tweet > findTweetsForSearchQuery () {
3130 String msg = MSG +"findTweetsForSearchQuery: " ;
@@ -41,6 +40,9 @@ public List<Tweet> findTweetsForSearchQuery() {
4140 log .debug (msg +" result.size: " +fetchedTweets .size ());
4241 return fetchedTweets ;
4342 }
43+ } catch (RateLimitExceededException e ){
44+ log .warn (msg +" Rate Limit Exceeded : " );
45+ return null ;
4446 } catch (Exception e ) {
4547 log .error (msg + e .getMessage ());
4648 return new ArrayList <>();
@@ -59,6 +61,9 @@ public Tweet findOneTweetById(long tweetTwitterId) {
5961 msg += " result: " ;
6062 log .debug (msg + result );
6163 return result ;
64+ } catch (RateLimitExceededException e ) {
65+ log .warn (msg + " Rate Limit Exceeded " );
66+ return null ;
6267 } catch (Exception e ){
6368 log .error (msg + e .getMessage ());
6469 e .printStackTrace ();
@@ -78,6 +83,12 @@ public TwitterProfile getUserProfileForTwitterId(long userProfileTwitterId) {
7883 log .debug (msg + " ScreenName: " + result .getScreenName ());
7984 log .debug (msg + " Name: " + result .getName ());
8085 return result ;
86+ } catch (RateLimitExceededException e ) {
87+ log .warn (msg + " Rate Limit Exceeded : " );
88+ return null ;
89+ } catch (ResourceNotFoundException e ) {
90+ log .warn (msg +" User not found : " +userProfileTwitterId );
91+ return null ;
8192 } catch (Exception e ) {
8293 log .error (msg + e .getMessage ());
8394 return null ;
@@ -90,16 +101,19 @@ public TwitterProfile getUserProfileForScreenName(String screenName) {
90101 log .debug (msg );
91102 TwitterProfile result ;
92103 try {
93- result = getTwitterProxy ().userOperations ().getUserProfile (screenName );
104+ result = getTwitterProxy ().userOperations ().getUserProfile (screenName );
94105 msg += " result: " ;
95- log .debug (msg + " ScreenName: " + result .getScreenName ());
96- log .debug (msg + " Name: " + result .getName ());
106+ log .debug (msg + " ScreenName: " + result .getScreenName ());
107+ log .debug (msg + " Name: " + result .getName ());
97108 return result ;
109+ } catch (RateLimitExceededException e ){
110+ log .warn (msg +" Rate Limit Exceeded : " );
111+ return null ;
98112 } catch (ResourceNotFoundException e ) {
99- log .debug (msg +" User not found : " +screenName );
113+ log .warn (msg +" User not found : " +screenName );
100114 return null ;
101115 } catch (Exception e ) {
102- log .debug (msg + e .getMessage ());
116+ log .error (msg + e .getMessage ());
103117 return null ;
104118 }
105119 }
@@ -114,28 +128,28 @@ public List<TwitterProfile> findUsersFromDefinedList(String screenName,String fe
114128 log .debug (msg + " result.size: " + result .size ());
115129 return result ;
116130 } catch (Exception e ) {
117- log .debug (msg + e .getMessage ());
131+ log .error (msg + e .getMessage ());
118132 return new ArrayList <>();
119133 }
120134 }
121135
122136 @ Override
123- public CursoredList <Long > findFollower () {
124- String msg = MSG +"findFollower : " ;
137+ public CursoredList <Long > getFollowerIds () {
138+ String msg = MSG +"getFollowerIds : " ;
125139 log .debug (msg );
126140 CursoredList <Long > result ;
127141 try {
128142 result = getTwitterProxy ().friendOperations ().getFollowerIds ();
129143 log .debug (msg + " result.size: " + result .size ());
130144 return result ;
131145 } catch (Exception e ) {
132- log .debug (msg + e .getMessage ());
146+ log .error (msg + e .getMessage ());
133147 return new CursoredList <>(new ArrayList <>(),0L ,0L );
134148 }
135149 }
136150
137151 @ Override
138- public CursoredList <Long > findFriends () {
152+ public CursoredList <Long > getFriendIds () {
139153 String msg = MSG +"findFriendss: " ;
140154 log .debug (msg );
141155 CursoredList <Long > result ;
@@ -144,12 +158,11 @@ public CursoredList<Long> findFriends() {
144158 log .debug (msg + " result.size: " + result .size ());
145159 return result ;
146160 } catch (Exception e ) {
147- log .debug (msg + e .getMessage ());
161+ log .error (msg + e .getMessage ());
148162 return new CursoredList <>(new ArrayList <>(),0L ,0L );
149163 }
150164 }
151165
152-
153166 @ Inject
154167 private Environment environment ;
155168
@@ -158,12 +171,10 @@ public CursoredList<Long> findFriends() {
158171 private final TwitterProperties twitterProperties ;
159172
160173 private Twitter getTwitterProxy () {
161-
162174 String consumerKey = environment .getProperty ("TWITTER_CONSUMER_KEY" );
163175 String consumerSecret = environment .getProperty ("TWITTER_CONSUMER_SECRET" );
164176 String accessToken = environment .getProperty ("TWITTER_ACCESS_TOKEN" );
165177 String accessTokenSecret = environment .getProperty ("TWITTER_ACCESS_TOKEN_SECRET" );
166-
167178 Twitter twitterTemplate = new TwitterTemplate (consumerKey , consumerSecret , accessToken , accessTokenSecret );
168179 return twitterTemplate ;
169180 }
0 commit comments