Committed version 3 of the bot from the source code on my computer. Older versions of the bot were version controlled manually before I knew how to use Git.
This commit is contained in:
6
interaction/Dockerfile
Normal file
6
interaction/Dockerfile
Normal file
@ -0,0 +1,6 @@
|
||||
FROM python:3.8.6-buster
|
||||
COPY . /usr/src/app
|
||||
WORKDIR /usr/src/app
|
||||
RUN pip install --upgrade pip
|
||||
RUN pip install -r requirements.txt
|
||||
CMD python3 -u ./script.py
|
1
interaction/last_seen.txt
Normal file
1
interaction/last_seen.txt
Normal file
@ -0,0 +1 @@
|
||||
1368349440298409985
|
3
interaction/requirements.txt
Normal file
3
interaction/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
python-dotenv
|
||||
datetime
|
||||
tweepy
|
63
interaction/script.py
Normal file
63
interaction/script.py
Normal file
@ -0,0 +1,63 @@
|
||||
#!/usr/local/bin/python3.8
|
||||
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)
|
||||
lastSeenFile = '/usr/src/app/last_seen.txt'
|
||||
|
||||
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)
|
||||
if not tweet.favorited:
|
||||
print('Not favourited.')
|
||||
# api.create_favorite(tweet.id)
|
||||
if not tweet.retweeted:
|
||||
print('Not retweeted.')
|
||||
# api.retweet(tweet.id)
|
||||
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)
|
Reference in New Issue
Block a user