2018-03-24 19:38:29 +01:00
|
|
|
"""Unit Tests for Instaloader"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2018-05-14 10:42:31 +02:00
|
|
|
from itertools import islice
|
2020-07-11 13:54:31 +02:00
|
|
|
from typing import Optional
|
2018-03-24 19:38:29 +01:00
|
|
|
|
|
|
|
import instaloader
|
|
|
|
|
2018-08-24 12:33:28 +02:00
|
|
|
PROFILE_WITH_HIGHLIGHTS = 325732271
|
2018-04-18 15:58:45 +02:00
|
|
|
PUBLIC_PROFILE = "selenagomez"
|
|
|
|
PUBLIC_PROFILE_ID = 460563723
|
2020-03-14 16:56:51 +01:00
|
|
|
PUBLIC_PROFILE_WITH_IGTV = "natgeo"
|
2018-03-24 19:38:29 +01:00
|
|
|
HASHTAG = "kitten"
|
2018-12-17 21:35:31 +01:00
|
|
|
LOCATION = "362629379"
|
2018-03-24 19:38:29 +01:00
|
|
|
OWN_USERNAME = "aandergr"
|
|
|
|
NORMAL_MAX_COUNT = 2
|
|
|
|
PAGING_MAX_COUNT = 15
|
|
|
|
PRIVATE_PROFILE = "aandergr"
|
2019-02-04 22:57:05 +01:00
|
|
|
PRIVATE_PROFILE_ID = 1706625676
|
|
|
|
EMPTY_PROFILE = "not_public"
|
|
|
|
EMPTY_PROFILE_ID = 1928659031
|
2018-03-24 19:38:29 +01:00
|
|
|
|
2020-07-11 13:54:31 +02:00
|
|
|
ratecontroller = None # type: Optional[instaloader.RateController]
|
2018-05-30 13:16:16 +02:00
|
|
|
|
2018-03-24 19:38:29 +01:00
|
|
|
|
2018-04-17 14:10:27 +02:00
|
|
|
class TestInstaloaderAnonymously(unittest.TestCase):
|
2018-03-24 19:38:29 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dir = tempfile.mkdtemp()
|
|
|
|
print("Testing in {}".format(self.dir))
|
|
|
|
os.chdir(self.dir)
|
2018-04-13 19:06:26 +02:00
|
|
|
self.L = instaloader.Instaloader(download_geotags=True,
|
|
|
|
download_comments=True,
|
|
|
|
save_metadata=True)
|
2018-04-05 00:20:03 +02:00
|
|
|
self.L.context.raise_all_errors = True
|
2020-07-11 13:54:31 +02:00
|
|
|
if ratecontroller is not None:
|
|
|
|
# pylint:disable=protected-access
|
|
|
|
ratecontroller._context = self.L.context
|
|
|
|
self.L.context._rate_controller = ratecontroller
|
2018-03-24 19:38:29 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
2019-05-17 21:53:21 +02:00
|
|
|
# pylint:disable=global-statement,protected-access
|
2020-07-11 13:54:31 +02:00
|
|
|
global ratecontroller
|
|
|
|
ratecontroller = self.L.context._rate_controller
|
2018-04-05 00:20:03 +02:00
|
|
|
self.L.close()
|
2018-03-24 19:38:29 +01:00
|
|
|
os.chdir('/')
|
|
|
|
print("Removing {}".format(self.dir))
|
|
|
|
shutil.rmtree(self.dir)
|
|
|
|
|
2018-07-09 10:08:18 +02:00
|
|
|
def post_paging_test(self, iterator):
|
|
|
|
previous_post = None
|
|
|
|
for post in islice(iterator, PAGING_MAX_COUNT):
|
|
|
|
print(post)
|
|
|
|
if previous_post:
|
|
|
|
self.assertTrue(post.date_utc < previous_post.date_utc)
|
|
|
|
previous_post = post
|
|
|
|
|
2019-05-17 21:53:21 +02:00
|
|
|
@unittest.skip('')
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_public_profile_download(self):
|
2019-05-17 21:53:21 +02:00
|
|
|
profiles = {self.L.check_profile_id(PUBLIC_PROFILE)}
|
|
|
|
self.L.download_profiles(profiles, profile_pic=False, fast_update=True, raise_errors=True)
|
|
|
|
self.L.download_profiles(profiles, profile_pic=False, fast_update=True, raise_errors=True)
|
2018-03-24 19:38:29 +01:00
|
|
|
|
2018-04-17 14:23:12 +02:00
|
|
|
def test_public_profile_paging(self):
|
2018-07-09 10:08:18 +02:00
|
|
|
self.post_paging_test(instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts())
|
2018-04-17 14:23:12 +02:00
|
|
|
|
2018-04-17 14:10:27 +02:00
|
|
|
def test_profile_pic_download(self):
|
2019-05-17 21:53:21 +02:00
|
|
|
self.L.download_profiles({self.L.check_profile_id(PUBLIC_PROFILE)}, posts=False, raise_errors=True)
|
2018-04-17 14:10:27 +02:00
|
|
|
|
|
|
|
def test_hashtag_download(self):
|
|
|
|
self.L.download_hashtag(HASHTAG, NORMAL_MAX_COUNT)
|
|
|
|
|
2018-10-07 14:59:16 +02:00
|
|
|
def test_hashtag_paging(self):
|
2020-04-19 12:21:27 +02:00
|
|
|
for count, post in enumerate(instaloader.Hashtag.from_name(self.L.context, HASHTAG).get_all_posts()):
|
2018-10-07 14:59:16 +02:00
|
|
|
print(post)
|
|
|
|
if count == PAGING_MAX_COUNT:
|
|
|
|
break
|
|
|
|
|
2018-04-17 14:10:27 +02:00
|
|
|
def test_get_id_by_username(self):
|
|
|
|
self.assertEqual(PUBLIC_PROFILE_ID,
|
|
|
|
instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).userid)
|
|
|
|
|
2019-02-04 22:57:05 +01:00
|
|
|
def test_get_username_by_id_private(self):
|
|
|
|
self.assertEqual(PRIVATE_PROFILE.lower(),
|
|
|
|
instaloader.Profile.from_id(self.L.context, PRIVATE_PROFILE_ID).username)
|
|
|
|
|
|
|
|
def test_get_username_by_id_public(self):
|
2018-05-01 13:06:06 +02:00
|
|
|
self.assertEqual(PUBLIC_PROFILE.lower(),
|
|
|
|
instaloader.Profile.from_id(self.L.context, PUBLIC_PROFILE_ID).username)
|
|
|
|
|
2019-02-04 22:57:05 +01:00
|
|
|
def test_get_username_by_id_empty(self):
|
|
|
|
self.assertEqual(EMPTY_PROFILE.lower(),
|
|
|
|
instaloader.Profile.from_id(self.L.context, EMPTY_PROFILE_ID).username)
|
|
|
|
|
2020-06-09 11:43:06 +02:00
|
|
|
def test_get_username_by_name_empty(self):
|
|
|
|
self.assertEqual(EMPTY_PROFILE_ID,
|
|
|
|
instaloader.Profile.from_username(self.L.context, EMPTY_PROFILE).userid)
|
|
|
|
|
2018-04-17 14:10:27 +02:00
|
|
|
def test_post_from_mediaid(self):
|
|
|
|
for post in instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts():
|
|
|
|
post2 = instaloader.Post.from_mediaid(self.L.context, post.mediaid)
|
|
|
|
self.assertEqual(post, post2)
|
|
|
|
break
|
|
|
|
|
2018-08-02 16:48:06 +02:00
|
|
|
def test_public_profile_tagged_paging(self):
|
|
|
|
for post in islice(instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_tagged_posts(),
|
|
|
|
PAGING_MAX_COUNT):
|
|
|
|
print(post)
|
|
|
|
|
2020-03-14 16:56:51 +01:00
|
|
|
def test_public_profile_igtv(self):
|
|
|
|
for post in islice(instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE_WITH_IGTV).get_igtv_posts(),
|
|
|
|
PAGING_MAX_COUNT):
|
|
|
|
print(post)
|
|
|
|
|
2018-04-17 14:10:27 +02:00
|
|
|
|
|
|
|
class TestInstaloaderLoggedIn(TestInstaloaderAnonymously):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.L.load_session_from_file(OWN_USERNAME)
|
|
|
|
|
2019-05-17 21:53:21 +02:00
|
|
|
@unittest.skip('')
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_stories_download(self):
|
|
|
|
self.L.download_stories()
|
|
|
|
|
2019-05-17 21:53:21 +02:00
|
|
|
@unittest.skip('')
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_private_profile_download(self):
|
2019-05-17 21:53:21 +02:00
|
|
|
self.L.download_profiles({self.L.check_profile_id(PRIVATE_PROFILE)}, stories=True, raise_errors=True)
|
2018-03-24 19:38:29 +01:00
|
|
|
|
2018-04-17 14:23:12 +02:00
|
|
|
def test_stories_paging(self):
|
|
|
|
for user_story in self.L.get_stories():
|
|
|
|
print("profile {}.".format(user_story.owner_username))
|
|
|
|
for item in user_story.get_items():
|
|
|
|
print(item)
|
|
|
|
|
2018-08-24 12:33:28 +02:00
|
|
|
def test_highlights_paging(self):
|
|
|
|
for user_highlight in self.L.get_highlights(PROFILE_WITH_HIGHLIGHTS):
|
|
|
|
print("Retrieving {} highlights \"{}\" from profile {}".format(user_highlight.itemcount,
|
|
|
|
user_highlight.title,
|
|
|
|
user_highlight.owner_username))
|
|
|
|
for item in user_highlight.get_items():
|
|
|
|
print(item)
|
|
|
|
|
2018-04-17 14:23:12 +02:00
|
|
|
def test_private_profile_paging(self):
|
2018-07-09 10:08:18 +02:00
|
|
|
self.post_paging_test(instaloader.Profile.from_username(self.L.context, PRIVATE_PROFILE).get_posts())
|
2018-04-17 14:23:12 +02:00
|
|
|
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_feed_download(self):
|
|
|
|
self.L.download_feed_posts(NORMAL_MAX_COUNT)
|
|
|
|
|
|
|
|
def test_feed_paging(self):
|
|
|
|
for count, post in enumerate(self.L.get_feed_posts()):
|
|
|
|
print(post)
|
|
|
|
if count == PAGING_MAX_COUNT:
|
|
|
|
break
|
|
|
|
|
|
|
|
def test_saved_download(self):
|
|
|
|
self.L.download_saved_posts(NORMAL_MAX_COUNT)
|
|
|
|
|
|
|
|
def test_saved_paging(self):
|
2018-04-10 15:25:36 +02:00
|
|
|
for count, post in enumerate(instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_saved_posts()):
|
2018-03-24 19:38:29 +01:00
|
|
|
print(post)
|
|
|
|
if count == PAGING_MAX_COUNT:
|
|
|
|
break
|
|
|
|
|
|
|
|
def test_test_login(self):
|
|
|
|
self.assertEqual(OWN_USERNAME, self.L.test_login())
|
|
|
|
|
2018-05-14 10:42:31 +02:00
|
|
|
def test_followees_and_stories(self):
|
|
|
|
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
|
2019-05-17 21:53:21 +02:00
|
|
|
followees = set(islice(profile.get_followees(), PAGING_MAX_COUNT))
|
|
|
|
self.L.download_profiles(followees, profile_pic=False, stories=True, posts=False, raise_errors=True)
|
2018-05-14 10:42:31 +02:00
|
|
|
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_get_followees(self):
|
2018-04-11 21:24:36 +02:00
|
|
|
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
|
|
|
|
for f in profile.get_followees():
|
2018-04-17 15:11:50 +02:00
|
|
|
print(f.username)
|
2018-03-24 19:38:29 +01:00
|
|
|
|
|
|
|
def test_get_followers(self):
|
2018-04-11 21:24:36 +02:00
|
|
|
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
|
|
|
|
for f in profile.get_followers():
|
2018-04-17 15:11:50 +02:00
|
|
|
print(f.username)
|
2018-03-24 19:38:29 +01:00
|
|
|
|
2020-03-25 19:06:05 +01:00
|
|
|
def test_get_similar_accounts(self):
|
|
|
|
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
|
|
|
|
for f in profile.get_similar_accounts():
|
|
|
|
print(f.username)
|
|
|
|
|
2018-03-24 19:38:29 +01:00
|
|
|
def test_get_likes(self):
|
2018-04-10 15:25:36 +02:00
|
|
|
for post in instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_posts():
|
2018-03-24 19:38:29 +01:00
|
|
|
for like in post.get_likes():
|
2018-04-28 17:32:13 +02:00
|
|
|
print(like.username)
|
2018-03-24 19:38:29 +01:00
|
|
|
break
|
|
|
|
|
2018-04-11 21:24:36 +02:00
|
|
|
def test_explore_paging(self):
|
|
|
|
for count, post in enumerate(self.L.get_explore_posts()):
|
|
|
|
print(post)
|
|
|
|
if count == PAGING_MAX_COUNT:
|
|
|
|
break
|
|
|
|
|
2019-08-10 16:39:32 +02:00
|
|
|
def test_location_download(self):
|
|
|
|
self.L.download_location(LOCATION, NORMAL_MAX_COUNT)
|
|
|
|
|
|
|
|
def test_location_paging(self):
|
|
|
|
for count, post in enumerate(self.L.get_location_posts(LOCATION)):
|
|
|
|
print(post)
|
|
|
|
if count == PAGING_MAX_COUNT:
|
|
|
|
break
|
|
|
|
|
2018-03-24 19:38:29 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|