From d1fa97cd8ed01a9166c3c41185bd00f1403af8db Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 16:11:22 -0800 Subject: [PATCH] break out functions, check if too large --- ttadmin/common/social.py | 48 +++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py index 6e8a567..d2a70d1 100644 --- a/ttadmin/common/social.py +++ b/ttadmin/common/social.py @@ -11,18 +11,44 @@ mastodon = Mastodon( ) tw_auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET) -tw_auth.set_acces_token(settings.TWITTER_TOKEN, settings.TWITTER_TOKEN_SECRET) +tw_auth.set_access_token(settings.TWITTER_TOKEN, settings.TWITTER_TOKEN_SECRET) twitter = tweepy.API(tw_auth) -def post_to_social(qs): - users = '' - for townie in qs: - users += '~{}\n'.format(townie.username) - users = users.strip() - if len(qs) != 0: - message = 'Welcome new users!!!\n\n{}'.format(users) +def post_to_mastodon(qs): + posts = [] + if len(qs) > 1: + welcome = 'Welcome new user ' else: - message = 'Welcome new user {}'.format(users) - mastodon.post(message) - twitter.update_status(message) + welcome = 'Welcome new users!!!\n\n' + message = welcome + for townie in qs: + if len(message + townie.username) + 1 > 500: + posts.append(message.strip()) + message = welcome + '~{}\n'.format(townie.username) + else: + message += '~{}\n'.format(townie.username) + posts.append(message.strip()) + for post in posts: + mastodon.post(post) + +def post_to_twitter(qs): + posts = [] + if len(qs) > 1: + welcome = 'Welcome new user ' + else: + welcome = 'Welcome new users!!!\n\n' + message = welcome + for townie in qs: + if len(message + townie.username) + 1 > 140: + posts.append(message.strip()) + message = welcome + '~{}\n'.format(townie.username) + else: + message += '~{}\n'.format(townie.username) + post.append(message.strip()) + for post in posts: + twitter.update_status(post) + +def post_to_social(qs): + post_to_twitter(qs) + post_to_mastodon(qs)