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
2019-02-16 01:17:41 -06:00

73 lines
2.2 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
# Tweet echoing works. I just need this to be refactored into something more useful...
from tweet_converter import convert_tweets
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)
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 = []
# Only bother processing new tweets.
for tweet in new_tweets:
if(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'])
# Debugging purposes...
# TODO: Clean this up probably.
for t in outtweets:
print(t['date'] + '\t' + t['text'])
sleep(30)
# 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())
# Clean these up lmao
'''
#write the csv
with open('%s_tweets.csv' % screen_name, 'w') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","retweet","text","hashtags","user_mentions"])
writer.writerows(outtweets)
pass
'''