diff --git a/instaloader/instaloader.py b/instaloader/instaloader.py index 9b9574b..62652e1 100644 --- a/instaloader/instaloader.py +++ b/instaloader/instaloader.py @@ -926,7 +926,7 @@ class Instaloader: self.download_hashtag_profilepic(hashtag) if posts: self.context.log("Retrieving pictures with hashtag #{}...".format(hashtag.name)) - self.posts_download_loop(hashtag.get_posts(), target, fast_update, post_filter, + self.posts_download_loop(hashtag.get_all_posts(), target, fast_update, post_filter, max_count=max_count) if self.save_metadata: json_filename = '{0}/{1}'.format(self.dirname_pattern.format(profile=target, diff --git a/instaloader/structures.py b/instaloader/structures.py index 0af9c80..87d9fba 100644 --- a/instaloader/structures.py +++ b/instaloader/structures.py @@ -1267,6 +1267,31 @@ class Hashtag: conn = data["edge_hashtag_to_media"] yield from (Post(self._context, edge["node"]) for edge in conn["edges"]) + def get_all_posts(self) -> Iterator[Post]: + """Yields all posts, i.e. all most recent posts and the top posts, in chronological order.""" + sorted_top_posts = iter(sorted(self.get_top_posts(), key=lambda p: p.date_utc, reverse=True)) + other_posts = self.get_posts() + next_top = next(sorted_top_posts, None) + next_other = next(other_posts, None) + while next_top is not None or next_other is not None: + if next_other is None: + yield from sorted_top_posts + break + if next_top is None: + yield from other_posts + break + if next_top == next_other: + yield next_top + next_top = next(sorted_top_posts, None) + next_other = next(other_posts, None) + continue + if next_top.date_utc > next_other.date_utc: + yield next_top + next_top = next(sorted_top_posts, None) + else: + yield next_other + next_other = next(other_posts, None) + class TopSearchResults: """ diff --git a/test/instaloader_unittests.py b/test/instaloader_unittests.py index faeb5ba..4dc1b3c 100644 --- a/test/instaloader_unittests.py +++ b/test/instaloader_unittests.py @@ -73,7 +73,7 @@ class TestInstaloaderAnonymously(unittest.TestCase): self.L.download_hashtag(HASHTAG, NORMAL_MAX_COUNT) def test_hashtag_paging(self): - for count, post in enumerate(instaloader.Hashtag.from_name(L.context, HASHTAG).get_posts()): + for count, post in enumerate(instaloader.Hashtag.from_name(self.L.context, HASHTAG).get_all_posts()): print(post) if count == PAGING_MAX_COUNT: break