示例#1
0
def get_media_from_hashtag(tag, media_type, quality, max_images, path):
    instagram = Instagram()
    medias = instagram.get_medias_by_tag(tag, count=max_images)
    count = 1
    for media in medias:
        media.type = 'image' if media.type == 'sidecar' or media.type == 'carousel' else media.type
        # Extracting Image URL
        if (media.type == 'image' and media_type == 'image'
                or media_type == 'all') and not media.is_ad:

            # Get the links form media
            all_quality = ['low', 'standard', 'high']
            url = media.__getattribute__(f"image_{quality}_resolution_url")

            # If the preferred quality is not available
            if not url:
                all_quality.remove(quality)
                for q in all_quality:
                    url = media.__getattribute__(f"image_{q}_resolution_url")
                    if url:
                        break

        # Extracting Video URL
        if (media.type == 'video' and media_type == 'all'
                or media_type == 'video') and not media.is_ad:

            # Get the links form media
            media = instagram.get_media_by_id(media.identifier)
            url = media.video_standard_resolution_url or media.video_low_bandwidth_url or media.video_low_resolution_url or media.video_url

        # Downloading the media
        if url:
            urllib.request.urlretrieve(
                url,
                f"{path}/{media.type}s/{media.type}{count}.{'jpg' if media.type == 'image' else 'mp4'}"
            )
            print(f"{count}/{max_images} media downloaded")
        else:
            print(
                f"[{count}] Failed downloading the media {media.link} (id - {media.identifier})"
            )

        count += 1
from igramscraper.instagram import Instagram

instagram = Instagram()
instagram.with_credentials('username', 'password', 'path/to/cache/folder')
instagram.login()

media = instagram.get_media_by_id('1880687465858169462')

#not optimal to many calls
tagged_users = instagram.get_media_tagged_users_by_code(media.shortCode)

print(tagged_users)
示例#3
0
from igramscraper.instagram import Instagram

# If account is public you can query Instagram without auth
instagram = Instagram()

# If account is private and you subscribed to it, first login
# instagram.with_credentials('username', 'password', 'cachepath')
# instagram.login()

media = instagram.get_media_by_id('1270593720437182847')

print(media)
print('Account info:')
account = media.owner
print('Id', account.identifier)
# print('Username', account.username)
# print('Full Name', account.full_name)
# print('Profile Pic Url', account.get_profile_picture_url_hd())
示例#4
0
def get_media_from_hashtag(tag,
                           media_type,
                           quality,
                           max_images,
                           path,
                           download=False):
    instagram = Instagram()
    medias = instagram.get_current_top_medias_by_tag_name(tag)[:max_images]
    max_images = len(medias)
    count = 1
    for media in medias:

        media = instagram.get_media_by_id(media.identifier)
        comments = instagram.get_media_comments_by_id(
            media.identifier)['comments'] or []
        newline = '\n '
        print(''.join(['-' for _ in range(20)]))
        print(
            f"\n Username: @{media.owner.username}\n Account Link: https://instagram.com/{media.owner.username}\n Post Link: {media.link}\n Likes: {media.likes_count}\n Top Comments: {''.join([comment.text + newline for comment in comments])}"
        )

        if download:
            media.type = 'image' if media.type == 'sidecar' or media.type == 'carousel' else media.type
            # Extracting Image URL
            if (media.type == 'image' and media_type == 'image'
                    or media_type == 'all') and not media.is_ad:

                # Get the links form media
                all_quality = ['low', 'standard', 'high']
                url = media.__getattribute__(f"image_{quality}_resolution_url")

                # If the preferred quality is not available
                if not url:
                    all_quality.remove(quality)
                    for q in all_quality:
                        url = media.__getattribute__(
                            f"image_{q}_resolution_url")
                        if url:
                            break

            # Extracting Video URL
            if (media.type == 'video' and media_type == 'all'
                    or media_type == 'video') and not media.is_ad:

                # Get the links form media
                media = instagram.get_media_by_id(media.identifier)
                url = media.video_standard_resolution_url or media.video_low_bandwidth_url or media.video_low_resolution_url or media.video_url

            # Downloading the media
            if url:
                urllib.request.urlretrieve(
                    url,
                    f"{path}/{media.type}s/{media.type}{count}.{'jpg' if media.type == 'image' else 'mp4'}"
                )
                print(f"{count}/{max_images} media downloaded")
            else:
                print(
                    f"[{count}] Failed downloading the media {media.link} (id - {media.identifier})"
                )

            count += 1
        print(''.join(['-' for _ in range(20)]))