Merge pull request #23 from tildetown/arch-dev

Add functions to post to social media
pull/27/head
Nathaniel Smith 2017-11-09 16:06:01 -08:00 committed by GitHub
commit 948aef85c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 1 deletions

View File

@ -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,
)

View File

@ -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)

View File

@ -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"

View File

@ -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'