From 543829cf1bbad3edc818804b19ddad95c310e475 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 12:50:30 -0800 Subject: [PATCH 1/9] add social functions to post new users --- ttadmin/common/social.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ttadmin/common/social.py diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py new file mode 100644 index 0000000..6e8a567 --- /dev/null +++ b/ttadmin/common/social.py @@ -0,0 +1,28 @@ +from mastodon import Mastodon +import tweepy + +from django.conf import settings + +mastodon = Mastodon( + client_id=settings.MASTO_CLIENT_ID, + client_secret=settings.MASTO_CLIENT_SECRET, + access_token=settings.MASTO_ACCESS_TOKEN, + api_base_url=settings.MASTO_BASE_URL +) + +tw_auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET) +tw_auth.set_acces_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) + else: + message = 'Welcome new user {}'.format(users) + mastodon.post(message) + twitter.update_status(message) + From 44f1b93e5e5b49e224ef349abe879faae4fd00e2 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 12:50:57 -0800 Subject: [PATCH 2/9] add configuration variables for posting to social --- ttadmin/settings.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ttadmin/settings.py b/ttadmin/settings.py index 205312b..b3c4091 100644 --- a/ttadmin/settings.py +++ b/ttadmin/settings.py @@ -103,3 +103,16 @@ STATIC_ROOT = 'static' MAILGUN_URL = "OVERWRITE THIS" MAILGUN_KEY = "OVERWRITE THIS" + +# Mastodon credentials +MASTO_CLIENT_ID = "OVERWRITE THIS" +MASTO_CLIENT_SECRET = "OVERWRITE THIS" +MASTO_ACCESS_TOKEN = "OVERWRITE THIS" +MASTO_BASE_URL = "https://tiny.tilde.website" + +# Twitter credentials +TWITTER_CONSUMER_KEY = "OVERWRITE THIS" +TWITTER_CONSUMER_SECRET = "OVERWRITE THIS" +TWITTER_TOKEN = "OVERWRITE THIS" +TWITTER_TOKEN_SECRET = "OVERWRITE THIS" + From a8998b30ee9019f83c09c79c5aecc17563a1a114 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 12:51:20 -0800 Subject: [PATCH 3/9] add new requirements --- setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f1b0d30..969eae5 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,8 @@ setup( 'sshpubkeys==2.2.0', 'psycopg2==2.6.2', 'requests==2.12.5', - 'gunicorn==19.6.0'], + 'gunicorn==19.6.0', + 'Mastodon.py==1.1.1', + 'tweepy==3.5.0'], include_package_data = True, ) From 35327b392262bc8391231b64a14f1bfb4a16f158 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 12:56:28 -0800 Subject: [PATCH 4/9] add social posting function --- ttadmin/users/admin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ttadmin/users/admin.py b/ttadmin/users/admin.py index 2ff3ce9..82ff673 100644 --- a/ttadmin/users/admin.py +++ b/ttadmin/users/admin.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User from django.contrib.auth.models import Group from .models import Townie, Pubkey +from common.social import post_to_social admin.site.unregister(User) admin.site.unregister(Group) @@ -15,6 +16,7 @@ def bulk_review(madmin, req, qs): for townie in qs: townie.reviewed = True townie.save() + post_to_social(qs) bulk_review.short_description = 'mark selected townies as reviewed' From d1fa97cd8ed01a9166c3c41185bd00f1403af8db Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 16:11:22 -0800 Subject: [PATCH 5/9] 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) From 1b4f3421193f7627a54c49c4001d62cd0d600db6 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Tue, 7 Nov 2017 16:15:42 -0800 Subject: [PATCH 6/9] fix typo --- ttadmin/common/social.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py index d2a70d1..b3a3657 100644 --- a/ttadmin/common/social.py +++ b/ttadmin/common/social.py @@ -44,7 +44,7 @@ def post_to_twitter(qs): message = welcome + '~{}\n'.format(townie.username) else: message += '~{}\n'.format(townie.username) - post.append(message.strip()) + posts.append(message.strip()) for post in posts: twitter.update_status(post) From d3f6fc4e863c6d86bfc3d527a0ae65377ae842c1 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Thu, 9 Nov 2017 13:57:25 -0800 Subject: [PATCH 7/9] properly split text for tweets and toots --- ttadmin/common/social.py | 72 +++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py index b3a3657..1287a44 100644 --- a/ttadmin/common/social.py +++ b/ttadmin/common/social.py @@ -1,3 +1,5 @@ +import re + from mastodon import Mastodon import tweepy @@ -14,41 +16,49 @@ tw_auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CO tw_auth.set_access_token(settings.TWITTER_TOKEN, settings.TWITTER_TOKEN_SECRET) twitter = tweepy.API(tw_auth) -def post_to_mastodon(qs): +def split_posts_by_length(text, length): + pattern = '.{,%d}(?:\s|$)' % length - 1 + chunks = re.findall(pattern, text) 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 > 500: - posts.append(message.strip()) - message = welcome + '~{}\n'.format(townie.username) + post = '' + for chunk in chunks: + if len(post + chunk) <= length: + post += chunk else: - message += '~{}\n'.format(townie.username) - posts.append(message.strip()) - for post in posts: - mastodon.post(post) + posts.append(post) + post = chunk + return posts -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) + +def post_to_mastodon(message): + posts = split_posts_by_length(message, 500) + status_info = None + for post in posts: + if status_info: + status_info = mastodon.status_post(post, in_reply_to_id=status_info['id']) else: - message += '~{}\n'.format(townie.username) - posts.append(message.strip()) + status_info = mastodon.status_post(post) + + +def post_to_twitter(message): + posts = split_posts_by_length(message, 140) + status_info = None for post in posts: - twitter.update_status(post) + if status_info: + status_info = twitter.update_status(post, in_reply_to_status_id=status_info.id) + else: + status_info = twitter.update_status(post) -def post_to_social(qs): - post_to_twitter(qs) - post_to_mastodon(qs) + +def post_users_to_social(qs): + users = '' + for townie in qs: + users += '~{}\n'.format(townie.username) + users = users.strip() + if len(qs) > 1: + message = 'Welcome new users!!!\n\n{}'.format(users) + else: + message = 'Welcome new user {}!'.format(users) + post_to_mastodon(message) + post_to_twitter(message) From 30a57d14619d092d143bf9c0fc5807541fa1d3f1 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Thu, 9 Nov 2017 13:58:57 -0800 Subject: [PATCH 8/9] update function name --- ttadmin/users/admin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ttadmin/users/admin.py b/ttadmin/users/admin.py index 82ff673..201c797 100644 --- a/ttadmin/users/admin.py +++ b/ttadmin/users/admin.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User from django.contrib.auth.models import Group from .models import Townie, Pubkey -from common.social import post_to_social +from common.social import post_users_to_social admin.site.unregister(User) admin.site.unregister(Group) @@ -16,7 +16,7 @@ def bulk_review(madmin, req, qs): for townie in qs: townie.reviewed = True townie.save() - post_to_social(qs) + post_users_to_social(qs) bulk_review.short_description = 'mark selected townies as reviewed' From 033054821b75becffa4c0104114c9d31ac9c5d6b Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Thu, 9 Nov 2017 14:04:16 -0800 Subject: [PATCH 9/9] make sure final post actually gets added to list --- ttadmin/common/social.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py index 1287a44..72966d8 100644 --- a/ttadmin/common/social.py +++ b/ttadmin/common/social.py @@ -27,6 +27,8 @@ def split_posts_by_length(text, length): else: posts.append(post) post = chunk + if post: + posts.append(post) return posts