Advertisement
Guest User

PRAW patch for reliable listing fetch

a guest
Aug 22nd, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.62 KB | None | 0 0
  1. --- a/praw/models/listing/generator.py    0000-00-00 00:00:00.000000000 +0000
  2. +++ b/praw/models/listing/generator,py    0000-00-00 00:00:00.000000000 +0000
  3. @@ -49,6 +49,8 @@
  4.          self.params["limit"] = limit or 1024
  5.          self.url = url
  6.          self.yielded = 0
  7. +        self._counter2 = 0
  8.  
  9.      def __iter__(self) -> Iterator[Any]:
  10.          """Permit :class:`.ListingGenerator` to operate as an iterator."""
  11. @@ -59,7 +61,7 @@
  12.          if self.limit is not None and self.yielded >= self.limit:
  13.              raise StopIteration()
  14.  
  15. -        if self._listing is None or self._list_index >= len(self._listing):
  16. +        while self._listing is None or self._list_index >= len(self._listing):
  17.              self._next_batch()
  18.  
  19.          self._list_index += 1
  20. @@ -84,18 +86,22 @@
  21.  
  22.      def _next_batch(self):
  23.          if self._exhausted:
  24.              raise StopIteration()
  25.  
  26.          self._listing = self._reddit.get(self.url, params=self.params)
  27.          self._listing = self._extract_sublist(self._listing)
  28.          self._list_index = 0
  29.  
  30. -        if not self._listing:
  31. -            raise StopIteration()
  32.  
  33. -        if self._listing.after and self._listing.after != self.params.get(
  34. -            self._listing.AFTER_PARAM
  35. -        ):
  36. -            self.params[self._listing.AFTER_PARAM] = self._listing.after
  37. +        if not self._listing:
  38. +            if self._counter2 > 5:
  39. +                raise StopIteration()
  40. +            self._counter2 += 1
  41.          else:
  42. -            self._exhausted = True
  43. +            self._counter2 = 0
  44. +            self.params[self._listing.AFTER_PARAM] = self._listing[-1].fullname
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement