Last active
October 30, 2019 06:43
-
-
Save aandergr/73140345c68ed7c36d74c92b86dd6271 to your computer and use it in GitHub Desktop.
Instagram - Download posts that match multiple hashtags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Instagram - Download posts that match multiple hashtags | |
See https://github.com/Thammus/instaloader/issues/24 | |
Requires an Instaloader version whose __version__ string begins with '3'. | |
(This code snippet is free and unencumbered software released into the public domain) | |
""" | |
from instaloader import Instaloader | |
def download_posts_with_multiple_hashtags(instaloader, hashtags, max_per_hashtag): | |
posts = [] | |
print("Getting posts with hashtag {}".format(hashtags[0])) | |
for count, post in enumerate(instaloader.get_hashtag_posts(hashtags[0])): | |
posts.append(post) | |
print(count + 1, sep='', end='\r', flush=True) | |
if count >= max_per_hashtag - 1: | |
break | |
for idx, hashtag in enumerate(hashtags[1:]): | |
prev_posts = posts | |
posts = [] | |
print("\nGetting posts with hashtag {} and {}".format(hashtag, | |
','.join(hashtags[:(idx + 1)]))) | |
for count, post in enumerate(instaloader.get_hashtag_posts(hashtag)): | |
if any(p == post for p in prev_posts): | |
posts.append(post) | |
print("{}, {} matching".format(count + 1, len(posts)), sep='', end='\r', | |
flush=True) | |
if count >= max_per_hashtag - 1: | |
break | |
if posts: | |
print("\nDownloading posts with hashtags {}".format(','.join(hashtags))) | |
for count, post in enumerate(posts): | |
print("[{:03d}/{:03d}] ".format(count + 1, len(posts)), end='', flush=True) | |
instaloader.download_post(post, target=','.join(hashtags)) | |
loader = Instaloader(sleep=True, filename_pattern='{date}') | |
try: | |
download_posts_with_multiple_hashtags(loader, | |
hashtags=['starwars', 'disney', 'toys'], | |
max_per_hashtag=2500) | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment