Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Changed beaker version for python 3.7+ #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ httplib2==0.9
python-instagram==1.1.3
redis==2.10.3
simplejson==3.6.3
beaker==1.6.4
six==1.8.0
beaker==1.11.0
six==1.8.0
79 changes: 51 additions & 28 deletions sample_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,45 @@

unauthenticated_api = client.InstagramAPI(**CONFIG)


@hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']


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"])
url = unauthenticated_api.get_authorize_url(scope=["likes", "comments"])
return '<a href="%s">Connect with Instagram</a>' % url
except Exception as e:
print(e)


def get_nav():
nav_menu = ("<h1>Python Instagram</h1>"
"<ul>"
"<li><a href='/recent'>User Recent Media</a> Calls user_recent_media - Get a list of a user's most recent media</li>"
"<li><a href='/user_media_feed'>User Media Feed</a> Calls user_media_feed - Get the currently authenticated user's media feed uses pagination</li>"
"<li><a href='/location_recent_media'>Location Recent Media</a> Calls location_recent_media - Get a list of recent media at a given location, in this case, the Instagram office</li>"
"<li><a href='/media_search'>Media Search</a> Calls media_search - Get a list of media close to a given latitude and longitude</li>"
"<li><a href='/media_popular'>Popular Media</a> Calls media_popular - Get a list of the overall most popular media items</li>"
"<li><a href='/user_search'>User Search</a> Calls user_search - Search for users on instagram, by name or username</li>"
"<li><a href='/user_follows'>User Follows</a> Get the followers of @instagram uses pagination</li>"
"<li><a href='/location_search'>Location Search</a> Calls location_search - Search for a location by lat/lng</li>"
"<li><a href='/tag_search'>Tags</a> Search for tags, view tag info and get media by tag</li>"
"<li><a href='/recent'>User Recent Media</a> Calls user_recent_media - Get a list of a user's most recent media</li>"
"<li><a href='/user_media_feed'>User Media Feed</a> Calls user_media_feed - Get the currently authenticated user's media feed uses pagination</li>"
"<li><a href='/location_recent_media'>Location Recent Media</a> Calls location_recent_media - Get a list of recent media at a given location, in this case, the Instagram office</li>"
"<li><a href='/media_search'>Media Search</a> Calls media_search - Get a list of media close to a given latitude and longitude</li>"
"<li><a href='/media_popular'>Popular Media</a> Calls media_popular - Get a list of the overall most popular media items</li>"
"<li><a href='/user_search'>User Search</a> Calls user_search - Search for users on instagram, by name or username</li>"
"<li><a href='/user_follows'>User Follows</a> Get the followers of @instagram uses pagination</li>"
"<li><a href='/location_search'>Location Search</a> Calls location_search - Search for a location by lat/lng</li>"
"<li><a href='/tag_search'>Tags</a> Search for tags, view tag info and get media by tag</li>"
"</ul>")
return nav_menu


@route('/oauth_callback')
def on_callback():
code = request.GET.get("code")
Expand All @@ -69,6 +75,7 @@ def on_callback():
print(e)
return get_nav()


@route('/recent')
def on_recent():
content = "<h2>User Recent Media</h2>"
Expand All @@ -81,15 +88,19 @@ def on_recent():
photos = []
for media in recent_media:
photos.append('<div style="float:left;">')
if(media.type == 'video'):
photos.append('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (media.get_standard_resolution_url()))
if (media.type == 'video'):
photos.append('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (
media.get_standard_resolution_url()))
else:
photos.append('<img src="%s"/>' % (media.get_low_resolution_url()))
photos.append("<br/> <a href='/media_like/%s'>Like</a> <a href='/media_unlike/%s'>Un-Like</a> LikesCount=%s</div>" % (media.id,media.id,media.like_count))
photos.append(
"<br/> <a href='/media_like/%s'>Like</a> <a href='/media_unlike/%s'>Un-Like</a> LikesCount=%s</div>" % (
media.id, media.id, media.like_count))
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/media_like/<id>')
def media_like(id):
Expand All @@ -98,13 +109,15 @@ def media_like(id):
api.like_media(media_id=id)
redirect("/recent")


@route('/media_unlike/<id>')
def media_unlike(id):
access_token = request.session['access_token']
api = client.InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret'])
api.unlike_media(media_id=id)
redirect("/recent")


@route('/user_media_feed')
def on_user_media_feed():
access_token = request.session['access_token']
Expand All @@ -126,7 +139,8 @@ def on_user_media_feed():
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/location_recent_media')
def location_recent_media():
Expand All @@ -143,7 +157,8 @@ def location_recent_media():
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/media_search')
def media_search():
Expand All @@ -153,14 +168,15 @@ def media_search():
return 'Missing Access Token'
try:
api = client.InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret'])
media_search = api.media_search(lat="37.7808851",lng="-122.3948632",distance=1000)
media_search = api.media_search(lat="37.7808851", lng="-122.3948632", distance=1000)
photos = []
for media in media_search:
photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/media_popular')
def media_popular():
Expand All @@ -177,7 +193,8 @@ def media_popular():
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/user_search')
def user_search():
Expand All @@ -190,11 +207,12 @@ def user_search():
user_search = api.user_search(q="Instagram")
users = []
for user in user_search:
users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
users.append('<li><img src="%s">%s</li>' % (user.profile_picture, user.username))
content += ''.join(users)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/user_follows')
def user_follows():
Expand All @@ -208,15 +226,16 @@ def user_follows():
user_follows, next = api.user_follows('25025320')
users = []
for user in user_follows:
users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
users.append('<li><img src="%s">%s</li>' % (user.profile_picture, user.username))
while next:
user_follows, next = api.user_follows(with_next_url=next)
for user in user_follows:
users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
users.append('<li><img src="%s">%s</li>' % (user.profile_picture, user.username))
content += ''.join(users)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/location_search')
def location_search():
Expand All @@ -226,14 +245,16 @@ def location_search():
return 'Missing Access Token'
try:
api = client.InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret'])
location_search = api.location_search(lat="37.7808851",lng="-122.3948632",distance=1000)
location_search = api.location_search(lat="37.7808851", lng="-122.3948632", distance=1000)
locations = []
for location in location_search:
locations.append('<li>%s <a href="https://www.google.com/maps/preview/@%s,%s,19z">Map</a> </li>' % (location.name,location.point.latitude,location.point.longitude))
locations.append('<li>%s <a href="https://www.google.com/maps/preview/@%s,%s,19z">Map</a> </li>' % (
location.name, location.point.latitude, location.point.longitude))
content += ''.join(locations)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/tag_search')
def tag_search():
Expand All @@ -251,7 +272,8 @@ def tag_search():
content += ''.join(photos)
except Exception as e:
print(e)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(), content, api.x_ratelimit_remaining, api.x_ratelimit)


@route('/realtime_callback')
@post('/realtime_callback')
Expand All @@ -269,4 +291,5 @@ def on_realtime_callback():
except subscriptions.SubscriptionVerifyError:
print("Signature mismatch")


bottle.run(app=app, host='localhost', port=8515, reloader=True)