Django With Tweepy

268 views
Skip to first unread message

coded kid

unread,
Mar 27, 2012, 3:59:28 AM3/27/12
to Django users
Hi guys, 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:


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!

Konstantinos Pachnis

unread,
Mar 28, 2012, 3:34:37 PM3/28/12
to django...@googlegroups.com
Hi,

You could use celery (http://celeryproject.org/) and create a task (
with your code) to run periodically.

Oh, one more thing. I don't really understand why you don't use the Django API for your queries.

Cheers,
Konstantinos

March 27, 2012 10:59
Hi guys, 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
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!

Python_Junkie

unread,
Mar 28, 2012, 6:39:02 PM3/28/12
to django...@googlegroups.com
Interesting question.

I would have to think about your larger question, but the smaller issue, is that you need to close the crsr and you need to commit,

I would think that you would not want to make commit automatic, so that your code has a chance to do some error checking and perhaps roll back.

coded kid

unread,
Mar 28, 2012, 7:07:22 PM3/28/12
to django...@googlegroups.com
Thanks guys!  @Pachnis How do you mean by Django API queries?

Python_Junkie

unread,
Mar 28, 2012, 8:56:16 PM3/28/12
to django...@googlegroups.com
I believe he means using the Object Relational Model  ORM, which is an API abstraction for several frameworks.

Whether you use sql or the ORM (you can learn more about the ORM on the django project tutorial) only clouds the issue that you are trying to answer

SQL is SQL whether you make an abstraction layer or not
Reply all
Reply to author
Forward
0 new messages