Commits

Brad Montgomery committed 6c2f9c2 Merge

branch merge

  • Participants
  • Parent commits e0c262a, b1bc933
  • Branches nosetests

Comments (0)

Files changed (1)

             search_string = u"%s / %s (fork of" % (self.username, self.slug)
             return self.scraped_content.decode("utf8").find(search_string) > 0
     
-    def contributors(self, limit=50):
+    def contributors(self, limit=50, sleep_after=None, sleep_for=None):
         """
         NOTE: This method is not very nice to the bitbucket API. 
             
         Github's ``contributors``. To do this, we look for data in all of a 
         project's changesets, and ``limit`` controls how many of those we 
         query at once.
+
+        The ``sleep_after`` parameter instructs this method to sleep for
+        ``sleep_for`` seconds after ``sleep_after``-many calls to the api.
+        Both of these parameters must be provided for this to work... (yeah, I know)
+
+        >>> repo.contributors(sleep_after=5, sleep_for=3)
+
+        The above example will sleep for 3 seconds after every 5th call to the 
+        changesets API.
         
         This method returns a dictionary of the form:
 
                     'contributions':1,
                 }
 
+        api_hit_count = 0
         # Do this for every "page" of the changsets... 
         start = None
         remaining_changesets = 1
         retrieved_changesets = 0
         while remaining_changesets > 0:
             changesets = self.changesets(limit=limit, start=start)  # initial group of changesets
+            if start:
+                start = start + limit + 1
+            else:
+                start = limit + 1
+            api_hit_count += 1 # see how many times we've hit the api
             for cset in changesets.get('changesets', []):
                 _update_contributors(cset, contributors)
             retrieved_changesets += len(changesets.get('changesets', []))
-            start = retrieved_changesets + (changesets.get('start', None) or 0)
             remaining_changesets = changesets['count'] - retrieved_changesets
-            
+            if remaining_changesets < start:
+                start = changesets['count'] - remaining_changesets
+
+            if sleep_after and sleep_for and api_hit_count > 0 and api_hit_count % sleep_after == 0:
+                time.sleep(sleep_for)
+        
         #TODO sort based on contributions, like github does?
         for k,v in contributors.items():
             v.update({'name':k})