4

This code is working for downloading all photos and videos

from instaloader import Instaloader, Profile

L = Instaloader()
PROFILE = "username"
profile = Profile.from_username(L.context, PROFILE)

posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda post: post.likes,reverse=True)

for post in posts_sorted_by_likes:
L.download_post(post, PROFILE)

Now i want to download only videos, But I can't. How can I filter this code for only videos?

Rasedul Islam
  • 77
  • 1
  • 9

2 Answers2

3

Post has an is_video property

for post in posts_sorted_by_likes:
    if post.is_video:
        L.download_post(post, PROFILE)
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
1

Sometimes the 'is_video' is not enough, so you can just delete the ones that don't have the .mp4 extension:

for file in os.listdir(path):
        if not file.endswith(".mp4"):
            os.remove(path + file)
Tomas1906
  • 7
  • 3
  • That's great it's working. I want to know how can I download only images without videos? – Rasedul Islam Mar 29 '22 at 19:03
  • Maybe try: if not post.is_video: download . But again, I'm not sure it worked perfectly. You can always download everything and the clean it with the function above, but instead of 'endswith(".mp4") you use 'endswith(".jpg") – Tomas1906 Mar 31 '22 at 00:57