Ejemplo n.º 1
0
def test_get_stories(login, password):
    agent = AgentAccount(login, password)

    data = agent.stories()

    Account.clear_cache()
    Story.clear_cache()
Ejemplo n.º 2
0
    def __init__(self, login, password, settings={}):
        if not isinstance(settings, dict):
            raise TypeError("'settings' must be dict type")

        Account.__init__(self, login)
        Agent.__init__(self, settings=settings)

        data = {"username": self.login, "password": password}

        if "headers" in settings:
            settings["headers"]["X-CSRFToken"] = self._csrf_token
            settings["headers"]["referer"] = "https://www.instagram.com/"
        else:
            settings["headers"] = {
                "X-CSRFToken": self._csrf_token,
                "referer": "https://www.instagram.com/",
            }

        response = self._post_request(
            "https://www.instagram.com/accounts/login/ajax/",
            data=data,
            **settings,
        )

        try:
            data = response.json()
            if not data["authenticated"]:
                raise AuthException(self.login)
        except (ValueError, KeyError) as exception:
            raise UnexpectedResponse(exception, response.url, response.text)
Ejemplo n.º 3
0
def setup_function():
    Account.clear_cache()
    Comment.clear_cache()
    Location.clear_cache()
    Media.clear_cache()
    Story.clear_cache()
    Tag.clear_cache()
Ejemplo n.º 4
0
def test_get_feed_long(login, password, count):
    agent = AgentAccount(login, password)

    data, pointer = agent.feed(count=count)

    assert (count >= len(data))

    Account.clear_cache()
Ejemplo n.º 5
0
def test_update(login, password):
    agent = AgentAccount(login, password)

    agent.update()

    assert (not getattr(agent, "id") is None)

    Account.clear_cache()
Ejemplo n.º 6
0
def test_follow_unfollow(login, password, username):
    agent = AgentAccount(login, password)
    account = Account(username)

    assert (agent.follow(account))
    assert (agent.unfollow(account))

    Account.clear_cache()
Ejemplo n.º 7
0
def test_update_account(login, password, username):
    agent = AgentAccount(login, password)
    account = Account(username)

    data = agent.update(account)

    assert (not data is None)

    Account.clear_cache()
Ejemplo n.º 8
0
def setup_function():
    Account.clear_cache()
    Media.clear_cache()
    Location.clear_cache()
    Tag.clear_cache()
    if not anon["global_delay"] is None:
        min_delay = anon["global_delay"].get("min", 0)
        max_delay = anon["global_delay"].get("max", 120)
        sleep(random() * (max_delay - min_delay) + min_delay)
Ejemplo n.º 9
0
def test_like_unlike_photo_set(login, password, shortcode):
    agent = AgentAccount(login, password)
    photo_set = Media(shortcode)

    assert (agent.like(photo_set))
    assert (agent.unlike(photo_set))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 10
0
def test_like_unlike_video(login, password, shortcode):
    agent = AgentAccount(login, password)
    video = Media(shortcode)

    assert (agent.like(video))
    assert (agent.unlike(video))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 11
0
def test_get_followers_long(login, password, count, username):
    agent = AgentAccount(login, password)
    account = Account(username)

    data, pointer = agent.get_followers(account, count=count)

    assert (min(account.followers_count, count) == len(data))
    assert ((pointer is None) == (account.followers_count <= count))

    Account.clear_cache()
Ejemplo n.º 12
0
def test_comment(login, password, shortcode):
    agent = AgentAccount(login, password)
    media = Media(shortcode)

    comment = agent.add_comment(media, "test")
    agent.delete_comment(comment)

    Account.clear_cache()
    Media.clear_cache()
    Comment.clear_cache()
Ejemplo n.º 13
0
def test_get_media_account(login, password, count, username):
    agent = AgentAccount(login, password)
    account = Account(username)

    data, pointer = agent.get_media(account, count=count)

    assert (min(account.media_count, count) == len(data))
    assert ((pointer is None) == (account.media_count <= count))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 14
0
def test_get_feed_pointer(login, password, count):
    agent = AgentAccount(login, password)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = agent.feed(pointer=pointer)
        data.extend(tmp)

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 15
0
def test_get_media_account(count, username):
    anon = Agent()
    account = Account(username)

    data, pointer = anon.get_media(account, count=count)

    assert (min(account.media_count, count) == len(data))
    assert ((pointer is None) == (account.media_count <= count))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 16
0
def get_posts_by_username(username, num=None, path=None):
    agent = Agent()
    agent.update(Account(username))
    account = Account(username)

    media = set()
    pointer = None

    if num == None:
        media_count = account.media_count
    else:
        media_count = num

    limit = 50
    batch_num = math.ceil(media_count / limit)

    for i in range(batch_num):
        if i == batch_num - 1:
            count = media_count - limit * (batch_num - 1)
            batch_media, pointer = agent.get_media(account,
                                                   pointer=pointer,
                                                   count=count)
        else:
            batch_media, pointer = agent.get_media(account,
                                                   pointer=pointer,
                                                   count=limit)

        for j, item in enumerate(batch_media):
            print("Getting media: " + str(i * 50 + j + 1) + " / " +
                  str(media_count))
            media.add(Media(item.code))

    media_posts = {}
    for i, item in enumerate(media):
        post_info = copy.copy(item)
        post_info.owner = username
        post_info.likes = dict(post_info.likes)
        post_info.comments = dict(post_info.comments)
        media_posts[i] = post_info.__dict__

    media_dict = {"posts": media_posts}
    media_json = json.dumps(media_dict, indent=2)
    print(media_json)

    if path == None:
        path = './data/' + username

    pathlib.Path(path).mkdir(parents=True, exist_ok=True)
    filename = path + '/' + username + '__last_posts.json'

    with open(filename, 'w', newline='', encoding='utf8') as f:
        f.write(media_json)

    return media
