Exemple #1
0
def get_new_friends(api: twitter.Api, bot: Bot, old_following: list,
                    current_following: set, user: str):
    """Get following ids

    Args:
        api: twitter api
        bot: telegram bot
        old_following: the following already in memory
        user: user to check the following of
    """
    new_following = tuple(current_following.difference(old_following))
    if len(new_following) > 0:
        logger.info("Updating following of %s", user)
        new_following_users = []
        for new_following_istance in (
                new_following[x:x + 100]
                for x in range(0, len(new_following), 100)):
            new_following_users += api.UsersLookup(
                user_id=new_following_istance)
        new_following_values = tuple(
            map(lambda e: (user, e.id, e.screen_name), new_following_users))
        DbManager.insert_into(table_name="followed_users",
                              columns=("follower_name", "followed_id",
                                       "followed_name"),
                              values=new_following_values,
                              multiple_rows=True)
        notify_user(
            bot=bot,
            follower_name=user,
            new_following=new_following_users,
            start_message=f"started following {len(new_following)} new users")
Exemple #2
0
    def run(self):
        try:
            from twitter import Api
        except ImportError as ie:
            raise ie

        d = dict()

        api = Api(Consumer_Key, Consumer_Secret, Access_Token,
                  Access_Token_Secret)

        cdlt = api.UsersLookup(screen_name=["CordaDLT"])[0]
        corda_followers = api.GetFollowers(user_id=cdlt)

        d["twitter_followers"] = len(corda_followers)

        usertimeline = [
            x for x in api.GetUserTimeline(count=100) if x.retweeted == False
        ]

        total_faves = 0
        total_retweets = 0

        for x in usertimeline[0:10]:
            #print x
            #   print x.text, x.favorite_count, x.retweet_count
            total_faves += x.favorite_count
            total_retweets += x.retweet_count

    #   print "In last 10 tweets"
    #   print total_faves, "faves", total_retweets, "retweets"

        d["twitter_running_10_average_faves"] = total_faves / 10.0
        d["twitter_running_10_retweets"] = total_retweets / 10.0

        return d
Exemple #3
0
class Twitter:
    def __init__(self, access_token_key, access_token_secret):
        self.consumer_key = settings.TWITTER_CONSUMER_KEY
        self.consumer_secret = settings.TWITTER_CONSUMER_SECRET
        self.access_token_key = access_token_key
        self.access_token_secret = access_token_secret

        self.api = Api(consumer_key=self.consumer_key,
                       consumer_secret=self.consumer_secret,
                       access_token_key=self.access_token_key,
                       access_token_secret=self.access_token_secret)

        self.key_id = ":1:twitter:user:id:{}"
        self.key_name = ":1:twitter:user:name:{}"

    def create_block(self, user_id):
        self.api.CreateBlock(user_id)

    def destroy_block(self, user_id):
        self.api.DestroyBlock(user_id)

    def lookup_users_from_id(self, ids):
        results = []
        missing_ids = []

        pipe = con.pipeline()

        print('Retrieving from cache....')
        for user_id in ids:
            pipe.get(self.key_id.format(user_id))

        users_found = pipe.execute()

        if None in users_found:
            print('Missing users...')
            for index, user_found in enumerate(users_found):
                if user_found is None:
                    missing_ids.append(ids[index])
                else:
                    results.append(user_found.decode('utf-8'))
        else:
            print('All users found!')
            print(users_found)
            return [u.decode('utf-8') for u in users_found]

        print('Retrieving from twitter api...')
        try:
            users = self.api.UsersLookup(user_id=missing_ids,
                                         include_entities=False)
            pipe = con.pipeline()
            for user in users:
                print(user.screen_name, user.id)
                pipe.set(self.key_id.format(user.id), user.screen_name,
                         60 * 60)
                pipe.set(self.key_name.format(user.screen_name), user.id,
                         60 * 60)
                results.append(user.screen_name)
        except TwitterError:
            return results

        pipe.execute()

        print('Cache refreshed...')

        return results

    def lookup_users_from_screen_name(self, screen_names):
        results = []
        missing_names = []

        pipe = con.pipeline()

        print('Retrieving {} from cache....'.format(len(screen_names)))
        for name in screen_names:
            pipe.get(self.key_name.format(name))

        users_found = pipe.execute()
        print('USERS_FOUND', users_found)

        if None in users_found:
            print('Missing users...')
            for index, user_found in enumerate(users_found):
                if user_found is None:
                    missing_names.append(screen_names[index])
                else:
                    results.append(int(user_found))
        else:
            print('All {} users found!'.format(len(screen_names)))
            return [int(i) for i in users_found]

        print('Retrieving from twitter api...')
        try:
            users = self.api.UsersLookup(screen_name=missing_names,
                                         include_entities=False)
            pipe = con.pipeline()
            for user in users:
                print(user.screen_name, user.id)
                pipe.set(self.key_id.format(user.id), user.screen_name,
                         60 * 60)
                pipe.set(self.key_name.format(user.screen_name), user.id,
                         60 * 60)
                results.append(user.id)
        except TwitterError:
            return results

        pipe.execute()

        print('Cache refreshed...')

        return results