InstaloaderContext
(Low-level functions)¶
InstaloaderContext
¶
-
class
InstaloaderContext
(sleep=True, quiet=False, user_agent=None, max_connection_attempts=3, request_timeout=300.0, rate_controller=None, fatal_status_codes=None, iphone_support=True)¶ Class providing methods for (error) logging and low-level communication with Instagram.
It is not thought to be instantiated directly, rather
Instaloader
instances maintain a context object.For logging, it provides
log()
,error()
,error_catcher()
.It provides low-level communication routines
get_json()
,graphql_query()
,graphql_node_list()
,get_and_write_raw()
and implements mechanisms for rate controlling and error handling.Further, it provides methods for logging in and general session handles, which are used by that routines in class
Instaloader
.-
anonymous_copy
()¶
-
log
(*msg, sep='', end='\n', flush=False)¶ Log a message to stdout that can be suppressed with –quiet.
-
error
(msg, repeat_at_end=True)¶ Log a non-fatal error message to stderr, which is repeated at program termination.
- Parameters
msg – Message to be printed.
repeat_at_end – Set to false if the message should be printed, but not repeated at program termination.
-
close
()¶ Print error log and close session
-
error_catcher
(extra_info=None)¶ Context manager to catch, print and record InstaloaderExceptions.
-
save_session_to_file
(sessionfile)¶ Not meant to be used directly, use
Instaloader.save_session_to_file()
.
-
load_session_from_file
(username, sessionfile)¶ Not meant to be used directly, use
Instaloader.load_session_from_file()
.
-
test_login
()¶ Not meant to be used directly, use
Instaloader.test_login()
.
-
login
(user, passwd)¶ Not meant to be used directly, use
Instaloader.login()
.- Raises
InvalidArgumentException – If the provided username does not exist.
BadCredentialsException – If the provided password is wrong.
ConnectionException – If connection to Instagram failed.
TwoFactorAuthRequiredException – First step of 2FA login done, now call
Instaloader.two_factor_login()
.
-
two_factor_login
(two_factor_code)¶ Second step of login if 2FA is enabled. Not meant to be used directly, use
Instaloader.two_factor_login()
.- Raises
InvalidArgumentException – No two-factor authentication pending.
BadCredentialsException – 2FA verification code invalid.
New in version 4.2.
-
do_sleep
()¶ Sleep a short time if self.sleep is set. Called before each request to instagram.com.
-
get_json
(path, params, host='www.instagram.com', session=None, _attempt=1)¶ JSON request to Instagram.
- Parameters
- Return type
- Returns
Decoded response dictionary
- Raises
QueryReturnedBadRequestException – When the server responds with a 400.
QueryReturnedNotFoundException – When the server responds with a 404.
ConnectionException – When query repeatedly failed.
-
graphql_query
(query_hash, variables, referer=None, rhx_gis=None)¶ Do a GraphQL Query.
- Parameters
- Return type
- Returns
The server’s response dictionary.
-
graphql_node_list
(query_hash, query_variables, query_referer, edge_extractor, rhx_gis=None, first_data=None)¶ Retrieve a list of GraphQL nodes.
Deprecated since version 4.5: Use
NodeIterator
instead, which provides more functionality.
-
get_iphone_json
(path, params)¶ JSON request to
i.instagram.com
.- Parameters
- Return type
- Returns
Decoded response dictionary
- Raises
QueryReturnedBadRequestException – When the server responds with a 400.
QueryReturnedNotFoundException – When the server responds with a 404.
ConnectionException – When query repeatedly failed.
New in version 4.2.1.
-
write_raw
(resp, filename)¶ Write raw response data into a file.
New in version 4.2.1.
- Return type
None
-
get_raw
(url, _attempt=1)¶ Downloads a file anonymously.
- Raises
QueryReturnedNotFoundException – When the server responds with a 404.
QueryReturnedForbiddenException – When the server responds with a 403.
ConnectionException – When download failed.
New in version 4.2.1.
- Return type
-
get_and_write_raw
(url, filename)¶ Downloads and writes anonymously-requested raw data into a file.
- Raises
QueryReturnedNotFoundException – When the server responds with a 404.
QueryReturnedForbiddenException – When the server responds with a 403.
ConnectionException – When download repeatedly failed.
- Return type
None
-
head
(url, allow_redirects=False)¶ HEAD a URL anonymously.
- Raises
QueryReturnedNotFoundException – When the server responds with a 404.
QueryReturnedForbiddenException – When the server responds with a 403.
ConnectionException – When request failed.
New in version 4.7.6.
- Return type
-
RateController
¶
-
class
RateController
(context)¶ Class providing request tracking and rate controlling to stay within rate limits.
It can be overridden to change Instaloader’s behavior regarding rate limits, for example to raise a custom exception when the rate limit is hit:
import instaloader class MyRateController(instaloader.RateController): def sleep(self, secs): raise MyCustomException() L = instaloader.Instaloader(rate_controller=lambda ctx: MyRateController(ctx))
New in version 4.5.
-
sleep
(secs)¶ Wait given number of seconds.
-
count_per_sliding_window
(query_type)¶ Return how many requests of the given type can be done within a sliding window of 11 minutes.
This is called by
RateController.query_waittime()
and allows to simply customize wait times before queries at query_type granularity. Consider overridingRateController.query_waittime()
directly if you need more control.- Return type
-
query_waittime
(query_type, current_time, untracked_queries=False)¶ Calculate time needed to wait before query can be executed.
- Return type
-
wait_before_query
(query_type)¶ This method is called before a query to Instagram.
It calls
RateController.query_waittime()
to determine the time needed to wait and then callsRateController.sleep()
to wait until the request can be made.- Return type
None
-
handle_429
(query_type)¶ This method is called to handle a 429 Too Many Requests response.
It calls
RateController.query_waittime()
to determine the time needed to wait and then callsRateController.sleep()
to wait until we can repeat the same request.- Return type
None
-