2017-11-09 21:57:25 +00:00
|
|
|
import re
|
|
|
|
|
2017-11-07 20:50:30 +00:00
|
|
|
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)
|
2017-11-08 00:11:22 +00:00
|
|
|
tw_auth.set_access_token(settings.TWITTER_TOKEN, settings.TWITTER_TOKEN_SECRET)
|
2017-11-07 20:50:30 +00:00
|
|
|
twitter = tweepy.API(tw_auth)
|
|
|
|
|
2017-11-09 21:57:25 +00:00
|
|
|
def split_posts_by_length(text, length):
|
|
|
|
pattern = '.{,%d}(?:\s|$)' % length - 1
|
|
|
|
chunks = re.findall(pattern, text)
|
2017-11-08 00:11:22 +00:00
|
|
|
posts = []
|
2017-11-09 21:57:25 +00:00
|
|
|
post = ''
|
|
|
|
for chunk in chunks:
|
|
|
|
if len(post + chunk) <= length:
|
|
|
|
post += chunk
|
2017-11-08 00:11:22 +00:00
|
|
|
else:
|
2017-11-09 21:57:25 +00:00
|
|
|
posts.append(post)
|
|
|
|
post = chunk
|
2017-11-09 22:04:16 +00:00
|
|
|
if post:
|
|
|
|
posts.append(post)
|
2017-11-09 21:57:25 +00:00
|
|
|
return posts
|
2017-11-08 00:11:22 +00:00
|
|
|
|
2017-11-09 21:57:25 +00:00
|
|
|
|
|
|
|
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'])
|
2017-11-08 00:11:22 +00:00
|
|
|
else:
|
2017-11-09 21:57:25 +00:00
|
|
|
status_info = mastodon.status_post(post)
|
|
|
|
|
|
|
|
|
|
|
|
def post_to_twitter(message):
|
2017-11-16 19:17:30 +00:00
|
|
|
posts = split_posts_by_length(message, 280)
|
2017-11-09 21:57:25 +00:00
|
|
|
status_info = None
|
2017-11-08 00:11:22 +00:00
|
|
|
for post in posts:
|
2017-11-09 21:57:25 +00:00
|
|
|
if status_info:
|
|
|
|
status_info = twitter.update_status(post, in_reply_to_status_id=status_info.id)
|
|
|
|
else:
|
|
|
|
status_info = twitter.update_status(post)
|
2017-11-08 00:11:22 +00:00
|
|
|
|
2017-11-09 21:57:25 +00:00
|
|
|
|
|
|
|
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)
|
2017-11-07 20:50:30 +00:00
|
|
|
|
2017-11-16 19:14:40 +00:00
|
|
|
def post_single_user_social(username):
|
|
|
|
message = 'Welcome new user ~{}!'.format(username)
|
|
|
|
post_to_mastodon(message)
|
|
|
|
post_to_twitter(message)
|
|
|
|
|