2021-08-06 00:39:58 +01:00
|
|
|
#!/usr/local/bin/python3.9
|
2021-07-06 14:09:06 +01:00
|
|
|
import tweepy
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import re
|
|
|
|
import math
|
|
|
|
|
|
|
|
print(datetime.now().strftime("%H:%M:%S"))
|
|
|
|
callback_uri = 'oob' # https://cfe.sh/twitter/callback
|
|
|
|
|
|
|
|
auth = tweepy.OAuthHandler(os.getenv('API_TOKEN'), os.getenv('API_KEY_SECRET'), callback_uri)
|
|
|
|
auth.set_access_token(os.getenv('ACCESS_TOKEN'), os.getenv('ACCESS_TOKEN_SECRET'))
|
|
|
|
|
|
|
|
api = tweepy.API(auth)
|
2021-08-06 10:06:19 +01:00
|
|
|
lastSeenFile = 'last_seen.txt'
|
|
|
|
if not os.path.isfile(lastSeenFile):
|
|
|
|
with open(lastSeenFile, 'w+') as fileWrite:
|
|
|
|
fileWrite.write(str(0))
|
2021-07-06 14:09:06 +01:00
|
|
|
|
|
|
|
def getLastSeen(filename):
|
|
|
|
with open(filename, 'r') as fileRead:
|
|
|
|
lastSeen = int(fileRead.read().strip())
|
|
|
|
return lastSeen
|
|
|
|
|
|
|
|
def storeLastSeen(filename, lastSeen):
|
|
|
|
with open(filename, 'w') as fileWrite:
|
|
|
|
fileWrite.write(str(lastSeen))
|
|
|
|
return
|
|
|
|
|
|
|
|
def searchTweets(query):
|
|
|
|
print('Searching for tweets')
|
|
|
|
lastSeen = getLastSeen(lastSeenFile)
|
|
|
|
tweets = api.search(query, since_id=lastSeen)
|
|
|
|
return tweets
|
|
|
|
|
|
|
|
def interactions():
|
|
|
|
tweets = searchTweets('persevering')
|
|
|
|
for tweet in reversed(tweets):
|
|
|
|
if not tweets:
|
|
|
|
print('No new tweets found.')
|
|
|
|
return
|
|
|
|
if re.search('what is.*if not.*persevering',tweet.text.lower()):
|
|
|
|
if not tweet.text.lower().startswith('rt') and tweet.user != api.me():
|
|
|
|
print(f'Tweet {tweet.text} by @{tweet.author.screen_name}', flush = True)
|
2021-08-06 10:06:19 +01:00
|
|
|
t = api.get_status(tweet.id)
|
|
|
|
if not t.favorited:
|
2021-07-06 14:09:06 +01:00
|
|
|
print('Not favourited.')
|
2021-08-06 00:39:58 +01:00
|
|
|
api.create_favorite(tweet.id)
|
2021-08-06 10:06:19 +01:00
|
|
|
if not t.retweeted:
|
2021-07-06 14:09:06 +01:00
|
|
|
print('Not retweeted.')
|
2021-08-06 00:39:58 +01:00
|
|
|
api.retweet(tweet.id)
|
2021-07-06 14:09:06 +01:00
|
|
|
storeLastSeen(lastSeenFile, tweet.id)
|
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
nextFifth = 5-(now.minute%5)
|
|
|
|
later = (now + timedelta(minutes=nextFifth)).replace(second=0,microsecond=0)
|
|
|
|
tdelta = later - datetime.now()
|
|
|
|
sec = math.floor(tdelta.total_seconds())
|
|
|
|
print(f'Waiting until {later} for first search interaction cycle, sleeping for {sec} seconds.')
|
|
|
|
time.sleep(sec)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
interactions()
|
|
|
|
print('Sleeping for 5 minutes')
|
|
|
|
time.sleep(300)
|