Steve Losh avatar Steve Losh committed cf9b7f8

Add generators.

Comments (0)

Files changed (2)

     for shot in steve.shots_following():
         print '    ', shot.title, shot.url
 
+    simplebits = d.player('simplebits')
+
+    print 'All shots from @simplebits:'
+    for shot in simplebits.ishots():
+        print '%8d: %s' % (shot.id, shot.title)
+
 Documentation
 =============
 
 * `avatar_url`: The URL of the player's avatar image.
 * `location`: The location of the player, if they've listed one.
 * `shots()`: A list containing the player's latest shots. You can pass `page`
+  and/or `per_page` to control pagination.
 * `shots_following()`: A list containing the latest Shots of the players this
   player is following. You can pass `page` and/or `per_page` to control
   pagination.
+* `ishots()`: A generator that yields shots from this player, newest to oldest.
+  It will hit Dribbble's API whenever it needs more shots, so be careful not to
+  get rate limited (i.e. don't try to take more than ~1,000 shots per minute).
+  You can pass `start_page` to start at a certain page.
+* `ishots_following()`: A generator that yields shots from players this player
+  is following, newest to oldest.  It will hit Dribbble's API whenever it needs
+  more shots, so be careful not to get rate limited (i.e. don't try to take
+  more than ~1,000 shots per minute). You can pass `start_page` to start at
+  a certain page.
 
 Shot
 ----
 
 
 def _api(url, id, pagination=None):
-    print url
     if pagination:
         query = '?' + urllib.urlencode(zip(('page', 'per_page'), pagination))
-        print query
     else:
         query = ''
 
-    print API_URL + (url % id) + query
-
     u = urllib2.urlopen(API_URL + (url % id) + query)
     return json.loads(u.read())
 
+def _i(f, start_page, **kwargs):
+    i = start_page
+    l = f(page=start_page, per_page=30, **kwargs)
+    seen = set([item.id for item in l])
+
+    while l:
+        yield l.pop(0)
+        if not l:
+            i += 1
+            items = f(page=i, per_page=30, **kwargs)
+            ids = set([item.id for item in items])
+            l.extend([item for item in items if item.id not in seen])
+            seen.update(ids)
+
+
 class Dribbble(object):
     def __init__(self):
         pass
         data = _api('shots/%s', typ, (page, per_page))
         return [Shot(sd) for sd in data]
 
+    def ishots(self, typ='everyone', start_page=1):
+        return _i(self.shots, start_page, typ=typ)
 
 class Shot(object):
     '''A single shot.
         return [Shot(sd) for sd in data]
 
 
+    def ishots(self, start_page=1):
+        return _i(self.shots, start_page)
+
+    def ishots_following(self, start_page=1):
+        return _i(self.shots_following, start_page)
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.