Thursday, April 23, 2015

Instagram auto like a new post of friend's

Google App Engine 提供的免费服务可以很容易的拿来实现一些自己的小想法,比如下面的两个例子。
Instagram自动点赞
自动为你Instagram上关注的一个或几个朋友发布的新照片点赞

1. 注册成为[Instagram Developer](http://instagram.com/developer). Register Client 获得Client ID 和 Client Secret.
2. 在[Google App Engine](https://appengine.google.com/)上新建一个Application.
3. 调用Instagram的API 实现点赞功能。首先在[Jelled](http://jelled.com/instagram/lookup-user-id)上获得需要关注点赞用户的UserID. 获得用户的info, 然后找到用户最新发布的一张照片,判断是否已经点赞,若是没有,则调用like的API点赞。将整个脚本用GAE支持的webapp2 RequestHandler实现get和post。我用的是Instagram的 Media Endpoints 和 Like Endpoints API.
https://api.instagram.com/v1/users/UserID/media/recent/?access_token=ACCESS-TOKEN&count=1
返回的是Json数据,从返回的数据中得到mediaID,也就是关注用户最新照片的ID。然后like这张图片。
setLike_req = "https://api.instagram.com/v1/media/%s/likes" %(mi)
values = {'access_token':ACCESS_TOKEN}
setLike_return = json.loads(urllib2.urlopen(urllib2.Request(setLike_req,urllib.urlencode(values))).read())
4. Google App Engine Configure, Deploy. GAE方便之处就在于configure和deploy都很简单,只需在app.yaml中添加application即可。
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
  script: likeinsta.application
然后建立后台cron job,只要新建一个cron.yaml然后可设置schedule时间,如every 1 mins,程序每隔一分钟检测一下关注对象的Instagram有没有新照片。
cron: 
- description: main job 
  url: /likeinsta 
  schedule: every 1 mins 
Code: likeinsta-Github
Useful link: python-instagram
Related: 为女神微博点赞

No comments:

Post a Comment