This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
multipub/tweet_echoer.py

104 lines
3.3 KiB
Python
Raw Normal View History

2019-02-16 01:17:41 -06:00
#!/usr/bin/env python
# encoding: utf-8
# Hey lazy, move this to its own repo, it doesn't need to be here
# Also give it its own API key
2019-02-16 01:17:41 -06:00
# Tweet echoing works. I just need this to be refactored into something more useful...
try:
from lib.twitter.tweet_converter import convert_tweets
except ImportError:
try:
from tweepy import convert_tweets
except ImportError:
print('Errori mporting tweet_converter')
2019-02-17 01:38:53 -06:00
from mastodon import Mastodon
2019-02-16 01:17:41 -06:00
from time import sleep
import tweepy
# Tentative... Might just settle with line-by-line with encryption
# TODO: Import and utilize encryption methods.
import json
# TODO: Major clean up.
def echo_recent_tweets(screen_name):
# TODO: Encrypt this file for extra security?
# TODO: Probably move all of this to its own function.
with open('keys.txt', 'r') as f:
#Twitter API credential
consumer_key = f.readline().rstrip()
consumer_secret = f.readline().rstrip()
access_key = f.readline().rstrip()
access_secret = f.readline().rstrip()
2019-02-17 01:38:53 -06:00
# Authorize Twitter, initialize tweepy
2019-02-16 01:17:41 -06:00
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
2019-02-17 01:38:53 -06:00
# Authorize Mastodon, set api base
# TODO: Modularize this
mastodon = Mastodon(
access_token = '~/.secrets/mastodon_api_access_token.secret',
2019-02-17 01:38:53 -06:00
api_base_url = 'https://masto.werefoxsoftware.com'
)
# In case I am just now starting up script, don't make 10 posts.
# TODO: Fix this later.
fresh_start = True
2019-02-16 01:17:41 -06:00
last_tweet = 0
# TODO: Don't do while(True). Use another safer method.
while(True):
# Request the last 10 tweets, since I'm assuming I won't have made 10 tweets in 30 seconds...
new_tweets = api.user_timeline(screen_name = screen_name,count=10,tweet_mode = 'extended')
recent_tweets = []
2019-02-17 01:38:53 -06:00
outtweets = []
2019-02-16 01:17:41 -06:00
# Only bother processing new tweets.
for tweet in new_tweets:
2019-02-17 01:38:53 -06:00
if(int(tweet.id) > last_tweet):
2019-02-16 01:17:41 -06:00
recent_tweets.append(tweet)
# Only do this if there are new tweets.
if recent_tweets:
outtweets = convert_tweets(recent_tweets)
last_tweet = int(outtweets[len(outtweets) - 1]['id'])
2019-02-17 01:38:53 -06:00
if(not fresh_start):
# Debugging purposes...
# TODO: Clean this up probably.
for t in outtweets:
'''
Mastodon.status_post(
status,
in_reply_to_id=None,
media_ids=None,
sensitive=False,
visibility=None,
spoiler_text=None,
language=None,
idempotency_key=None)
'''
if((not t['reply']) and (not t['retweet'])):
mastodon.status_post(
t['text'][1:-1],
visibility='public',
spoiler_text='Twitter Echo, I\'m not on Masto rn.'
)
print(t['date'] + '\t' + t['text'])
2019-02-16 01:17:41 -06:00
sleep(30)
2019-02-17 01:38:53 -06:00
fresh_start = False
2019-02-16 01:17:41 -06:00
# TODO: Stop reading users from a file, unnecessary.
if __name__ == '__main__':
with open('users.txt', 'r') as f:
for l in f:
echo_recent_tweets(l.rstrip())