Indra Indra - 8 months ago 54
MySQL Question

self.m StreamListener Tweepy in Flask doesn't get the textbox value

I tried using the code below but it doesn't work. when I press Fetch Tweets button it won't pass value from the textbox name number from HTML page to self.m and keep streaming all the tweets and store them all into database.

def index():
return render_template('fetchtweetpage.html')

@fetchtweet.route('/fetchtrainingtweets/', methods=["GET", "POST"])
def ttweets():
if request.method == 'POST':
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
api = tweepy.API(auth)
getlabelid = Label.query.filter_by(label_id=request.form['label']).first()
getlabelname = getlabelid.label_name
number = request.form['number']

class listener(StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.n = 0
self.m = number

def on_data(self, data):
all_data = json.loads(data)
tweet = all_data["text"]
username = all_data["user"]["screen_name"]
label = getlabelid.label_id
ttweets = TrainingTweets(label_id=label, tweet_username=username, tweet=tweet)
# Increment the counter here, as we've truly successfully
# stored a tweet.
self.n += 1

except IntegrityError:
# Don't stop the stream, just ignore the duplicate.
# print("Duplicate entry detected!")
if self.n >= self.m:
#print("Successfully stored", self.m, "tweets into database")
# Cross the... stop the stream.
return False

# Keep the stream going.
return True

def on_error(self, status):

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=[getlabelname], languages=["en"])
return render_template('fetchtrainingtweets.html',
return render_template('fetchtrainingtweets.html',

I want to know if I can pass the value from textbox name number from HTML page to self.m StreamListener Tweepy in Flask. Is it possible or? because I pass the value to self.m but it doesn't work. it will work only if I set the value of self.m by myself.


You are trying to compare a string to an int. In Python 2, strings are always greater than numbers.

All input values in HTML, no matter what the type of the input, are strings. So all the data Flask sees in the form data is strings. You have to convert to a number yourself.

number = request.form.get('number', type=int)