class TestIgramscraper(unittest.TestCase):

    @classmethod
    def setUpClass(self):

        cwd = os.getcwd()
        session_folder = cwd + os.path.sep + 'sessions' + os.path.sep
        if username == None or password == None:
            self.instagram = Instagram()
        else:
            self.instagram = Instagram()
            self.instagram.with_credentials(username, password, session_folder)
            self.instagram.login()
        
        if user_agent != None:
            #TODO set user agent
            pass

    @classmethod
    def tearDownClass(self):
        pass

    def test_get_account_by_username(self):
        account = self.instagram.get_account('kevin')
        self.assertEqual('kevin', account.username)
        self.assertEqual('3', account.identifier)
    
    def test_get_account_by_id(self):
        account = self.instagram.get_account_by_id(3)
        self.assertEqual('kevin', account.username)
        self.assertEqual('3', account.identifier)
    
    def test_get_medias(self):
        medias = self.instagram.get_medias('kevin', 80)
        self.assertEqual(80, len(medias))

    def test_get_hundred_medias(self):
        medias = self.instagram.get_medias('kevin', 100)
        self.assertEqual(100, len(medias))

    def test_get_medias_by_tag(self):
        medias = self.instagram.get_medias_by_tag('youneverknow', 20)
        self.assertEqual(20, len(medias))

    def test_get_medias_by_code(self):
        media = self.instagram.get_medias_by_code('BHaRdodBouH')
        self.assertEqual('kevin', media.owner.username)

    def test_get_media_by_url(self):
        media = self.instagram.get_media_by_url('https://www.instagram.com/p/BHaRdodBouH')
        self.assertEqual('kevin', media.owner.username)

    def test_get_account_by_id_with_invalid_numeric_id(self):
        # sys.maxsize is far larger than the greatest id so far and thus does not represent a valid account.
        try:
            self.instagram.get_account_by_id(sys.maxsize)
        except Exception as e:
            self.assertEqual(str(e), 'Failed to fetch account with given id')

    def test_get_location_top_medias_by_id(self):
        medias = self.instagram.get_current_top_medias_by_tag_name(1)
        self.assertEqual(9, len(medias))

    def test_get_location_medias_by_id(self):
        medias = self.instagram.get_medias_by_location_id(1, 56)
        self.assertEqual(56, len(medias))

    def test_get_location_by_id(self):
        location = self.instagram.get_location_by_id(1)
        self.assertEqual('Dog Patch Labs', location.name)

    def test_get_id_from_code(self):
        code = Media.get_code_from_id('1270593720437182847')
        self.assertEqual('BGiDkHAgBF_', code)
        code = Media.get_code_from_id('1270593720437182847_3')
        self.assertEqual('BGiDkHAgBF_', code)
        code = Media.get_code_from_id(1270593720437182847)
        self.assertEqual('BGiDkHAgBF_', code)
    
    def test_get_code_from_id(self):
        id = Media.get_id_from_code('BGiDkHAgBF_')
        self.assertEqual(1270593720437182847, id)
    
    
    def test_get_media_comments_by_code(self):
        comments = self.instagram.get_media_comments_by_code('BR5Njq1gKmB', 40)
        #TODO: check why returns less comments
        self.assertLessEqual(40, len(comments))

    def test_get_username_by_id(self):
        username = self.instagram.get_username_by_id(3)
        self.assertEqual('kevin', username)
    
    def test_get_medias_by_user_id(self):
        medias = self.instagram.get_medias_by_user_id(3)
        self.assertEqual(12, len(medias))
from igramscraper.instagram import Instagram
from firebase import firebase
instagram = Instagram()
firebase = firebase.FirebaseApplication(
    'https://covidai-1dd78.firebaseio.com/', None)
data = firebase.get(
    '/covidai-1dd78/latest_media/-M3f_ZqzLKNGLoqFP5Mr/-M3gWKCfYoXQpB0gbGJh',
    '')
media_ids = data['medias']
# authentication supported
instagram.with_credentials('covid.ai_bengali', 'CoronaCann09')
instagram.login()
# datas = firebase.post('/covidai-1dd78/latest_media/-M3f_ZqzLKNGLoqFP5Mr/-M3gWKCfYoXQpB0gbGJh', da)
media = instagram.get_current_top_medias_by_tag_name('corona')
print(media)
for m in media:
    if m.identifier not in media_ids:
        media_ids.append(m.identifier)
        comment = instagram.add_comment(
            m.identifier, 'Follow @covid.ai for the latest coronavirus stats')
        print(comment)
result = firebase.put(
    '/covidai-1dd78/latest_media/-M3f_ZqzLKNGLoqFP5Mr/-M3gWKCfYoXQpB0gbGJh',
    'medias', media_ids)
print(result)
示例#3
0
from igramscraper.instagram import Instagram

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

medias = instagram.get_current_top_medias_by_tag_name('youneverknow')
media = medias[0]

print(media)
print(media.owner)
示例#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)]))