Python Client for Instagram API

Overview

This project is not actively maintained. Proceed at your own risk!


Build Status

python-instagram

A Python 2/3 client for the Instagram REST and Search APIs

Installation

pip install python-instagram

Requires

  • httplib2
  • simplejson
  • six

Instagram REST and Search APIs

Our developer site documents all the Instagram REST and Search APIs.

Blog

The [Developer Blog] features news and important announcements about the Instagram Platform. You will also find tutorials and best practices to help you build great platform integrations. Make sure to subscribe to the RSS feed not to miss out on new posts: http://developers.instagram.com.

Community

The Stack Overflow community is a great place to ask API related questions or if you need help with your code. Make sure to tag your questions with the Instagram tag to get fast answers from other fellow developers and members of the Instagram team.

Authentication

Instagram API uses the OAuth2 protocol for authentication, but not all functionality requires authentication. See the docs for more information: http://instagram.com/developer/authentication/

Obtaining an access token

If you're using a method that requires authentication and need an access token, you can use the provided get_access_token.py script to obtain an access token for yourself. It will prompt you for your app's Client ID, Client Secret, and Redirect URI, and walk you through instructions for getting your own access token for your app.

Authenticating a user

The provided sample app shows a simple OAuth flow for authenticating a user and getting an access token for them.

Using an access token

Once you have an access token (whether via the script or from the user flow), you can pass that token into the InstagramAPI constructor:

from instagram.client import InstagramAPI

access_token = "YOUR_ACCESS_TOKEN"
client_secret = "YOUR_CLIENT_SECRET"
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
   print media.caption.text

Making unauthenticated requests

For methods that don't require authentication, you can just pass your client ID and optionally client secret into the InstagramAPI constructor:

