-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
277 lines (242 loc) · 9.08 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
app python mini tweet bot application
takes user input and tweets to designated account
"""
import tweepy
import multiprocessing
import os
import time
from flask import Flask, render_template, request, jsonify, redirect, url_for
from werkzeug import secure_filename
from time import sleep
from credentials import (consumer_key, consumer_secret, access_token,
access_token_secret)
# custom imports
import censorship
from aldict import ascii_dict, leet_dict
# custom variable names from imports
remove_whitespace = censorship.remove_whitespace
censor = censorship.censor
# tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# flask integrations
app = Flask(__name__)
port = int(os.getenv('PORT', 8080))
UPLOAD_FOLDER = './static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = set(['txt', 'png', 'jpg', 'jpeg', 'gif'])
# flask support function to verify file import type
def allowed_file(filename):
"""checks if file is allowable and safe guards the file"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# adds date to input image file
def add_date(filename):
"""makes each file unique by adding date"""
extension = filename[-4:]
prefix = filename[:-4]
thedate = time.strftime("%d-%m-%y--%H:%M:%S")
return "{:}{:}{:}".format(prefix, thedate, extension)
# translation to ascii or leet
def translate(tweetvar, c):
"""function translates input tweet to leet or ascii art"""
if c == 'a':
tweetvar = tweetvar.lower()
for key in ascii_dict:
if key in tweetvar:
tweetvar = tweetvar.replace(key, ascii_dict[key])
else:
for key in leet_dict:
if key in tweetvar:
tweetvar = tweetvar.replace(key, leet_dict[key])
return tweetvar
# begin tweet functions here
def tweet_text(tweetvar):
""" tweets text from input variable """
try:
api.update_status(tweetvar)
except:
return False
return True
def tweet_image(filename, tweetvar):
""" tweets image with status """
try:
api.update_with_media(filename, tweetvar)
return True
except tweepy.TweepError as e:
print(e.reason)
pass
return False
def retweet_follow(searchterms):
"""searches tweets with searchterms, retweets, then follows"""
for tweet in tweepy.Cursor(api.search, q=searchterms).items(60):
try:
tweet.retweet()
if not tweet.user.following:
tweet.user.follow()
return True
except tweepy.TweepError as e:
print(e.reason)
pass
return False
def follow_x(searchterms, xfollowers):
"""follow ten new followers based on given searchterms"""
retval = False
for x in range(xfollowers):
for tweet in tweepy.Cursor(api.search, q=searchterms).items(60):
try:
if not tweet.user.following:
tweet.user.follow()
retval = True
break
except tweepy.TweepError as e:
print(e.reason)
pass
return retval
def auto_retweet(searchterms, seconds, iterations):
""" searches for tweets, attempts to retweet, follows, and repeat """
for x in range(iterations):
for tweet in tweepy.Cursor(api.search, q=searchterms).items(60):
try:
tweet.retweet()
if not tweet.user.following:
tweet.user.follow()
break
except tweepy.TweepError as e:
print(e.reason)
pass
sleep(seconds)
def auto_tweet_file(filename, seconds):
""" runs loop through all lines in text file and tweets them """
fout = open(filename, 'r')
file_lines = fout.readlines()
fout.close()
for line in file_lines:
try:
if line != '\n':
if censor(line):
api.update_status(line)
else:
pass
except tweepy.TweepError as e:
print(e.reason)
pass
sleep(seconds)
def follow_followers():
""" follow all your followers """
for follower in tweepy.Cursor(api.followers).items():
are_following = follower.__dict__.get('following')
request_sent = follower.__dict__.get('follow_request_sent')
try:
if not are_following and not request_sent:
follower.follow()
except tweepy.TweepError as e:
print(e.reason)
pass
# begin flask template rendering
@app.route('/', methods=['GET', 'POST'])
def index():
"""renders main index page"""
if request.method == 'GET':
return render_template('index.html', newstring="none")
if request.method == 'POST':
tweetvar = request.form['tweet']
if not censor(tweetvar):
return redirect(url_for('confirmtweet', tweetvar="profanity"))
if request.form['translate'] == "ascii":
tweetvar = translate(tweetvar, 'a')
elif request.form['translate'] == "leet":
tweetvar = translate(tweetvar, 'l')
if request.form['action'] == 'translate':
return render_template('index.html', newstring=tweetvar)
else:
if request.files['file']:
file = request.files['file']
if allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = os.path.join(UPLOAD_FOLDER, filename)
filename = add_date(filename)
file.save(filename)
if tweet_image(filename, tweetvar):
return redirect(url_for('confirmtweet',
tweetvar=tweetvar))
elif tweet_text(tweetvar):
return redirect(url_for('confirmtweet', tweetvar='party time'))
return redirect(url_for('confirmtweet', tweetvar='failure'))
@app.route('/confirmtweet/<tweetvar>', methods=['GET'])
def confirmtweet(tweetvar):
"""renders confirmation tweet"""
return render_template('confirmtweet.html', tweetvar=tweetvar)
@app.route('/features', methods=['GET', 'POST'])
def features():
if request.method == 'GET':
return render_template('features.html')
if request.method == 'POST':
failcount = 0
searchterms = (request.form['searchterms']
if request.form['searchterms'] else '#opensource')
seconds = float(request.form['seconds'] if request.form['seconds']
else 86400)
iterations = int(request.form['iterations']
if request.form['iterations'] else 3)
xfollowers = int(request.form['xfollowers']
if request.form['xfollowers'] else 3)
if not censor(searchterms):
return redirect(url_for('confirmtweet', tweetvar='profanity'))
try:
if request.form['retweet']:
if retweet_follow(searchterms) is not True:
failcount += 1
except:
failcount += 1
try:
if request.form['followfollowers']:
follow_followers()
except:
failcount += 1
try:
if request.form['followx']:
if follow_x(searchterms, xfollowers) is False:
failcount += 1
except:
failcount += 1
try:
if request.form['autotweet'] and request.files['file']:
file = request.files['file']
if allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = os.path.join(UPLOAD_FOLDER, filename)
filename = add_date(filename)
file.save(filename)
p = multiprocessing.Process(target=auto_tweet_file,
args=(filename, seconds))
p.start()
else:
failcount += 1
except:
failcount += 1
try:
if request.form['autoretweet']:
p = multiprocessing.Process(target=auto_retweet,
args=(searchterms, seconds,
iterations))
p.start()
except:
failcount += 1
if failcount >= 5:
return redirect(url_for('confirmfeature', status='failure'))
else:
return redirect(url_for('confirmfeature', status='success'))
@app.route('/confirmfeature/<status>', methods=['GET'])
def confirmfeature(status):
return render_template('confirmfeature.html', status=status)
@app.route('/are-no-one', methods=['GET'])
def easteregg():
return render_template('easteregg.html')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
if __name__ == "__main__":
app.run(host='0.0.0.0', port=port, debug=True)