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, ) diff --git a/ttadmin/common/social.py b/ttadmin/common/social.py new file mode 100644 index 0000000..72966d8 --- /dev/null +++ b/ttadmin/common/social.py @@ -0,0 +1,66 @@ +import re + +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_access_token(settings.TWITTER_TOKEN, settings.TWITTER_TOKEN_SECRET) +twitter = tweepy.API(tw_auth) + +def split_posts_by_length(text, length): + pattern = '.{,%d}(?:\s|$)' % length - 1 + chunks = re.findall(pattern, text) + posts = [] + post = '' + for chunk in chunks: + if len(post + chunk) <= length: + post += chunk + else: + posts.append(post) + post = chunk + if post: + posts.append(post) + return posts + + +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: + 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: + 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_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) + 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" + diff --git a/ttadmin/users/admin.py b/ttadmin/users/admin.py index 2ff3ce9..201c797 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_users_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_users_to_social(qs) bulk_review.short_description = 'mark selected townies as reviewed'