I’m confused on how to make this work. I want to stream user’s tweets in my django app using tweepy. I’ve written the streaming code but the problem I’m facing is: should I paste the code in views.py and input-
return render_to_response('tweet.html',context_instance=RequestContext(request))
after writing the code. Just like this:
import sys
import tweepy
import webbrowser
import MySQLdb
Q= sys.argv[1:]
db=MySQLdb.connect("localhost","","","Juzme")
auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
cur=db.cursor()
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
print "%s\t%s\t%s\t%s" % (status.text,
status.author.screen_name,
status.created_at,
status.source,)
cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %s)", (status.text,
status.author.screen_name,
status.created_at,
status.source))
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
streaming_api=tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)
streaming_api.filter(follow=[], track=Q)
return render_to_response('tweet.html',context_instance=RequestContext(request))
If I can do it like this, won’t there be any code in template? Or what’s the best way I can carry out this operation. I hope you get my point? Thanks