A simple Instagram's web API library
Project description
Instpector
A simple Instagram's web API library written in Python. No selenium or webdriver required.
Supports login with two-factor authentication enabled.
Installation
pip install instpector
Sample usage
from instpector import Instpector, endpoints
instpector = Instpector()
# Login into Instagram's web
instpector.login("my_username", "my_password")
# Get the profile of any user, for example 'some_username'
profile = endpoints.factory.create("profile", instpector)
insta_profile = profile.of_user("some_username")
print(insta_profile)
# id, followers_count, following_count, is_private, ...
# Iterate all followers of 'some_username'
followers = endpoints.factory.create("followers", instpector)
for follower in followers.of_user(insta_profile.id):
print(follower)
# id, username, full_name, ...
# Logout
instpector.logout()
Examples
Check out more examples here.
Using 2FA
For login in using two-factor authentication, generate your 2fa key on Instagram's app and provide the code when logging in with instpector
. The following example uses pytop
to demonstrate the usage:
from pyotp import TOTP
from instpector import Instpector, endpoints
instpector = Instpector()
totp = TOTP("my_2fa_key") # Input without spaces
# Login into Instagram's web
instpector.login("my_username", "my_password", totp.now())
Available endpoints
- Followers
- Following
- Timeline
- Profile
- Story Reel
- Story
More to come
API
Classes
Instpector
Method | Details |
---|---|
login(user, password, two_factor_code=None) | Login to an Instagram account. If your account is 2FA protected check the provided example. |
logout() | Logouts from an Instagram account |
session() | Returns the current session used by instpector |
EndpointFactory
Method | Details |
---|---|
create(endpoint_name, instpector_instance) | Creates and returns an endpoint instance based on the provided name. Available names are: "followers" , "following" , "profile" , "timeline" , "story_reel" and "story" |
Endpoints
Profile
Gets the profile of any public or friend user account.
Method | Details |
---|---|
of_user(username) | Returns a TProfile instance of the provided username. |
Followers
Endpoint for accessing the follower list of any public or friend user account.
Method | Details |
---|---|
of_user(user_id) | Returns a generator of TUser instances with all followers. Note the method receives a user id and not a username. To get the user id use the Profile endpoint. |
Following
Endpoint for accessing the followees list of any public or friend user account.
Method | Details |
---|---|
of_user(user_id) | Returns a generator of TUser instances with all followees. Note the method receives a user id and not a username. To get the user id use the Profile endpoint. |
Timeline
Endpoint for accessing the timeline of any public or friend account.
Method | Details |
---|---|
of_user(user_id) | Returns a generator of TTimelinePost instances with all timeline posts. Note the method receives a user id and not a username. To get the user id use the Profile endpoint. |
StoryReel
Endpoint for accessing the story reel (stories) of any public or friend user account.
Method | Details |
---|---|
of_user(user_id) | Returns a generator of TStoryReelItem instances with all stories. Note the method receives a user id and not a username. To get the user id use the Profile endpoint. |
download(story_item, low_quality=False) | Downloads and save the available resources (image and video) for the provided TStoryReelItem . The files are named using the story's Instagram Id and saved in the execution directory. If low_quality is True the resource will be the downloaded with the least size available. |
Story
Endpoint for accessing the story details of a story reel item. This endpoint is only available for stories posted by the current logged in user.
Method | Details |
---|---|
viewers_for(story_id) | Returns a generator of TStoryViewer instances with all viewers for the provided story id. |
Types
TUser
Field | Type | Details |
---|---|---|
id | string | The Instagram Id of the user |
username | string | The user's name |
full_name | string | The full name of the user |
is_private | bool | A flag to show if the user account is private |
TProfile
Field | Type | Details |
---|---|---|
id | string | The Instagram Id of the user |
username | string | The user's name |
biography | string | The biography of the user |
is_private | bool | A flag to show if the user account is private |
followers_count | integer | The follower count of the user |
following_count | integer | The following count of the user |
TTimelinePost
Field | Type | Details |
---|---|---|
id | string | The Instagram Id of the user |
timestamp | integer | The timestamp of the post |
is_video | bool | A flag to know if the story is a video |
like_count | integer | The like count of the post |
comment_count | integer | The comment count of the post |
TStoryReelItem
Field | Type | Details |
---|---|---|
id | string | The Instagram Id of the story |
timestamp | integer | The timestamp of the story |
expire_at | integer | The expiration timestamp of the story |
audience | string | The type of audience of the story. If public the value is MediaAudience.DEFAULT , if private the value is MediaAudience.BESTIES |
is_video | bool | A flag to know if the story is a video |
view_count | integer | The view count of the story. The count is only available for stories posted by the currently logged in user. Other accounts will have a count equal to 0 . |
display_resources | list | A list of images URLs associated with the story |
video_resources | list | A list of video URLs associated with the story |
TStoryViewer
Field | Type | Details |
---|---|---|
id | string | The Instagram Id of the story viewer |
username | string | The user name of the viewer |
Development dependencies
- requests
Tests
-
Create a
pytest.ini
file with the sample contents ofpytest.sample.ini
in thetests
directory. -
Add your account information.
-
Run with
pytest
:
(env)$ pytest -qs tests
Disclaimer
This tool is not affiliated with, authorized, maintained or endorsed by Instagram or any of its affiliates or subsidiaries. Use at your own risk.
License
Licensed under MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.