streams-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jfrazee <...@git.apache.org>
Subject [GitHub] incubator-streams pull request: STREAMS-235
Date Thu, 02 Apr 2015 19:09:25 GMT
Github user jfrazee commented on a diff in the pull request:

    https://github.com/apache/incubator-streams/pull/202#discussion_r27687475
  
    --- Diff: streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/provider/TwitterFollowersProviderTask.java
---
    @@ -0,0 +1,181 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +package org.apache.streams.twitter.provider;
    +
    +import com.fasterxml.jackson.core.JsonParseException;
    +import com.fasterxml.jackson.databind.JsonMappingException;
    +import com.fasterxml.jackson.databind.ObjectMapper;
    +import com.google.common.collect.Lists;
    +import org.apache.streams.core.StreamsDatum;
    +import org.apache.streams.jackson.StreamsJacksonMapper;
    +import org.apache.streams.twitter.pojo.Follow;
    +import org.apache.streams.twitter.pojo.User;
    +import org.apache.streams.util.ComponentUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import twitter4j.PagableResponseList;
    +import twitter4j.Twitter;
    +import twitter4j.TwitterException;
    +import twitter4j.TwitterObjectFactory;
    +
    +import java.io.IOException;
    +
    +/**
    + *  Retrieve recent posts for a single user id.
    + */
    +public class TwitterFollowersProviderTask implements Runnable {
    +
    +    private final static Logger LOGGER = LoggerFactory.getLogger(TwitterFollowersProviderTask.class);
    +
    +    private final static ObjectMapper mapper = StreamsJacksonMapper.getInstance();
    +
    +    protected TwitterFollowingProvider provider;
    +    protected Twitter client;
    +    protected Long id;
    +    protected String screenName;
    +
    +    public TwitterFollowersProviderTask(TwitterFollowingProvider provider, Twitter twitter,
Long id) {
    +        this.provider = provider;
    +        this.client = twitter;
    +        this.id = id;
    +    }
    +
    +    public TwitterFollowersProviderTask(TwitterFollowingProvider provider, Twitter twitter,
String screenName) {
    +        this.provider = provider;
    +        this.client = twitter;
    +        this.screenName = screenName;
    +    }
    +
    +
    +    @Override
    +    public void run() {
    +
    +        if( id != null )
    +            getFollowers(id);
    +        if( screenName != null)
    +            getFollowers(screenName);
    +
    +        LOGGER.info(id != null ? id.toString() : screenName + " Thread Finished");
    +
    +    }
    +
    +    protected void getFollowers(Long id) {
    +
    +        int keepTrying = 0;
    +
    +        long curser = -1;
    +
    +        do
    +        {
    +            try
    +            {
    +                twitter4j.User followee4j;
    +                String followeeJson;
    +                try {
    +                    followee4j = client.users().showUser(id);
    +                    followeeJson = TwitterObjectFactory.getRawJSON(followee4j);
    +                } catch (TwitterException e) {
    +                    LOGGER.error("Failure looking up " + id);
    +                    break;
    +                }
    +
    +                PagableResponseList<twitter4j.User> followerList = client.friendsFollowers().getFollowersList(id.longValue(),
curser);
    +
    +                for (twitter4j.User follower4j : followerList) {
    +
    +                    String followerJson = TwitterObjectFactory.getRawJSON(follower4j);
    +
    +                    try {
    +                        Follow follow = new Follow()
    +                                .withFollowee(mapper.readValue(followeeJson, User.class))
    +                                .withFollower(mapper.readValue(followerJson, User.class));
    +
    +                        ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue);
    +                    } catch (JsonParseException e) {
    +                        LOGGER.warn(e.getMessage());
    +                    } catch (JsonMappingException e) {
    +                        LOGGER.warn(e.getMessage());
    +                    } catch (IOException e) {
    +                        LOGGER.warn(e.getMessage());
    +                    }
    +                }
    +                curser = followerList.getNextCursor();
    +            }
    +            catch(TwitterException twitterException) {
    +                keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException);
    +            }
    +            catch(Exception e) {
    +                keepTrying += TwitterErrorHandler.handleTwitterError(client, e);
    +            }
    +        } while (curser != 0 && keepTrying < 10);
    +    }
    +
    +    protected void getFollowers(String screenName) {
    --- End diff --
    
    Since showUser() is called, getFollowers(Long id) could be the primary implementation
with getFollowers(String screenName) calling it after the id is available from the user lookup.
This would cut out a bit of code.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message