Beispiel #1
0
def retrieve_posts_from_tag(client: ApiClient, tag: str,
                            limit: int) -> List[dict]:
    obj = ps.RetrieveByTag(tag_name=tag)
    obj, result = client.post_retrieve_by_tag(obj)
    retrieved_items = []

    while result and len(retrieved_items) < limit:
        retrieved_items.extend(result)
        obj, result = client.post_retrieve_by_tag(obj)
    return retrieved_items[:limit:]
Beispiel #2
0
def retrieve_posts_from_tag(client: ApiClient, tag: str,
                            limit: int) -> List[models.Post]:
    """Retrieve x amount of posts tagged with a tag.

    Args:
        client: your `ApiClient`
        limit: maximum amount of posts to retrieve
        tag: the tag to search for

    Returns:
        A list of Instagram post objects (objects/post.json).
    """
    obj = ps.RetrieveByTag(tag_name=tag)
    obj, result = client.post_retrieve_by_tag(obj)
    retrieved_items = []

    while result and len(retrieved_items) < limit:
        logger.info(f"Retrieved {len(retrieved_items)} posts by tag")
        retrieved_items.extend(result)
        obj, result = client.post_retrieve_by_tag(obj)
    return [models.Post.parse(p) for p in retrieved_items[:limit:]]
from instauto.api.client import ApiClient
from instauto.api.actions import post as ps
from instauto.api.actions import search as se

if __name__ == '__main__':
    if os.path.isfile('./.instauto.save'):
        client = ApiClient.initiate_from_file('./.instauto.save')
    else:
        client = ApiClient(username=os.environ.get("INSTAUTO_USER")
                           or "your_username",
                           password=os.environ.get("INSTAUTO_PASS")
                           or "your_password")
        client.log_in()
        client.save_to_disk('./.instauto.save')

s = se.Tag('instagram', 1)
resp = client.search_tag(s).json()
results = resp['results'][0]
tag_name = results['name']
rbt = ps.RetrieveByTag(tag_name)
obj, result = client.post_retrieve_by_tag(rbt)
retrieved_items = []

# retrieve the first 20 posts
while result and len(retrieved_items) < 20:
    retrieved_items.extend(result)
    obj, result = client.post_retrieve_by_tag(obj)
    print(f"Retrieved {len(result)} new posts!")
print(f"Retrieved a total of {len(retrieved_items)} posts!")