Merge remote-tracking branch 'tildetown/master'

pull/27/head
nathaniel smith 2017-11-10 18:43:33 -08:00
commit c6a720e7fb
4 changed files with 84 additions and 1 deletions

View File

@ -19,6 +19,8 @@ setup(
'sshpubkeys==2.2.0', 'sshpubkeys==2.2.0',
'psycopg2==2.6.2', 'psycopg2==2.6.2',
'requests==2.12.5', '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, 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_URL = "OVERWRITE THIS"
MAILGUN_KEY = "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 django.contrib.auth.models import Group
from .models import Townie, Pubkey from .models import Townie, Pubkey
from common.social import post_users_to_social
class PubkeyInline(admin.TabularInline): class PubkeyInline(admin.TabularInline):
model = Pubkey model = Pubkey
@ -12,6 +13,7 @@ def bulk_review(madmin, req, qs):
for townie in qs: for townie in qs:
townie.reviewed = True townie.reviewed = True
townie.save() townie.save()
post_users_to_social(qs)
bulk_review.short_description = 'mark selected townies as reviewed' bulk_review.short_description = 'mark selected townies as reviewed'