Esempio n. 1
0
def main(args):
    """Bot main function."""
    # Load configurations
    config = load_config(args.config)

    sleep.interval = config['request_interval']
    sleep.window = config['request_window']

    # Twitter OAuth Authentication
    log.success('Authenticating on Twitter API')
    twitter_auth = tweepy.OAuthHandler(config['consumer_key'],
                                       config['consumer_secret'])
    twitter_auth.set_access_token(config['access_token'],
                                  config['access_token_secret'])

    # Initialize Twitter API
    twitter_api = tweepy.API(twitter_auth)

    # Search for tweets containing terms from config file
    for term in config['terms']:
        tweets = search_tweets(twitter_api, term, config['tweets_count'],
                               False)
        like_tweets(twitter_api, tweets)  # Add gathered tweets to favorites

    # Show favorites
    get_favorites(twitter_api, 0, False)
Esempio n. 2
0
def load_config(conf):
    """Load configuration from file."""
    log.success('Loading configurations from {file}'.format(file=conf))
    config = dict()

    if conf:
        try:
            with open(conf, 'rt') as conf_fd:
                config = json.load(conf_fd)
        except FileNotFoundError:
            log.error('Specified configuration file not found')
            sys.exit(1)
        except Exception:
            log.error('An error occured while parsing configuration file')
            sys.exit(1)

    return config
Esempio n. 3
0
def like_tweets(api, tweets):
    """Add specified tweets to favorites."""
    log.success('Adding gathered tweets to favorites')

    for tweet in tweets:
        try:
            result = api.create_favorite(tweet['id_str'])
        except tweepy.error.TweepError as e:  # Already favorited
            if e.response is not None and e.response.status_code in set([429]):
                sleep.sleep_window()
            elif not e.api_code or (e.api_code
                                    and e.api_code not in set([139])):
                # log.debug(e.response.__dict__)
                log.debug(e)

            continue

        sleep.sleep_interval()  # Sleep to prevent API rate limit

    return result
Esempio n. 4
0
def get_home_timeline(api, limit=100, display=True):
    """Get home timeline."""
    log.success('Gathering home timeline')

    result = handle_request(tweepy.Cursor(api.home_timeline).items(limit))
    tweets = []

    if result:
        for tweet in result:
            tweets += [{'id_str': tweet.id_str, 'text': tweet.text}]

        count = len(tweets)
        log.bold('{count} tweet{s}'.format(count=count,
                                           s='' if count == 1 else 's'))

        if display:
            for tweet in tweets:
                log.info('{tweet}'.format(tweet=tweet['text']))

    sleep.sleep_interval()  # Sleep to prevent API rate limit
    return tweets
Esempio n. 5
0
def get_followers(api, limit=100, display=True):
    """Get followers list."""
    log.success('Gathering followers')

    result = handle_request(tweepy.Cursor(api.followers).items(limit))
    users = []

    if result:
        for user in result:
            users += [{'id_str': user.id_str, 'screen_name': user.screen_name}]

        count = len(users)
        log.bold('{count} user{s}'.format(count=count,
                                          s='' if count == 1 else 's'))

        if display:
            for user in users:
                log.info('@{name}'.format(name=user['screen_name']))

    sleep.sleep_interval()  # Sleep to prevent API rate limit
    return users
Esempio n. 6
0
def search_tweets(api, term, limit=100, display=True):
    """Search for tweets containing specified term."""
    log.success('Gathering {count} tweet{s} containing \'{term}\''.format(
        count=limit, s='' if limit == 1 else 's', term=term))

    result = handle_request(tweepy.Cursor(api.search, q=term).items(limit))
    tweets = []

    if result:
        for tweet in result:
            tweets += [{'id_str': tweet.id_str, 'text': tweet.text}]

        count = len(tweets)
        log.bold('{count} tweet{s} containing \'{term}\''.format(
            count=count, s='' if count == 1 else 's', term=term))

        if display:
            for tweet in tweets:
                log.info('{tweet}'.format(tweet=tweet['text']))

    sleep.sleep_interval()  # Sleep to prevent API rate limit
    return tweets
Esempio n. 7
0
 def window(self, value):
     """Define window."""
     log.success('Updating request window to {value}'.format(
         value=self.pretty_time(value)))
     self.request_window = value
Esempio n. 8
0
 def interval(self, value):
     """Define interval."""
     log.success('Updating request interval to {value}'.format(
         value=self.pretty_time(value)))
     self.request_interval = value