instatools package

Submodules

instatools.api module

Wrappers for interactions with Instagram API

class instatools.api.ApiMethod(api)[source]

Bases: object

action(path, *args, max_attempts=10, return_key='', extra=None, method='POST', headers=None, params=None, data=None)[source]
Parameters:
  • path
  • args
  • max_attempts
  • return_key
  • extra
  • method
  • headers
  • params
  • data
Returns:

feed(feed_type, *args, seen=None, raw=False)[source]
Parameters:
  • feed_type
  • args
  • seen
  • raw
Returns:

form(path, bodies, boundary, params=None, return_key='')[source]
Parameters:
  • path
  • bodies
  • boundary
  • params
  • return_key
Returns:

instatools.api.requires_login(func)[source]

instatools.cache module

Response caching for testing integration with instatools

class instatools.cache.DataBaseCache(db_path, default_factories=None)[source]

Bases: object

clear()[source]
delete(key)[source]
get(key)[source]
set(key, value)[source]
instatools.cache.clear(data_dir)[source]
instatools.cache.read(data_dir)[source]
instatools.cache.record(data_dir)[source]

instatools.models module

class instatools.models.Comment(api, json)[source]

Bases: instatools.models.Model

classmethod parse(api, json, extra=None)[source]
remove()[source]
reply(msg)[source]
class instatools.models.Location(api, json)[source]

Bases: instatools.models.Model

classmethod parse(api, json, extra=None)[source]
class instatools.models.Model(api, json)[source]

Bases: object

classmethod parse(api, json, extra=None)[source]
classmethod parse_list(api, json_list, extra=None)[source]
class instatools.models.ModelFactory[source]

Bases: object

comment

alias of Comment

default

alias of Model

friendship_status

alias of Relationship

item

alias of Post

location

alias of Location

post

alias of Post

ranked_item

alias of Post

relationship

alias of Relationship

user

alias of User

class instatools.models.Post(api, json)[source]

Bases: instatools.models.Model

comments[source]
download()[source]
likers[source]
classmethod parse(api, json, extra=None)[source]
save()[source]
share(user_ids, msg=None)[source]
unsave()[source]
upload()[source]
class instatools.models.Relationship(api, json)[source]

Bases: instatools.models.Model

classmethod parse(api, json, extra=None)[source]
class instatools.models.User(api, json)[source]

Bases: instatools.models.Model

approve()[source]
block()[source]
direct_message(msg)[source]
direct_share(post_id, msg=None)[source]
feed
follow()[source]
followers[source]
following[source]
ignore()[source]
classmethod parse(api, json, extra=None)[source]
unblock()[source]
unfollow()[source]

instatools.session module

Instagram Session class provides bare minimum to make authenticated, rate_limited requests to the Instagram API

class instatools.session.Session(username=None, password=None, session=None)[source]

Bases: object

Class representing the request-making Session of a single Instagram user

static build_form_body(bodies, boundary)[source]
Parameters:
  • bodies
  • boundary
Returns:

static configure_data(width, height, upload_id, caption, video=False)[source]
static configure_data_photo(width, height)[source]
static configure_data_video(duration)[source]
device_id = None
exponential_sleep_increase = 2
form_data_for_message(recipients, text=None)[source]
Parameters:
  • recipients
  • text
Returns:

static form_headers(boundary)[source]
Parameters:boundary
Returns:
login()[source]

Login to Instagram with account credentials provided in __init__

login_data(cookies)[source]

Using cookies provided, gives data to send to Instagram for login approval :param cookies: cookies from login challenge :return: dict: data to use for login

logout()[source]

Logout of currently logged-in account

