Beispiel #1
0
 def get_network(self, user, max_hits=100):
     
     following = Followers.select_by_id(user.id)
     if following is None or len(following) == 0:
         try:
             following = self.twitter_collector.friends_ids(user_id=user.id)
             max_hits -= 1
             Followers.insert_many(user.id, following)
         except UnauthorizedException:
             print 'failed for user ', user.id
             raise Exception('Unable to collect user network: Not authorized to access user %s.'%user.id)
         
     if max_hits <= 0:
         raise Exception('Unable to collect user network: Rate limit reached.')
     
     friends_following = []
     nfriends = len(following)
     for i, friend_id in enumerate(following):
         
         self.sent_to_user('Collecting information to build a personalized list of topics (%s/%s)'%(i,nfriends))
         
         friend = User.select_by_id(friend_id)
         if friend and (friend.blocked == 'Y' or friend.friends_count == 0):
             logger.debug('Not searching for user: %s', friend_id)
             continue
         following = Followers.select_by_id(friend_id)
         if following is None or len(following) == 0:
             try:
                 print 'Getting followers of', friend_id
                 following = self.twitter_collector.friends_ids(user_id=friend_id)
                 if len(following) == 0:
                     if not friend:
                         friend = User()
                     friend.id = friend_id
                     friend.friends_count = len(following)
                     friend.blocked = 'N'
                     User.add(friend)
                 
                 print 'Got %s followers of %s'%(len(following), friend_id)
                 max_hits -= 1
                 Followers.insert_many(friend_id, following)
             except UnauthorizedException:
                 print 'failed for user ', friend_id
                 User.set_blocked(friend_id, 'Y')
                 following = []
         friends_following.append(following)
         if max_hits <= 0: break
         
     commit()