Custom User-Agent Header

147 views
Skip to first unread message

Doza

unread,
Feb 7, 2010, 11:59:08 AM2/7/10
to tweepy
Hello,

I have been using Tweepy for a few weeks now. This is an excellent
project!

The problem that I am running into has to deal with Twitter's Search
API. My application needs to perform searches for hundreds of terms,
so I've written a wrapper that makes no more than N calls per hour
(currently N=100) using Tweepy's Cursor.

I keep running into rate-limit errors after only a few minutes. I
think the problem might have to do with the User-Agent header that I
am passing to the service. I've found a few references to this
online, so I am hoping that it might solve my problem as well.

How can I set a custom User-Agent header using Tweepy? For instance,
given the following example, how can I add the header to the call?

def search(term,sinceid=0,max=1500):
customheader = 'MyApplication v0.16a'
results = tweepy.Cursor(tweepy.api.search, q=term,
since_id=sinceid).items(max)
return results

Thanks for any help!

Josh Roesslein

unread,
Feb 7, 2010, 12:11:22 PM2/7/10
to twe...@googlegroups.com
Hello,

You can provide additional headers with any request by passing a karg named "headers".
Here is an example:

api.get_user('twitter', headers={'User-Agent': 'MyUserAgent'})

That should also work with cursors like this:

Cursor(api.get_user, 'twitter', headers={'User-Agent': 'MyUserAgent'})

Hope that helps you out. Thanks for using Tweepy!

Josh

Doza

unread,
Feb 7, 2010, 1:04:59 PM2/7/10
to tweepy
Thanks, Josh! That's perfect. Seems to have solved the problem.

On Feb 7, 12:11 pm, Josh Roesslein <jroessl...@gmail.com> wrote:
> Hello,
>
> You can provide additional headers with any request by passing a karg named
> "headers".
> Here is an example:
>
> api.get_user('twitter', headers={'User-Agent': 'MyUserAgent'})
>
> That should also work with cursors like this:
>
> Cursor(api.get_user, 'twitter', headers={'User-Agent': 'MyUserAgent'})
>
> Hope that helps you out. Thanks for using Tweepy!
>
> Josh
>

Doza

unread,
Feb 10, 2010, 11:03:37 PM2/10/10
to tweepy
Hi Josh,

Is there a similar way to add headers to the Streaming class? I've
tried it a couple of ways but I keep getting errors.

For example, in streamwatcher.py, I thought I would be able to do the
following:
stream = tweepy.Stream(username, password, StreamWatcherListener(),
timeout=None, headers={'User-Agent': 'MyUserAgent'})

But I get an unexpected keyword argument error. Can you let me know
what I am doing incorrectly?

Thanks

On Feb 7, 12:11 pm, Josh Roesslein <jroessl...@gmail.com> wrote:

> Hello,
>
> You can provide additional headers with any request by passing a karg named
> "headers".
> Here is an example:
>
> api.get_user('twitter', headers={'User-Agent': 'MyUserAgent'})
>
> That should also work with cursors like this:
>
> Cursor(api.get_user, 'twitter', headers={'User-Agent': 'MyUserAgent'})
>
> Hope that helps you out. Thanks for using Tweepy!
>
> Josh
>

Josh Roesslein

unread,
Feb 10, 2010, 11:31:26 PM2/10/10
to twe...@googlegroups.com
Sorry I forgot to expose the headers in the Stream constructor.
I just pushed a commit that allows you to pass in headers now.
Stream(headers={}) should work now.

Josh

Mike Cardosa

unread,
Feb 11, 2010, 12:29:35 AM2/11/10
to twe...@googlegroups.com
You are quick! Thanks!

Brainewave Consulting

unread,
Dec 14, 2010, 8:38:00 AM12/14/10
to twe...@googlegroups.com

Is there a way to get since_id out of a SearchResult, and I'm just not figuring out how to do it?


thanks!

Mike Caprio
Principal and Lead Consultant

Brainewave Consulting
402 Graham Avenue PMB 211
Brooklyn, NY  11211


Reply all
Reply to author
Forward
0 new messages