Advertisement
Guest User

Python Instagram

a guest
Oct 20th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import bottle_session
  3. import bottle
  4. from bottle import route, redirect, post, run, request
  5. from instagram import client, subscriptions
  6.  
  7. bottle.debug(True)
  8.  
  9. app = bottle.app()
  10. plugin = bottle_session.SessionPlugin(cookie_lifetime=600)
  11. app.install(plugin)
  12.  
  13. CONFIG = {
  14. 'client_id': '<client_id>',
  15. 'client_secret': '<client_secret>',
  16. 'redirect_uri': 'http://localhost:8515/oauth_callback'
  17. }
  18.  
  19. unauthenticated_api = client.InstagramAPI(**CONFIG)
  20.  
  21. def process_tag_update(update):
  22. print update
  23.  
  24. reactor = subscriptions.SubscriptionsReactor()
  25. reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update)
  26.  
  27. @route('/')
  28. def home():
  29. try:
  30. url = unauthenticated_api.get_authorize_url(scope=["likes","comments"])
  31. return '<a href="%s">Connect with Instagram</a>' % url
  32. except Exception, e:
  33. print e
  34.  
  35. def get_nav():
  36. nav_menu = ("<h1>Python Instagram</h1>"
  37. "<ul>"
  38. "<li><a href='/recent'>User Recent Media</a> Calls user_recent_media - Get a list of a user's most recent media</li>"
  39. "<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>"
  40. "<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>"
  41. "<li><a href='/media_search'>Media Search</a> Calls media_search - Get a list of media close to a given latitude and longitude</li>"
  42. "<li><a href='/media_popular'>Popular Media</a> Calls media_popular - Get a list of the overall most popular media items</li>"
  43. "<li><a href='/user_search'>User Search</a> Calls user_search - Search for users on instagram, by name or username</li>"
  44. "<li><a href='/user_follows'>User Follows</a> Get the followers of @instagram uses pagination</li>"
  45. "<li><a href='/location_search'>Location Search</a> Calls location_search - Search for a location by lat/lng</li>"
  46. "<li><a href='/tag_search'>Tags</a> Search for tags, view tag info and get media by tag</li>"
  47. "</ul>")
  48.  
  49. return nav_menu
  50.  
  51. @route('/oauth_callback')
  52. def on_callback(session):
  53. code = request.GET.get("code")
  54. if not code:
  55. return 'Missing code'
  56. try:
  57. access_token, user_info = unauthenticated_api.exchange_code_for_access_token(code)
  58. print "access token= " + access_token
  59. if not access_token:
  60. return 'Could not get access token'
  61. api = client.InstagramAPI(access_token=access_token)
  62. session['access_token']=access_token
  63. except Exception, e:
  64. print e
  65. return get_nav()
  66.  
  67. @route('/recent')
  68. def on_recent(session):
  69. access_token = session.get('access_token')
  70. content = "<h2>User Recent Media</h2>"
  71. if not access_token:
  72. return 'Missing Access Token'
  73. try:
  74. api = client.InstagramAPI(access_token=access_token)
  75. recent_media, next = api.user_recent_media()
  76. photos = []
  77. for media in recent_media:
  78. photos.append('<div style="float:left;">')
  79. if(media.type == 'video'):
  80. photos.append('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (media.get_standard_resolution_url()))
  81. else:
  82. photos.append('<img src="%s"/>' % (media.get_low_resolution_url()))
  83. print media
  84. 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))
  85. content += ''.join(photos)
  86. except Exception, e:
  87. print e
  88. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  89.  
  90. @route('/media_like/<id>')
  91. def media_like(session,id):
  92. access_token = session.get('access_token')
  93. api = client.InstagramAPI(access_token=access_token)
  94. api.like_media(media_id=id)
  95. redirect("/recent")
  96.  
  97. @route('/media_unlike/<id>')
  98. def media_unlike(session,id):
  99. access_token = session.get('access_token')
  100. api = client.InstagramAPI(access_token=access_token)
  101. api.unlike_media(media_id=id)
  102. redirect("/recent")
  103.  
  104. @route('/user_media_feed')
  105. def on_user_media_feed(session):
  106. access_token = session.get('access_token')
  107. content = "<h2>User Media Feed</h2>"
  108. if not access_token:
  109. return 'Missing Access Token'
  110. try:
  111. api = client.InstagramAPI(access_token=access_token)
  112. media_feed, next = api.user_media_feed()
  113. photos = []
  114. for media in media_feed:
  115. photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
  116. counter = 1
  117. while next and counter < 3:
  118. media_feed, next = api.user_media_feed(with_next_url=next)
  119. for media in media_feed:
  120. photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
  121. counter += 1
  122. content += ''.join(photos)
  123. except Exception, e:
  124. print e
  125. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  126.  
  127. @route('/location_recent_media')
  128. def location_recent_media(session):
  129. access_token = session.get('access_token')
  130. content = "<h2>Location Recent Media</h2>"
  131. if not access_token:
  132. return 'Missing Access Token'
  133. try:
  134. api = client.InstagramAPI(access_token=access_token)
  135. recent_media, next = api.location_recent_media(location_id=514276)
  136. photos = []
  137. for media in recent_media:
  138. photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
  139. content += ''.join(photos)
  140. except Exception, e:
  141. print e
  142. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  143.  
  144. @route('/media_search')
  145. def media_search(session):
  146. access_token = session.get('access_token')
  147. content = "<h2>Media Search</h2>"
  148. if not access_token:
  149. return 'Missing Access Token'
  150. try:
  151. api = client.InstagramAPI(access_token=access_token)
  152. media_search = api.media_search(lat="37.7808851",lng="-122.3948632",distance=1000)
  153. photos = []
  154. for media in media_search:
  155. photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
  156. content += ''.join(photos)
  157. except Exception, e:
  158. print e
  159. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  160.  
  161. @route('/media_popular')
  162. def media_popular(session):
  163. access_token = session.get('access_token')
  164. content = "<h2>Popular Media</h2>"
  165. if not access_token:
  166. return 'Missing Access Token'
  167. try:
  168. api = client.InstagramAPI(access_token=access_token)
  169. media_search = api.media_popular()
  170. photos = []
  171. for media in media_search:
  172. photos.append('<img src="%s"/>' % media.get_standard_resolution_url())
  173. content += ''.join(photos)
  174. except Exception, e:
  175. print e
  176. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  177.  
  178. @route('/user_search')
  179. def user_search(session):
  180. access_token = session.get('access_token')
  181. content = "<h2>User Search</h2>"
  182. if not access_token:
  183. return 'Missing Access Token'
  184. try:
  185. api = client.InstagramAPI(access_token=access_token)
  186. user_search = api.user_search(q="Instagram")
  187. users = []
  188. for user in user_search:
  189. users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
  190. content += ''.join(users)
  191. except Exception, e:
  192. print e
  193. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  194.  
  195. @route('/user_follows')
  196. def user_follows(session):
  197. access_token = session.get('access_token')
  198. content = "<h2>User Follows</h2>"
  199. if not access_token:
  200. return 'Missing Access Token'
  201. try:
  202. api = client.InstagramAPI(access_token=access_token)
  203. # 25025320 is http://instagram.com/instagram
  204. user_follows, next = api.user_follows('25025320')
  205. users = []
  206. for user in user_follows:
  207. users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
  208. while next:
  209. user_follows, next = api.user_follows(with_next_url=next)
  210. for user in user_follows:
  211. users.append('<li><img src="%s">%s</li>' % (user.profile_picture,user.username))
  212. content += ''.join(users)
  213. except Exception, e:
  214. print e
  215. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  216.  
  217. @route('/location_search')
  218. def location_search(session):
  219. access_token = session.get('access_token')
  220. content = "<h2>Location Search</h2>"
  221. if not access_token:
  222. return 'Missing Access Token'
  223. try:
  224. api = client.InstagramAPI(access_token=access_token)
  225. location_search = api.location_search(lat="37.7808851",lng="-122.3948632",distance=1000)
  226. locations = []
  227. for location in location_search:
  228. 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))
  229. content += ''.join(locations)
  230. except Exception, e:
  231. print e
  232. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  233.  
  234. @route('/tag_search')
  235. def tag_search(session):
  236. access_token = session.get('access_token')
  237. content = "<h2>Tag Search</h2>"
  238. if not access_token:
  239. return 'Missing Access Token'
  240. try:
  241. api = client.InstagramAPI(access_token=access_token)
  242. tag_search, next_tag = api.tag_search(q="catband")
  243. tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name)
  244. photos = []
  245. for tag_media in tag_recent_media:
  246. photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url())
  247. content += ''.join(photos)
  248. except Exception, e:
  249. print e
  250. return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
  251.  
  252. @route('/realtime_callback')
  253. @post('/realtime_callback')
  254. def on_realtime_callback():
  255. mode = request.GET.get("hub.mode")
  256. challenge = request.GET.get("hub.challenge")
  257. verify_token = request.GET.get("hub.verify_token")
  258. if challenge:
  259. return challenge
  260. else:
  261. x_hub_signature = request.header.get('X-Hub-Signature')
  262. raw_response = request.body.read()
  263. try:
  264. reactor.process(CONFIG['client_secret'], raw_response, x_hub_signature)
  265. except subscriptions.SubscriptionVerifyError:
  266. print "Signature mismatch"
  267.  
  268. run(host='localhost', port=8515, reloader=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement