-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterBot.py
84 lines (72 loc) · 2.63 KB
/
twitterBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from flask import Flask, jsonify
import tweepy
import time
import logging
app = Flask(__name__)
# Logging setup
logging.basicConfig(filename='app.log', level=logging.DEBUG)
# Set up API keys
consumer_key = 'your-consumer-key'
consumer_secret = 'your-consumer-secret-key'
access_token = 'your-access-token'
access_token_secret = 'your-access-token-secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
selected_accounts = ['account1', 'account2', 'account3'] #add tokens in the following form: for example, @crypto_website should be included as crypto_website
def like_and_save_tweets():
messages = []
for account in selected_accounts:
try:
tweets = api.user_timeline(screen_name=account, count=1)
if tweets:
tweet = tweets[0]
if not tweet.favorited:
api.create_favorite(tweet.id)
messages.append(f"Liked latest tweet from {account}.")
if tweet.text:
with open('tweets_for_review.txt', 'a') as file:
file.write(tweet.text + "\n")
messages.append(f"Saved tweet from {account}.")
else:
messages.append(f"No tweets found for {account}.")
except Exception as e:
msg = f"Error for account {account}: {str(e)}"
logging.error(msg)
messages.append(msg)
return messages
@app.route('/')
def status():
return "The bot is up and running!"
@app.route('/like_and_save')
def trigger_activity():
messages = like_and_save_tweets()
return jsonify(messages)
@app.route('/like_latest_tweet')
def trigger_like():
messages = []
for account in selected_accounts:
try:
tweets = api.user_timeline(screen_name=account, count=1)
if tweets:
tweet = tweets[0]
if not tweet.favorited:
api.create_favorite(tweet.id)
messages.append(f"Liked latest tweet from {account}.")
else:
messages.append(f"No tweets found for {account}.")
except Exception as e:
msg = f"Error for account {account}: {str(e)}"
logging.error(msg)
messages.append(msg)
return jsonify(messages)
@app.route('/retrieve_tweets')
def retrieve_tweets():
with open('tweets_for_review.txt', 'r') as file:
tweets = file.readlines()
if tweets:
return jsonify(tweets)
else:
return "No tweets found."
if __name__ == "__main__":
app.run(debug=True)