#!/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 # 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') from mastodon import Mastodon 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() # Authorize Twitter, initialize tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) # Authorize Mastodon, set api base # TODO: Modularize this mastodon = Mastodon( access_token = '~/.secrets/mastodon_api_access_token.secret', 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 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 = [] outtweets = [] # Only bother processing new tweets. for tweet in new_tweets: if(int(tweet.id) > last_tweet): 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']) 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']) sleep(30) fresh_start = False # 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())