コード例 #1
0
def retweet_verified_users_with_tag(user):
    api = connect_to_twitter_api(user)

    # get random verified user from our DB
    ids_list = VerifiedUserWithTag.objects.filter(
        account_owner=user).values_list('id', flat=True)
    ver_user = VerifiedUserWithTag.objects.get(id=random.choice(ids_list),
                                               account_owner=user)

    # get recent 20 tweets for current user
    recent_tweets = api.user_timeline(ver_user.screen_name)

    if ver_user and ver_user.tags:
        tag = '#{}'.format(random.choice(ver_user.tags))
    else:
        tag = ''

    for tweet in recent_tweets:
        tw_text = tweet.text.lower()

        if tag in tw_text and not tweet.in_reply_to_status_id and (
                tweet.lang == 'en' and not tweet.in_reply_to_user_id):

            try:
                api.retweet(tweet.id)
            except tweepy.error.TweepError as err:
                if err.api_code == 327 or err.api_code == 185:
                    logger.info(err.args[0][0]['message'])
                    continue
            msg = 'New retweet for {}. Date: {}'.format(user, date.today())
            send_message_to_slack(msg)
            return
コード例 #2
0
def follow_all_own_followers(account):
    logger.info('Start follow own followers for {}'.format(
        account.screen_name))
    api = connect_to_twitter_api(account)
    count = _follow_followers(api)
    text = "Account: {}. Follow {} own followers." \
           " Date: {}".format(account.screen_name, count, date.today())
    send_message_to_slack(text)
    send_message_to_telegram(text, account)
    logger.info('Finish follow own followers for {}'.format(
        account.screen_name))
コード例 #3
0
def make_follow_for_current_account(account):
    """Follow twitter accounts for current account owner"""
    logger.info('Start follow for {}'.format(account.screen_name))
    api = connect_to_twitter_api(account)
    before_stat = get_count_of_followers_and_following(api)
    _follow_target_accounts(account, api)
    stats = get_count_of_followers_and_following(api)
    text = ("Finished following. Account: {}. Number of followers: {}."
            " We're following {}. Following before task: {}. Date: {}.".format(
                account.screen_name, *stats, before_stat[1], date.today()))
    send_message_to_slack(text)
    send_message_to_telegram(text, account)
    logger.info(text)
コード例 #4
0
def update_db_lists_non_automatic_changes(accounts_list, acc_owner, user_type,
                                          db_list, tw_user):
    db_users = [user['user_id'] for user in db_list]

    tw_list = [acc.id_str for acc in accounts_list]
    lost_accounts = [acc for acc in db_users if acc not in tw_list]
    TwitterFollower.objects.filter(user_id__in=lost_accounts,
                                   user_type=user_type,
                                   account_owner=acc_owner).delete()

    if user_type == TwitterFollower.FOLLOWER:
        new_followers = [f for f in accounts_list if f.id_str not in db_users]
        new_followers = sorted(new_followers,
                               key=lambda f: f.followers_count,
                               reverse=True)
        titles = '[{}]({})\nLocation: {}\nFollowers: {}, Friends: {}\n' \
                 '\U0000270F: {}\n'

        new_followers_info = [
            titles.format(u.screen_name,
                          'twitter.com/{}'.format(u.screen_name),
                          replace_characters(u.location, '\n*_`'),
                          u.followers_count, u.friends_count,
                          replace_characters(u.description, '\n*_`'))
            for u in new_followers
        ]
        stats = tw_user.followers_count, tw_user.friends_count
        text = ('New Twitter Followers Report!\n'
                '\U00002705{}  \U0000274C{}  \U00002B06{}\n'
                'Date: {}\nFollowers: {}, Friends: {}\n\n').format(
                    len(new_followers), len(lost_accounts),
                    len(new_followers) - len(lost_accounts), date.today(),
                    *stats)
        text += '\n'.join(new_followers_info)

        # avoid telegram markdown errors
        send_message_to_telegram(text, acc_owner, mode='Markdown')
        send_message_to_slack(text)

        # send poll to add followers to favourites
        acc_names = [
            ', '.join([acc.screen_name,
                       str(acc.followers_count)]) for acc in new_followers
        ]
        send_poll_to_telegram(acc_owner, acc_names)
コード例 #5
0
def unfollow():
    accounts = AccountOwner.objects.filter(is_active=True)
    for account in accounts:
        try:
            make_unfollow_for_current_account(account)
        except HTTPError:
            logger.exception('Something gone wrong')
        except tweepy.error.TweepError as err:
            message = err.args[0][0]['message']
            logger.info(message)
            if err.api_code in [89, 226, 326]:
                send_message_to_slack(message)
                send_message_to_telegram(message, account)
            else:
                logger.exception(err)
                raise err
        except Exception as err:
            logger.exception(err)
            raise err
コード例 #6
0
def retweet_verified_users(user):
    api = connect_to_twitter_api(user)
    max_retweets = _get_tweets_to_retweet(api)
    last_five_days_tweets = _get_retweeted_screen_names(api)

    for tweet in max_retweets:
        logger.info('Try to retweet tweet {} of {}'.format(
            tweet.id, tweet.user.screen_name))
        if tweet.user.screen_name not in last_five_days_tweets:
            try:
                api.retweet(tweet.id)
            except tweepy.error.TweepError as err:
                if err.api_code == 327 or err.api_code == 185:
                    logger.info(err.args[0][0]['message'])
                    continue
            msg = 'New retweet! Date: {}\ntwitter.com/{}/status/{}'.format(
                datetime.today().date(), tweet.user.screen_name, tweet.id)
            send_message_to_slack(msg)
            send_message_to_telegram(msg, user, False)
            return