password = None
paths = {'approve': 'friendships/approve/{}/', 'autocomplete_users': 'friendships/autocomplete_user_list', 'block': 'friendships/block/{}/', 'change_password': 'accounts/change_password/', 'change_profile_picture': 'accounts/change_profile_picture/', 'comment': 'media/{}/comment/', 'comments': 'media/{}/comments/', 'configure': 'media/configure', 'create_post': '', 'direct_link': 'direct_v2/threads/broadcast/link/', 'direct_message': 'direct_v2/threads/broadcast/text/', 'direct_share': 'direct_v2/threads/broadcast/media_share/', 'direct_threads': 'direct_v2/threads/{}/', 'edit_profile': 'accounts/edit_profile/', 'explore': 'discover/explore', 'expose': 'qe/expose', 'facebook_search': 'fbsearch/topsearch', 'follow': 'friendships/create/{}/', 'followers': 'friendships/{}/followers', 'following': 'friendships/{}/following', 'friendship': 'friendships/show/{}/', 'geo_media': 'maps/user/{}', 'ignore': 'friendships/ignore/{}/', 'inbox': 'direct_v2/inbox', 'like': 'media/{}/like/', 'liked': 'feed/liked', 'likers': 'media/{}/likers/', 'location_feed': 'feed/location/{}/', 'location_search': 'fbsearch/places', 'login': 'accounts/login/', 'login_challenge': 'si/fetch_headers/', 'logout': 'accounts/logout/', 'megaphone': 'megaphone/log', 'pending': 'friendships/pending', 'popular': 'feed/popular', 'post': 'media/{}/info/', 'profile': 'accounts/current_user/', 'recent_activity': 'news/inbox', 'recent_following_activity': 'news', 'remove_comment': 'media/{}/comment/{}/delete/', 'remove_post': 'media/{}/delete/', 'remove_profile_picture': 'accounts/remove_profile_picture/', 'remove_tag': 'media/{}/remove/', 'save': 'media/{}/save/', 'saved': 'feed/saved', 'set_phone_name': 'accounts/set_phone_and_name/', 'set_private': 'accounts/set_private/', 'set_public': 'accounts/set_public/', 'share_inbox': 'direct_share/inbox', 'story': 'feed/user/{}/reel_media', 'tag_feed': 'feed/tag/{}/', 'tag_search': 'tags/search', 'timeline': 'feed/timeline', 'unblock': 'friendships/unblock/{}/', 'unfollow': 'friendships/destroy/{}/', 'unlike': 'media/{}/unlike/', 'unsave': 'media/{}/unsave/', 'upload_photo': 'upload/photo', 'upload_video': 'upload/video', 'user': 'users/{}/info/', 'user_feed': 'feed/user/{}/', 'user_search': 'users/search', 'user_tags': 'usertags/{}/feed', 'username': 'users/{}/usernameinfo'}
rank_token
relog_after_failed = 5
request(method, url, *, params=None, data=None, return_json=True, **kwargs)[source]
Parameters:
  • method
  • url
  • params
  • data
  • return_json
  • kwargs
Returns:

request_safely(*args, max_attempts=0, **kwargs)[source]

Make a safe request that returns correct results or dies trying! Keeps requesting with exponential back-off until requests_to_break is reached, at which point each consecutive request is circuit-broken and waits sleep_on_break seconds until a successful request is made, or until relog_after_failed circuit-broken requests are made, at which point the client re-logs and begins the whole cycle again. :param args: :param max_attempts: :param kwargs: :return:

requests_to_break = 10
session_data
set_proxy(proxy)[source]

Set proxy for all requests made with this session :param proxy: str: proxy - format: “user:password@ip:port” OR “ip:port”

setup(username, password, session)[source]

Setup session variables by username/password or by previous session :param username: :param password: :param session: :return:

sleep_on_break = 600
sleep_on_page = 0.5
switch_user(username=None, password=None, session=None)[source]

Switches current account username and password - requires login

token = None
upload_data_photo(upload_id, photo_file_object)[source]
static upload_headers(content_type, video=False)[source]
Parameters:
  • content_type
  • video
Returns:

url(path, *args)[source]

Return url for api path formatted with args

username = None
username_id = None
uuid = None
wait_limit(url)[source]
Parameters:url
Returns:
instatools.session.generate_device_id(seed)[source]

Generate new device id

instatools.session.generate_signature(data)[source]

Generates signed signature of POST data using SIG_KEY (signing key) :param data: dict: POST data :return: str: data to append to request url

instatools.session.generate_upload_id()[source]

Generate new upload id :return:

instatools.session.generate_uuid(typ)[source]

Generate new uuid :param typ: :return:

instatools.utils module

Module contents

Top-level package for instatools.

class instatools.Instagram(username=None, password=None, session=None)[source]

Bases: object

approve(user_id)[source]
block(user_id)[source]
comment(post_id, text)[source]
direct_message(recipients, msg)[source]
direct_share(post_id, recipients, msg=None)[source]
edit_caption(post_id, new_caption)[source]
follow(user_id)[source]
get_comments(post_id)[source]
get_followers(user_id=None)[source]
get_following(user_id=None)[source]
get_friendship(user_id)[source]
get_geo_media(user_id)[source]
get_likers(post_id)[source]
get_post(post_id)[source]
get_story(user_id)[source]
get_user(user_id)[source]
get_username(username)[source]
ignore(user_id)[source]
like(post_id)[source]
login()[source]

Login to Instagram with account credentials provided in __init__

logout()[source]

Logout of currently logged-in account

password

Password of Instagram account

post(*args)[source]
remove_comment(post_id, comment_id)[source]
remove_post(post_id)[source]
remove_tag(post_id)[source]
save(post_id)[source]
switch_user(username=None, password=None, session=None)[source]

Switches current account username and password - requires login

unblock(user_id)[source]
unfollow(user_id)[source]
unlike(post_id)[source]
unsave(post_id)[source]
username

Username of Instagram account

username_id

User-id of Instagram account