Ejemplo n.º 17
0
def test_get_followers_pointer(login, password, count, username):
    agent = AgentAccount(login, password)
    account = Account(username)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = agent.get_followers(account, pointer=pointer)
        data.extend(tmp)

    assert ((pointer is None) == (account.followers_count <= count))

    Account.clear_cache()
Ejemplo n.º 18
0
def test_get_media_account_pointer(count, username):
    anon = Agent()
    account = Account(username)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = anon.get_media(account, pointer=pointer)
        data.extend(tmp)

    assert ((pointer is None) == (account.media_count <= count))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 19
0
def test_get_likes_pointer(login, password, count, shortcode):
    agent = AgentAccount(login, password)
    media = Media(username)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = agent.get_likes(media, pointer=pointer)
        data.extend(tmp)

    assert ((pointer is None) == (media.likes_count <= count))

    Account.clear_cache()
    Media.clear_cache()
Ejemplo n.º 20
0
def test_get_media_tag_pointer(login, password, count, name):
    agent = AgentAccount(login, password)
    tag = Tag(name)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = agent.get_media(tag, pointer=pointer)
        data.extend(tmp)

    assert ((pointer is None) == (tag.media_count <= count))

    Account.clear_cache()
    Media.clear_cache()
    Tag.clear_cache()
Ejemplo n.º 21
0
def test_get_media_location_pointer(login, password, count, id):
    agent = AgentAccount(login, password)
    location = Location(id)
    pointer = None
    data = []

    for i in range(count):
        tmp, pointer = agent.get_media(location, pointer=pointer)
        data.extend(tmp)

    assert ((pointer is None) == (location.media_count <= count))

    Account.clear_cache()
    Media.clear_cache()
    Location.clear_cache()
Ejemplo n.º 22
0
def test_clear_cache_story():
    account = Account("test")
    story = Story("test")
    
    Story.clear_cache()

    assert(Story._cache == dict())
Ejemplo n.º 23
0
 def profile_exists(username):
     try:
         agent = Agent()
         account = Account(username)
         agent.update(account)
         return True
     except:
         return False
Ejemplo n.º 24
0
def test_get_media_account(agent, delay, settings, count, username):
    account = Account(username)
    data, pointer = agent.get_media(account,
                                    count=count,
                                    delay=delay,
                                    settings=settings)

    assert min(account.media_count, count) == len(data)
    assert (pointer is None) == (account.media_count <= count)
Ejemplo n.º 25
0
def test_clear_cache_comment(id):
    account = Account("test")
    media = Media("test")
    comment = Comment(id, media=media, owner=account, text="test", created_at=0)
    assert Comment.cache == {id: comment}  

    Comment.clear_cache()
    assert Comment.cache == dict()
    assert Media.cache == {"test": media}
    assert Account.cache == {"test": account}
Ejemplo n.º 26
0
def test_clear_cache_comment():
    account = Account("test")
    media = Media("test")
    comment = Comment(1488, media=media, owner=account, text="test",
                      created_at=0)
    
    Media.clear_cache()
    Comment.clear_cache()
    
    assert(Comment._cache == dict())
    assert(Media._cache == dict())
Ejemplo n.º 27
0
def test_get_media_account_pointer(agent_account, delay, settings, count, username):
    account = Account(username)
    pointer = None
    data = []

    for _ in range(count):
        tmp, pointer = agent_account.get_media(account, pointer=pointer, settings=settings)
        sleep(delay)
        data.extend(tmp)

    assert (pointer is None) == (account.media_count == len(data))
Ejemplo n.º 28
0
def test_get_followers_pointer(agent_account, delay, settings, count, username):
    account = Account(username)
    pointer = None
    data = []

    for _ in range(count):
        tmp, pointer = agent_account.get_followers(account, pointer=pointer, settings=settings)
        sleep(delay)
        data.extend(tmp)

    assert (pointer is None) == (account.followers_count <= count)
Ejemplo n.º 29
0
def test_update_account(username):
    anon = Agent()
    account = Account(username)

    data = anon.update(account)

    assert (not data is None)
    assert (not account.id is None)
    assert (not account.full_name is None)
    assert (not account.profile_pic_url is None)
    assert (not account.profile_pic_url_hd is None)
    assert (not account.biography is None)
    assert (not account.follows_count is None)
    assert (not account.followers_count is None)
    assert (not account.media_count is None)
    assert (not account.is_private is None)
    assert (not account.is_verified is None)
    assert (not account.country_block is None)

    Account.clear_cache()
Ejemplo n.º 30
0
async def test_async_get_followers(async_agent_account, delay, settings, count, username):
    account = Account(username)
    data, pointer = await async_agent_account.get_followers(
        account,
        count=count,
        delay=delay,
        settings=settings,
    )

    assert min(account.followers_count, count) == len(data)
    assert (pointer is None) == (account.followers_count <= count)