def update_favourites_list(users_list, account): logger.info('Started updating favourites list!') favourites = [] updates = get_poll_updates(account).json()['result'] for upd in updates: try: options = upd['poll']['options'] for opt in options: if opt['voter_count'] > 0: acc_name = opt['text'].split(',')[0] favourites.append(acc_name) except KeyError: continue favourites = set(favourites) TwitterFollower.objects.filter( screen_name__in=favourites, account_owner=account).update(is_favourite=True) # send csv statistic db_favourites = TwitterFollower.objects.filter( is_favourite=True, account_owner=account).values_list('user_id', flat=True) favourites_list = [f for f in users_list if f.id_str in db_favourites] favourites_list_print = [ '[{}]({}), {}'.format(u.screen_name, 'twitter.com/{}'.format(u.screen_name), u.followers_count) for u in favourites_list ] text = 'Favourites list!\n\n' + '\n'.join(favourites_list_print) send_message_to_telegram(text, account, mode='Markdown') send_csv_statistic_to_telegram(favourites_list, account, 'favourites') logger.info('Finished updating favourites list!')
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))
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)
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)
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
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