Skip to content

Instantly share code, notes, and snippets.

@BexTuychiev
Last active July 17, 2020 09:20
Show Gist options
  • Save BexTuychiev/1beb25d7a66ee42958d230368d231ff9 to your computer and use it in GitHub Desktop.
Save BexTuychiev/1beb25d7a66ee42958d230368d231ff9 to your computer and use it in GitHub Desktop.
def tops_posts_from_hashtag(hashtag_name: str, max_count: int):
"""
A function that downloads top {max_count} posts from a hashtag
"""
# Load the hashtag object into a variable
hashtag = instaloader.Hashtag.from_name(bot.context, hashtag_name)
# Get top posts in a generator
posts = hashtag.get_top_posts()
for index in range(1, max_count + 1):
try:
# Download the post
bot.download_post(next(posts), target=f'{hashtag_name}_{index}')
except:
break # If there are any errors, we break out of the loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment