alexis-mignon / python-flickr-api

A python implementation of the Flickr API
BSD 3-Clause "New" or "Revised" License
367 stars 108 forks source link

Walker's next() needs to handle empty page returned from Flickr service #100

Open chcao052 opened 5 years ago

chcao052 commented 5 years ago

I am not sure why Flickr service returns multiple empty pages inside next() with the following code.

w = flickr_api.Walker(flickr_api.Photo.search, text="sydney night", license="8,9,10", media="photos")
for i, photo in enumerate(w):
     <download the photo>

It crashed due to accessing empty list at around photo 728 Here is my fix:

  def next(self):
        if self._curr_index == len(self._curr_list):
            if self._page < self._info.pages:
                while self._page < self._info.pages:
                    self._page += 1
                    self.kwargs["page"] = self._page
                    self._curr_list = self.method(*self.args, **self.kwargs)
                    self._info = self._curr_list.info
                    self._curr_index = 0
                    if len(self._curr_list) == 0:
                        continue        # here
                    break
                if len(self._curr_list) == 0:
                    raise StopIteration()
            else:
                raise StopIteration()
        curr = self._curr_list[self._curr_index]
        self._curr_index += 1
        return curr