def test_length_hint_empty(self): looter = instaLooter.InstaLooter(profile="jkshksjdhfjkhdkfhk") self.assertEqual(operator.length_hint(looter), 0) looter = instaLooter.InstaLooter(hashtag="jkshksjdhfjkhdkfhk") self.assertEqual(operator.length_hint(looter), 0)
def test_dump_json(self): looter = instaLooter.InstaLooter( self.tmpdir, profile="instagram", dump_json=True, ) test_medias = list( itertools.islice((m for m in looter.medias() if not m['is_video']), 3)) looter.download(media_count=3) # Check all files were downloaded as expected self.assertEqual( sorted(os.listdir(self.tmpdir)), sorted(f for media in test_medias for f in ( str("{}.jpg").format(media['id']), str("{}.json").format(media['id']), ))) # Check the metadata are OK for media in test_medias: with open( os.path.join(self.tmpdir, "{}.json").format(media['id'])) as f: dump = json.load(f) self.assertMediaEqual(media, dump)
def test_pr_122(self): """ Feature implemented by @susundberg. Set the access time and modification time of a downloaded media according to its IG date. """ looter = instaLooter.InstaLooter( self.tmpdir, profile='franz_ferdinand', template='{code}') # Test download_post info = looter.get_post_info('BY77tSfBnRm') looter.download_post('BY77tSfBnRm') stat = os.stat(os.path.join(self.tmpdir, 'BY77tSfBnRm.jpg')) self.assertEqual(stat.st_atime, info['date']) self.assertEqual(stat.st_mtime, info['date']) # Test download_pictures pic = next(m for m in looter.medias() if not m['is_video']) looter.download_pictures(media_count=1) stat = os.stat(os.path.join(self.tmpdir, '{}.jpg'.format(pic['code']))) self.assertEqual(stat.st_atime, pic['date']) self.assertEqual(stat.st_mtime, pic['date']) # Test download_videos vid = next(m for m in looter.medias() if m['is_video']) looter.download_videos(media_count=1) stat = os.stat(os.path.join(self.tmpdir, '{}.mp4'.format(vid['code']))) self.assertEqual(stat.st_atime, vid['date']) self.assertEqual(stat.st_mtime, vid['date'])
def test_extended_dump(self): looter = instaLooter.InstaLooter( self.tmpdir, profile="instagram", dump_only=True, extended_dump=True, ) test_medias = list( itertools.islice((m for m in looter.medias() if not m['is_video']), 3)) looter.download(media_count=3) # Check all files were downloaded as expected self.assertEqual( sorted(os.listdir(self.tmpdir)), sorted( str("{}.json").format(media['id']) for media in test_medias)) # Check the metadata are OK for media in test_medias: with open( os.path.join(self.tmpdir, "{}.json").format(media['id'])) as f: dump = json.load(f) self.assertMediaEqual(media, dump) # Check the dump was "extended" self.assertIn('edge_media_to_comment', dump) self.assertIn('edge_media_to_caption', dump)
def test_proxy_setup(self): looter = instaLooter.InstaLooter(socks_port=9090) self.assertTrue(hasattr(looter, 'tor_manager')) self.assertTrue( str(looter.session.proxies) == str(looter.tor_manager.proxies)) self.assertTrue( isinstance(looter.session.hooks.get('response', None), type(looter.tor_manager.call_for_new_ip)))
def test_issue_6(self): """ Checks that instaLooter does not iterate forever on a private profile. """ looter = instaLooter.InstaLooter(self.tmpdir, profile="tldr") with self.assertRaises(StopIteration): next(looter.medias())
def _test(self): looter = instaLooter.InstaLooter(self.tmpdir, profile=profile, get_videos=True) looter.download(media_count=200) self.assertEqual(len(os.listdir(self.tmpdir)), min(200, int(looter.metadata['media']['count']))) self.assertEqual(profile, looter.metadata['username'])
def test_issue_41(self): """ Feature request by @liorlior Allow downloading only videos. """ looter = instaLooter.InstaLooter(self.tmpdir, profile="nintendo", videos_only=True) looter.download(timeframe=(datetime.date(2017, 3, 10), )*2) self.assertEqual(os.listdir(self.tmpdir), ["1467639884243493431.mp4"])
def test_template_1(self): PROFILE = "therock" looter = instaLooter.InstaLooter(self.tmpdir, profile=PROFILE, get_videos=True, template='{username}') looter.download(media_count=30, with_pbar=True) for f in os.listdir(self.tmpdir): self.assertTrue(f.startswith(PROFILE))
def test_template_1(self): profile = "therock" looter = instaLooter.InstaLooter(self.tmpdir, profile=profile, get_videos=True, template='{username}-{id}') looter.download(media_count=self.MEDIA_COUNT, with_pbar=False) for f in os.listdir(self.tmpdir): self.assertTrue(f.startswith(profile))
def test_resizer(self): self.looter = instaLooter.InstaLooter(self.tmpdir, url_generator=resizer(320), profile="instagram") self.looter.download(media_count=self.MEDIA_COUNT) for img_file in glob.iglob(os.path.join(self.tmpdir, "*.jpg")): with PIL.Image.open(img_file) as img: width, height = img.size self.assertEqual(max(width, height), 320)
def test_issue_12(self): """ Feature request by @paramjitrohit. Allows downloading pictures and videos only within a timeframe. """ looter = instaLooter.InstaLooter(self.tmpdir, profile="slotfaceofficial") timeframe = (datetime.date(2017, 2, 18),)*2 medias_in_timeframe = list(looter.medias(timeframe=timeframe)) self.assertEqual(len(medias_in_timeframe), 2)
def test_thumbnail(self): self.looter = instaLooter.InstaLooter(self.tmpdir, url_generator=thumbnail, profile="therock") self.looter.download(media_count=self.MEDIA_COUNT) for img_file in glob.iglob(os.path.join(self.tmpdir, "*.jpg")): with PIL.Image.open(img_file) as img: width, height = img.size self.assertEqual(width, height)
def test_issue_26(self): """ Feature request by @verafide. Checks that pictures that are downloaded are not resized. """ looter = instaLooter.InstaLooter(self.tmpdir) looter.download_post("BO0XpEshejh") filename = "1419863760138791137.jpg" pic = PIL.Image.open(os.path.join(self.tmpdir, filename)) self.assertEqual(pic.size, (525, 612))
def test_length_hint(self): looter = instaLooter.InstaLooter(self.tmpdir, profile="tide") hint = operator.length_hint(looter) # Check the post count is greater than 0 self.assertGreater(hint, 0) # Download pictures and check if the count # match (at most as many posts downloaded) looter.download() self.assertLessEqual(len(os.listdir(self.tmpdir)), hint)
def test_ip_changeability(self): def get_ip(proxies): r = requests.get('https://api.ipify.org?format=json', proxies=proxies) return json.loads(r.text)['ip'] looter = instaLooter.InstaLooter(socks_port=9090, change_ip_after=1) old_ip = get_ip(looter.tor_manager.proxies) looter.session.get('https://www.instagram.com/') new_ip = get_ip(looter.tor_manager.proxies) self.assertTrue(old_ip != new_ip)
def _test(self): looter = instaLooter.InstaLooter(self.tmpdir, profile=profile, get_videos=True) looter.download(media_count=200) # We have to use GreaterEqual since multi media posts # are counted as 1 but will download more than one # picture / video self.assertGreaterEqual( len(os.listdir(self.tmpdir)), min(200, int(looter.metadata['media']['count']))) self.assertEqual(profile, looter.metadata['username'])
def test_issue_76(self): """ Thanks to @zeshuaro for reporting this bug. Check that when downloading hashtags, the downloader actually stops. """ looter = instaLooter.InstaLooter(self.tmpdir, hashtag="oulianov") postcount = looter.__length_hint__() # operator.length_hint for i, m in enumerate(looter.medias()): if i > postcount: self.fail("looter.medias() did not stop.")
def test_issue_15(self): """ Feature request by @MohamedIM. Checks that videos are not downloaded several times if present already in the destination directory. """ looter = instaLooter.InstaLooter(self.tmpdir, profile="instagram") looter.download_videos(media_count=1) video_file = next(glob.iglob(os.path.join(self.tmpdir, '*.mp4'))) mtime = os.stat(video_file).st_mtime looter.download_videos(media_count=1) #, new_only=True) self.assertEqual(mtime, os.stat(video_file).st_mtime)
def test_issue_94(self): """ Thanks to @jeanmarctst for raising this issue. Make sure caption is properly extracted from images downloaded from a post code and written to the metadata. """ looter = instaLooter.InstaLooter( self.tmpdir, add_metadata=True, template='{code}') looter.download_post('BY77tSfBnRm') metadata = piexif.load( os.path.join(self.tmpdir, 'BY77tSfBnRm.jpg'), True) self.assertTrue(metadata['Exif']['UserComment'])
def test_issue_44(self): """ Thanks to @Bangaio64 for reporting this bug. Checks that warn_windows does not trigger an exception. """ import instaLooter.utils warnings.showwarning = instaLooter.utils.warn_windows looter = instaLooter.InstaLooter( directory=self.tmpdir, profile="akjhdskjhfkjsdhfkjhdskjhfkjdshkfjhsdkjfdhkjdfshdfskhfd") try: looter.download() except Exception: self.fail()
def test_issue_66(self): """ Thanks to @douglasrizzo for reporting this bug. Check that likescount and commentscount can be used in filename templates without causing the program to crash. """ looter = instaLooter.InstaLooter( self.tmpdir, profile="zuck", get_videos=True, add_metadata=True, template='{id}-{likescount}-{commentscount}', ) looter.download(media_count=10) for image in os.listdir(self.tmpdir): self.assertRegex(image, '[a-zA-Z0-9]*-[0-9]*-[0-9]*.(jpg|mp4)')
def test_issue_84(self): """ Thanks to @raphaelbernardino for reporting this bug. Make sure private profiles with few pictures (less than a page worth) raise the private warning as expected. """ with warnings.catch_warnings(record=True) as registry: warnings.simplefilter('always') looter = instaLooter.InstaLooter(profile="raphaelbernardino") list(looter.medias()) self.assertEqual( six.text_type(registry[0].message), u"Profile raphaelbernardino is private, retry after logging in.")
def test_issue_42(self): """ Thanks to @MohamedIM for reporting this bug. Checks that a multipost is successfully downloaded from the CLI `post` option. """ looter = instaLooter.InstaLooter(self.tmpdir) looter.download_post('BRW-j_dBI6F') self.assertEqual( set(os.listdir(self.tmpdir)), { '1465633492745668095.mp4', '1465633517836005761.mp4', '1465633541559037966.mp4', '1465633561523918792.mp4', })
def test_issue_39(self): """ Feature request by @verafide Checks that all pictures are downloaded from posts with more than one picture. """ looter = instaLooter.InstaLooter(self.tmpdir) looter.download_post("BRHecUuFhPl") self.assertEqual( set(os.listdir(self.tmpdir)), { "1461270165803344956.jpg", "1461270167497776767.jpg", "1461270174435133336.jpg", "1461270172581471925.jpg", "1461270181565655668.jpg", })
def test_issue_82(self): """ Thanks to @MohamedIM for reporting this bug. Check that urls containing 'h-ak-igx' are not stripped from all their parameters. """ looter = instaLooter.InstaLooter(self.tmpdir, template='{code}') info = looter.get_post_info('BWOYSYQDCo5') info['display_url'] = \ 'https://ig-s-c-a.akamaihd.net/h-ak-igx/19764472_1586345694718446_4011887281420894208_n.jpg' looter.get_post_info = lambda code: info looter.download_post('BWOYSYQDCo5') with open(os.path.join(self.tmpdir, 'BWOYSYQDCo5.jpg'), 'rb') as f: self.assertNotIn(b'5xx Server Error', f.read())
def test_issue_57(self): """ Thanks to @VasiliPupkin256 for reporting this bug. Checks that metadata can successfully extract caption out of multiposts containing images. """ looter = instaLooter.InstaLooter(self.tmpdir, profile="awwwwshoot_ob", get_videos=True, add_metadata=True) looter._medias_queue = six.moves.queue.Queue() looter._fill_media_queue() while not looter._medias_queue.empty(): media = looter._medias_queue.get() for key in ('caption', 'code', 'date'): self.assertIn(key, media) self.assertIsNotNone(key, media[key])
def test_hashtag_download(self): looter = instaLooter.InstaLooter(self.tmpdir, hashtag="python", get_videos=True) looter.download(media_count=200) self.assertEqual(len(os.listdir(self.tmpdir)), 200)
def test_url_generator_nocallable(self): with self.assertRaises(ValueError): self.looter = instaLooter.InstaLooter(self.tmpdir, profile="instagram", url_generator=1)
def setUp(self): super(TestUtils, self).setUp() self.looter = instaLooter.InstaLooter()