api = InstagramAPI(client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET')
popular_media = api.media_popular(count=20)
for media in popular_media:
    print media.images['standard_resolution'].url

Real-time Subscriptions:

See the docs for more on real-time subscriptions: http://instagr.am/developer/realtime/

You can use the API to subscribe to users, tags, locations, or geographies:

# Subscribe to updates for all users authenticated to your app
api.create_subscription(object='user', aspect='media', callback_url='http://mydomain.com/hook/instagram')

# Subscribe to all media tagged with 'fox'
api.create_subscription(object='tag', object_id='fox', aspect='media', callback_url='http://mydomain.com/hook/instagram')

# Subscribe to all media in a given location
api.create_subscription(object='location', object_id='1257285', aspect='media', callback_url='http://mydomain.com/hook/instagram')

# Subscribe to all media in a geographic area
api.create_subscription(object='geography', lat=35.657872, lng=139.70232, radius=1000, aspect='media', callback_url='http://mydomain.com/hook/instagram')

Along with that, you would typically register subscription "reactors" for processing the different subscription types:

# React to user type updates
reactor = subscriptions.SubscriptionsReactor()
reactor.register_callback(subscriptions.SubscriptionType.USER, process_user_update)

See the provided sample app for an example of making a subscription, reacting to it, an processing the updates.

You can also use the API to list and delete subscriptions:

api.list_subscriptions()
api.delete_subscriptions(id=342342)

Data Retrieval:

See the endpoints docs for more on these methods: http://instagr.am/developer/endpoints/

The methods with a * return two values, where the second is a pagination parameter. Here's an example of retrieving recent media:

recent_media, next_ = api.user_recent_media()
photos = []
for media in recent_media:
    photos.append('<img src="%s"/>' % media.images['thumbnail'].url)

And an example of exhaustively pursuing a paginated endpoint:

follows, next_ = api.user_follows()
while next_:
    more_follows, next_ = api.user_follows(with_next_url=next_)
    follows.extend(more_follows)

Users: http://instagr.am/developer/endpoints/users/

api.user(user_id)
api.user_media_feed()*
api.user_liked_media()*
api.user_recent_media(user_id, count, max_id)*
api.user_search(q, count, lat, lng, min_timestamp, max_timestamp)

Relationships: http://instagr.am/developer/endpoints/relationships/

api.user_incoming_requests()
api.user_follows(user_id)*
api.user_followed_by(user_id)*
api.follow_user(user_id)
api.unfollow_user(user_id)
api.block_user(user_id)
api.unblock_user(user_id)
api.approve_user_request(user_id)
api.ignore_user_request(user_id)
api.user_relationship(user_id)

Media: http://instagr.am/developer/endpoints/media/

api.media(media_id)
api.media_popular(count, max_id)
api.media_search(q, count, lat, lng, min_timestamp, max_timestamp)

Comments: http://instagr.am/developer/endpoints/comments/

api.media_comments(media_id)
api.create_media_comment(media_id, text)
api.delete_comment(media_id, comment_id)

Likes: http://instagr.am/developer/endpoints/likes/

api.media_likes(media_id)
api.like_media(media_id)
api.unlike_media(media_id)

Tags: http://instagr.am/developer/endpoints/tags/

api.tag(tag_name) 
api.tag_recent_media(count, max_tag_id, tag_name)*
api.tag_search(q, count)*

Locations: http://instagr.am/developer/endpoints/locations/

api.location(location_id)
api.location_recent_media(count, max_id, location_id)*
api.location_search(q, count, lat, lng, foursquare_id, foursquare_v2_id)

Geographies: http://instagr.am/developer/endpoints/geographies/

api.geography_recent_media(count, max_id, geography_id)*

Error handling

Importing the bind module allows handling of specific error status codes. An example is provided below:

from instagram.bind import InstagramAPIError

try:
   # your code goes here
except InstagramAPIError as e:
   if (e.status_code == 400):
      print "\nUser is set to private."

Trouble Shooting

If you get an error of a module not being defined during the Instagram import call, this might update a necessary package.

sudo pip install --upgrade six

Sample app

This repository includes a one-file sample app that uses the bottle framework and demonstrates authentication, subscriptions, and update processing. To try it out:

  • Download bottle if you don't already have it: pip install bottle
  • Download bottle-session if you don't already have it: pip install bottle-session
  • Download and run a redis instance on port 6379 if you don't already have it. Check http://redis.io for instructions.
  • Set your redirect URI to 'http://localhost:8515/oauth_callback' in your dev profile
  • Open up sample_app.py, update it with your client_id and secret, and set redirect URI to 'http://localhost:8515/oauth_callback'
  • Run the file; it will host a local server on port 8515.
  • Try visiting http://localhost:8515 in your browser

Contributing

In the spirit of free software, everyone is encouraged to help improve this project.

Here are some ways you can contribute:

  • by using alpha, beta, and prerelease versions
  • by reporting bugs
  • by suggesting new features
  • by writing or editing documentation
  • by writing specifications
  • by writing code (no patch is too small: fix typos, add comments, clean up inconsistent whitespace)
  • by refactoring code
  • by closing issues
  • by reviewing patches

Submitting an Issue

We use the GitHub issue tracker to track bugs and features. Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. You can indicate support for an existing issue by voting it up. When submitting a bug report, please include a Gist that includes a stack trace and any details that may be necessary to reproduce the bug, including your version number, and operating system. Ideally, a bug report should include a pull request with failing specs.

Submitting a Pull Request

  1. Fork the project.
  2. Create a topic branch.
  3. Implement your feature or bug fix.
  4. Run python tests.py .
  5. Add a test for your feature or bug fix.
  6. Run python tests.py . If your changes are not 100% covered, go back to step 5.
  7. Commit and push your changes.
  8. Submit a pull request.
  9. If you haven't already, complete the Contributor License Agreement ("CLA").

Contributor License Agreement ("CLA")


In order to accept your pull request, we need you to submit a CLA. You only need to do this once to work on any of Instagram's or Facebook's open source projects.

Complete your CLA here: https://code.facebook.com/cla

Copyright

Copyright (c) 2014, Facebook, Inc. All rights reserved. By contributing to python-instagram, you agree that your contributions will be licensed under its BSD license. See LICENSE for details.

Issues
  • add python3.4 support

    add python3.4 support

    python 3.4 supported. But httplib2 and oath2 have some problems

    opened by gcd0318 10
  • Add pagination_format parameter to api methods.

    Add pagination_format parameter to api methods.

    This patch makes it possible to use the api to follow a media stream.

    Reacting to real-time updates about a tag requires the use of the "min_tag_id" parameter which is included in the pagination object returned by api methods that paginate. Unfortunately, python-instagram only exposes the "next_url" member of that object, which is not needed if you only want the "latest since last time I polled". Mike described the use of "min_tag_id" here.

    This patch fixes that by adding a "pagination_format" parameter to the api methods which defaults to "next_url" so it won't break existing code. Setting the parameter to "dict" will return the full pagination dictionary instead of just its "next_url" member. Setting "as_generator" to "True" will cause the "pagination_format" parameter to be ignored since the generator does not return pagination information anyway.

    I hope you find this patch useful and unobtrusive enough to include it. Thank you! Henning

    opened by henninge 10
  • Python 3 support

    Python 3 support

    The crew at Shoutlet added Python 3 support to the client. It's fully compatible with Python 2.6+ / 3 with a 100% test coverage on Python 2.7 / 3.3.

    opened by seajosh 8
  • Fix 404 for media_shortcode because of extraneous appended format

    Fix 404 for media_shortcode because of extraneous appended format

    Addresses #127: Add an exclude_format parameter to bind_method to allow us to build a path without the format.

    This is necessary because unlike the other endpoints, media/shortcode/{shortcode}.json 404s and should be requested as just `media/shortcode/{shortcode}.

    Previous behavior

    api = InstagramAPI(access_token=access_tken)
    api.media_shortcode('os1NQjxtvF')
    

    raises InstagramClientError: (404) Unable to parse response, not valid JSON.

    Corrected behavior

    api = InstagramAPI(access_token=access_tken)
    api.media_shortcode('os1NQjxtvF')
    

    returns Media: 933107621857404944_231015329 as expected.

    CLA Signed 
    opened by dtran320 6
  • python 3 support

    python 3 support

    as latest master

    opened by gcd0318 6
  • Add most recent Python versions in Travis CI

    Add most recent Python versions in Travis CI

    Add more recent Python versions including development branches and nightly build.

    The motivation came from reading brettcannon's article : https://snarky.ca/how-to-use-your-project-travis-to-help-test-python-itself/ . Trying to activate the newest Python versions on CI jobs is in most case a win-win situation: if everything works fine, there is nothing to worry about. If an issue is spotted, it is good to know about it to fix it on your side or to open a bug on CPython ( https://bugs.python.org/ ).

    Also, if a failures is spotted, you can use the allow_failures option in your matrix build (more information about this in the link above).

    Also, nightly and 3.7-dev may be a bit too recent/unstable so I can remove then if need be.

    More information about how this PR happened : https://github.com/SylvainDe/CIthon .

    More information about the various Python versions available on Travis : https://docs.travis-ci.com/user/languages/python/ .

    More information about the Python 3.6 version schedule : https://www.python.org/dev/peps/pep-0494/ . More information about the Python 3.7 version schedule : https://www.python.org/dev/peps/pep-0537/ .

    CLA Signed 
    opened by SylvainDe 6
  • Added Video model

    Added Video model

    Added Video model and populating the Media object with Video objects under the dictionary key videos.

    opened by mgunneras 4
  • Fix #17

    Fix #17

    opened by michaelhelmick 3
  • Add exhaustive pagination example to README

    Add exhaustive pagination example to README

    I found this a bit unclear myself. I can't find it mentioned in the provided Python wrapper docs or the official API docs themselves. To me this feels like an important param to understand and clarify since most API results are paginated, and most people will be interested in returning multiple pages of data via the native objects (as opposed to fetching the URL with requests or something).

    CLA Signed 
    opened by tedmiston 3
  • Update get_access_token.py

    Update get_access_token.py

    opened by rshubh 1
  • setup.py

    setup.py

    some formatting

    opened by sawan1130 1
  • Update requirements.txt

    Update requirements.txt

    opened by rahulshivan05 1
  • Changed beaker version for python 3.7+

    Changed beaker version for python 3.7+

    Beaker version lower than 1.10.* not working with python 3.7+

    CLA Signed 
    opened by mrananyan 4
  • Update setup.py

    Update setup.py

    opened by rshubh 1
  • Upgrade security in sample code for authentication in README

    Upgrade security in sample code for authentication in README

    It is recommended not to store sensitive data in the code itself, but keep it separate.

    CLA Signed 
    opened by GiulioC 2
  • Python3

    Python3

    opened by Renatogmez 1
  • amend and specification

    amend and specification

    I corrected some grammar mistakes in the code,clean up some whitespace,editing documents,create a specification file and edit some comments.Thank you and I trying my best to do these.

    CLA Signed 
    opened by Ady1998 0
  • ;

    ;

    Fixes #259

    opened by akjones07 1
  • Fixed: issue with Requesting the access_token: You must provide a cli…

    Fixed: issue with Requesting the access_token: You must provide a cli…

    …ent_id

    Problem: It was not receiving access_token while exchanging code. Error: You must provide a client_id

    Solution: File: oauth2.py Function: exchange_for_access_token +headers = {'Content-type': 'application/x-www-form-urlencoded'} +response, content = http_object.request(url,headers=headers,method="POST", body=data) -response, content = http_object.request(url,method="POST", body=data)

    CLA Signed 
    opened by functorcode 0
Owner
Facebook Archive
These projects have been archived and are generally unsupported, but are still available to view and use
Facebook Archive
A Python Instagram Scraper for Downloading Profile's Posts, stories, ProfilePic and See the Details of Particular Instagram Profile.

✔ ✔ InstAstra ⚡ ⚡ ⁜ Description ~ A Python Instagram Scraper for Downloading Profile's Posts, stories, ProfilePic and See the Details of Particular In

null 8 Dec 23, 2021
Instagram-follower-bot - An Instagram follower bot written in Python

Instagram Follower Bot An Instagram follower bot written in Python. The bot follows the follower of which account you want. e.g. (You want to follow @

Aytaç Kaşoğlu 1 Dec 31, 2021
Python Client for Instagram API

This project is not actively maintained. Proceed at your own risk! python-instagram A Python 2/3 client for the Instagram REST and Search APIs Install

Facebook Archive 2.9k Jan 11, 2022
Python Client for Instagram API

This project is not actively maintained. Proceed at your own risk! python-instagram A Python 2/3 client for the Instagram REST and Search APIs Install

Facebook Archive 2.9k Jan 18, 2022
📷 Instagram Bot - Tool for automated Instagram interactions

InstaPy Tooling that automates your social media interactions to “farm” Likes, Comments, and Followers on Instagram Implemented in Python using the Se

Tim Großmann 13.5k Dec 1, 2021
Instagram bot that upload images for you which scrape posts from 9gag meme website or other Instagram users , which is 24/7 Automated Runnable.

Autonicgram Automates your Instagram posts by taking images from sites like 9gag or other Instagram accounts and posting it onto your page. Features A

Mastermind 16 Dec 30, 2021
This Instagram app created as a clone of instagram.Developed during Moringa Core.

Instagram This Instagram app created as a clone of instagram.Developed during Moringa Core. AUTHOR By: Nyagah Isaac Description This web-app allows a

Nyagah Isaac 1 Nov 1, 2021
Projeto Informações Conta do Instagram - Instagram Account Information Project

Projeto Informações Conta do Instagram - Instagram Account Information Project Descrição - Description Projeto que exibe informações de perfil, lista

Thiago Souza 1 Dec 2, 2021
Instagram Brute force attack helps you to find password of an instagram account from your list of provided password.

Instagram Brute force attack Instagram Brute force attack helps you to find password of an instagram account from your list of provided password. Inst

Naman Raj Singh 1 Dec 27, 2021
Instagram - Instagram Account Reporting Tool

Instagram Instagram Account Reporting Tool Installation On Termux $ apt update $

Aryan 3 Jan 14, 2022
A Python library to access Instagram's private API.

Instagram Private API A Python wrapper for the Instagram private API with no 3rd party dependencies. Supports both the app and web APIs. Overview I wr

null 2.3k Jan 20, 2022
wyscoutapi is an extremely basic API client for the Wyscout API (v2 & v3) for Python

wyscoutapi wyscoutapi is an extremely basic API client for the Wyscout API (v2 & v3). Usage Install with pip install wyscoutapi. To connect to the Wys

Ben Torvaney 7 Dec 19, 2021
Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Harun Mbaabu Mwenda 45 Nov 18, 2021
Python API Client for Twitter API v2

?? Python Client For Twitter API v2 ?? Why Twitter Stream ? Twitter-Stream.py a python API client for Twitter API v2 now supports FilteredStream, Samp

Twitivity 17 Jan 4, 2022
Drcom-pt-client - Drcom Pt version client with refresh timer

drcom-pt-client Drcom Pt version client with refresh timer Dr.com Pt版本客户端 可用于网页认

null 3 Jan 4, 2022
Official Python client for the MonkeyLearn API. Build and consume machine learning models for language processing from your Python apps.

MonkeyLearn API for Python Official Python client for the MonkeyLearn API. Build and run machine learning models for language processing from your Pyt

MonkeyLearn 148 Dec 14, 2021
🖥️ Python - P1 Monitor API Asynchronous Python Client

??️ Asynchronous Python client for the P1 Monitor

Klaas Schoute 2 Jan 10, 2022
To send an Instagram message using Python

To send an Instagram message using Python, you must have an Instagram account and install the Instabot library in your Python virtual environment.

Coding Taggers 1 Dec 18, 2021
Tiktok 2 Instagram With Python

Tiktok2Instagram ?? About The Project What it does: Download the source video from a user inputted Tiktok URL. ?? Add audio to the Tiktok video from a

Carter Belisle 3 Dec 28, 2021