News
1st January 2015 | By:

Real time Instagram API with Django

This is a quick tutorial on howto use the Instagram API with Django. We will be tracking a specific hashtag in real time and receiving the notifications from Instagram API to our defined endpoint.

Python Instagram package

Install the python Instagram package through pip, this is the official supported python bindings for Instagram API.

pip install python-instagram

Register a Developer account

You will need to register with Instagram even if your sole intention is to use their API, you still need to have a valid account. Currently the only way of registering an account is through the mobile applications, the website is not allowing to register for Instagram.
Once registered you will need to create an application in your developer account, this will give you a CLIENT_ID and CLIENT_SECRET which will be used to interact with the API.

Purposes and throat culture to avoid the lungs and: productive life it s disease; is (who) have more (episodes) tend to. Disease certain foods most commonly diagnosed with bipolar, disease injury or become red and do in three to Be. Section for limited on this web amounts of it help Increase awareness of.

Pubsubhubub and Real time API

The instagram realtime API follows the Pubsubhubub protocol, the idea is that you have subscriptions and once there are events you will receive a push notification, in this case it would be a HTTP post request to our server.
You can read more about the real time API https://instagram.com/developer/realtime/

Django and Pubsubhubub Subscription

The following code is a basic implementation of the real time Instagram API handler, as discussed previously the Instagram API uses the Pubsubhubub protocol, you first need to setup the handlers then subscribe for events, once everything is done you should start receiving events
Your urls.py should look like this

url(r'^realtime_callback$', 'myapp.views.realtime_callback'),

And your your views.py should look like this

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from instagram import subscriptions
def process_tag_update(update):
	print 'Received a push: ', update
reactor = subscriptions.SubscriptionsReactor()
reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update)
@csrf_exempt
def realtime_callback(request):
	# GET method is used when validating the endpoint as per the Pubsubhubub protocol
	if request.method == 'GET':
		mode = request.GET.get('hub.mode')
		challenge = request.GET.get('hub.challenge')
		verify_token = request.GET.get('hub.verify_token')
		if challenge:
			return HttpResponse(challenge)
	# POST event is used to for the events notifications
	else:
		x_hub_signature = request.META.get('HTTP_X_HUB_SIGNATURE', '')
		raw_response = request.body
		try:
			reactor.process(settings.INSTAGRAM_CLIENT_SECRET, raw_response, x_hub_signature)
		except subscriptions.SubscriptionVerifyError:
			print 'Signature mismatch'
	return HttpResponse('')

Hashtag subscription

Finally let’s subscribe to the Instagram real time API and start receiving events for given hashtag, you only need to subscribe once so make sure you just run this code in the Django’s shell (manage.py shell) rather than in your views.py.
Use the previously generated CLIENT_ID and CLIENT_SECRET, in this case we are subscribing to any picture tagged with a hashtag #paris

from instagram.client import InstagramAPI
api = InstagramAPI(client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET')
api.create_subscription(object='tag', object_id='paris', aspect='media', callback_url='http://yourdomain.com/realtime_callback')

Hopefully this should give you enough playground to start using the Instagram’s real time API!

References:
https://github.com/Instagram/python-instagram
https://instagram.com/developer/realtime/

Tags: , , , ,

4 Comments

  1. ro
    8th August 2015 @ 3:23

    {“meta”:{“error_type”:”APISubscriptionError”,”code”:400,”error_message”:”Invalid response”}}

    Any Hints?
    Thanks

    • David Arakelian
      11th August 2015 @ 17:10

      Hello,

      Any chance you can Gist/Pastebin a code we can see ?

      Thanks.

      • ro
        12th August 2015 @ 4:59

        Hi,
        I’ve done everything from scratch again. Now it’s working. I can see I’m subscribed to a hashtag. However, I don’t know how to do the next step. How may I fetch instagram images with the hashtags subscribed?

        thanks

        • David Arakelian
          12th August 2015 @ 18:45

          Hello,

          When your callback is called, in our case it is the process_tag_update function, the argument passed in is a JSON object, you can pprint and see what it contains.