import bottle_session import bottle from bottle import route, redirect, post, run, request from instagram import client, subscriptions bottle.debug(True) app = bottle.app() plugin = bottle_session.SessionPlugin(cookie_lifetime=600) app.install(plugin) CONFIG = { 'client_id': '', 'client_secret': '', 'redirect_uri': 'http://localhost:8515/oauth_callback' } unauthenticated_api = client.InstagramAPI(**CONFIG) def process_tag_update(update): print(update) reactor = subscriptions.SubscriptionsReactor() reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update) @route('/') def home(): try: url = unauthenticated_api.get_authorize_url(scope=["likes","comments"]) return 'Connect with Instagram' % url except Exception as e: print(e) def get_nav(): nav_menu = ("

Python Instagram

" "") return nav_menu @route('/oauth_callback') def on_callback(session): code = request.GET.get("code") if not code: return 'Missing code' try: access_token, user_info = unauthenticated_api.exchange_code_for_access_token(code) print(("access token= " + access_token)) if not access_token: return 'Could not get access token' api = client.InstagramAPI(access_token=access_token) session['access_token']=access_token except Exception as e: print(e) return get_nav() @route('/recent') def on_recent(session): access_token = session.get('access_token') content = "

User Recent Media

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) recent_media, next = api.user_recent_media() photos = [] for media in recent_media: photos.append('
') if(media.type == 'video'): photos.append('' % (media.get_standard_resolution_url())) else: photos.append('' % (media.get_low_resolution_url())) print(media) photos.append("
Like Un-Like LikesCount=%s
" % (media.id,media.id,media.like_count)) content += ''.join(photos) except Exception as e: print(e) return "%s %s
Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/media_like/') def media_like(session,id): access_token = session.get('access_token') api = client.InstagramAPI(access_token=access_token) api.like_media(media_id=id) redirect("/recent") @route('/media_unlike/') def media_unlike(session,id): access_token = session.get('access_token') api = client.InstagramAPI(access_token=access_token) api.unlike_media(media_id=id) redirect("/recent") @route('/user_media_feed') def on_user_media_feed(session): access_token = session.get('access_token') content = "

User Media Feed

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) media_feed, next = api.user_media_feed() photos = [] for media in media_feed: photos.append('' % media.get_standard_resolution_url()) counter = 1 while next and counter < 3: media_feed, next = api.user_media_feed(with_next_url=next) for media in media_feed: photos.append('' % media.get_standard_resolution_url()) counter += 1 content += ''.join(photos) except Exception as e: print(e) return "%s %s
Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/location_recent_media') def location_recent_media(session): access_token = session.get('access_token') content = "

Location Recent Media

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) recent_media, next = api.location_recent_media(location_id=514276) photos = [] for media in recent_media: photos.append('' % media.get_standard_resolution_url()) content += ''.join(photos) except Exception as e: print(e) return "%s %s
Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/media_search') def media_search(session): access_token = session.get('access_token') content = "

Media Search

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) media_search = api.media_search(lat="37.7808851",lng="-122.3948632",distance=1000) photos = [] for media in media_search: photos.append('' % media.get_standard_resolution_url()) content += ''.join(photos) except Exception as e: print(e) return "%s %s
Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/media_popular') def media_popular(session): access_token = session.get('access_token') content = "

Popular Media

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) media_search = api.media_popular() photos = [] for media in media_search: photos.append('' % media.get_standard_resolution_url()) content += ''.join(photos) except Exception as e: print(e) return "%s %s
Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/user_search') def user_search(session): access_token = session.get('access_token') content = "

User Search

" if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) user_search = api.user_search(q="Instagram") users = [] for user in user_search: users.append('
  • %s
  • ' % (user.profile_picture,user.username)) content += ''.join(users) except Exception as e: print(e) return "%s %s
    Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/location_search') def location_search(session): access_token = session.get('access_token') content = "

    Location Search

    " if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) location_search = api.location_search(lat="37.7808851",lng="-122.3948632",distance=1000) locations = [] for location in location_search: locations.append('
  • %s Map
  • ' % (location.name,location.point.latitude,location.point.longitude)) content += ''.join(locations) except Exception as e: print(e) return "%s %s
    Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/tag_search') def tag_search(session): access_token = session.get('access_token') content = "

    Tag Search

    " if not access_token: return 'Missing Access Token' try: api = client.InstagramAPI(access_token=access_token) tag_search, next_tag = api.tag_search(q="catband") tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name) photos = [] for tag_media in tag_recent_media: photos.append('' % tag_media.get_standard_resolution_url()) content += ''.join(photos) except Exception as e: print(e) return "%s %s
    Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit) @route('/realtime_callback') @post('/realtime_callback') def on_realtime_callback(): mode = request.GET.get("hub.mode") challenge = request.GET.get("hub.challenge") verify_token = request.GET.get("hub.verify_token") if challenge: return challenge else: x_hub_signature = request.header.get('X-Hub-Signature') raw_response = request.body.read() try: reactor.process(CONFIG['client_secret'], raw_response, x_hub_signature) except subscriptions.SubscriptionVerifyError: print("Signature mismatch") run(host='localhost', port=8515, reloader=True)