Skip to content

Instantly share code, notes, and snippets.

@yomguy
Last active November 22, 2017 15:34
Show Gist options
  • Save yomguy/adaf032f6713e8abf55977a25f32d4f1 to your computer and use it in GitHub Desktop.
Save yomguy/adaf032f6713e8abf55977a25f32d4f1 to your computer and use it in GitHub Desktop.
Instafeed like django tag displaying an instagram gallery by hashtag (as a workaround since official API change in june 2016)
from django import template
import requests, json
register = template.Library()
@register.simple_tag
def instafeed(hashtag, count):
"""Simply use with autoescapeoff, for example: {% autoescape off %}{% instafeed 'manifeste16' 8 %}{% endautoescape %}"""
html = ''
main_url = 'https://www.instagram.com'
query_url = main_url + '/query/?q='
params = 'ig_hashtag(%s){media.first(%s){count,nodes{caption,code,comments{count},date,dimensions{height,width},display_src,id,is_video,likes{count},owner{id,username},thumbnail_src,video_views,video_url},page_info}}' % (hashtag, str(count))
item_html = '<div class="box-item-25"><a href="%s" target="_blank"><img src="%s" alt="%s" title="%s"></a></div>'
r = requests.get(query_url + params)
j = json.loads(r.content.decode('utf-8'))
for node in j['media']['nodes']:
item_url = main_url + '/p/' + node['code'] + '/'
item_src = node['thumbnail_src']
item_title = node['caption']
html += item_html % (item_url, item_src, item_title, item_title)
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment