exception in cursor

69 views
Skip to first unread message

JP

unread,
Dec 26, 2009, 6:59:41 AM12/26/09
to tweepy
Hello all!

I am using tweepy to extract all my friends friends and use the
following code for that:

13 def process(s):
14 l = len(s)
15 p = 0
16 for n in list(s):
17 p += 1
18 if not os.path.isfile(n+'.f'):
19 try:
20 print 'Processing %s (%s/%s)' % (n, p, l)
21 f = open(n+'.f', 'wb')
22 for user in tweepy.Cursor(tweepy.api.friends,
screen_name=n).items():
23 f.write(user.screen_name+'\n')
24 except (tweepy.TweepError, KeyboardInterrupt), e:
25 print str(e)
26 f.close()
27 os.rename(n+'.f', n+'.p')
28 break
29 else:
30 f.close()

I dump results for each user into a separate file. For some users, I
catch a "Not found" TweepError which I don't know how to handle. I'd
like to simply skip that user but wonder how.

Here's the unhandled stack trace of the above code when commenting
lines 24-30 out.

$ ./scanf.py friends.dat
Reading files.
Processing oliviertripet (40/267)
Traceback (most recent call last):
File "./scanf.py", line 57, in <module>
process(s)
File "./scanf.py", line 22, in process
for user in tweepy.Cursor(tweepy.api.friends, screen_name=n).items
():
File "build/bdist.cygwin-1.5.25-i686/egg/tweepy/cursor.py", line
109, in next
File "build/bdist.cygwin-1.5.25-i686/egg/tweepy/cursor.py", line 60,
in next
File "build/bdist.cygwin-1.5.25-i686/egg/tweepy/binder.py", line
167, in _call
tweepy.error.TweepError: Not found

Thanks in advance for any suggestion!

JP

JP

unread,
Jan 4, 2010, 3:32:26 AM1/4/10
to tweepy
No replies yet - I just wanted to add that the Not found comes back
from the Twitter API and might correspond to a deleted user - why
crash the entire cursor? I would like to be able to resume scanning of
a person's friends list and not be bothered by a Not found error.

Any ideas?

Josh Roesslein

unread,
Jan 4, 2010, 1:31:02 PM1/4/10
to twe...@googlegroups.com
Hello JP,

Sorry for the delayed response, been away during the holidays.

I am guessing you might have invalid screen names in your list "s" you
pass into process().
So when you go to page through the friends, twitter is sending a 404
not found response
back when the Cursor goes to fetch the first page of results. There is
no way to recover from this
since that user does not exist any more.

What you need to do is skip the screen name in the list and just go
onto the next one.
If the user does not exist there's no friends to be fetched, so it is
not recoverable.

Hope the helps you out.

Josh

JP

unread,
Jan 5, 2010, 5:42:59 AM1/5/10
to tweepy
Thanks Josh! This helped - I changed the code accordingly and am able
now to set aside problematic accounts.
The graph starts now to build up nicely using graphviz. I will first
complete the processing of my friend list and experiment with an R
package called SNA.

All the best for 2010!!

JP

> > Any ideas?- Hide quoted text -
>
> - Show quoted text -

JP

unread,
Jan 5, 2010, 5:58:54 AM1/5/10
to tweepy
Ooops - Josh I noticed something: the list containing the screen name
to be skipped is not 's' but those contained in the individual files
generated based on 's' contents. So I am catching an exception raised
from the inner for loop with the tweepy.Cursor(tweepy.api.friends,
screen_name=n).items() statement. I don't know if I can really catch
and continue on that. Or can I?

On Jan 4, 7:31 pm, Josh Roesslein <jroessl...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages