用Python開發plurk機器人(自動發噗機)

  • 9732
  • 0
  • web
  • 2011-01-26

開發plurk機器人(Python)

同事的卡馬都快100了,安迪兒自從聽到同事說plurk 2小時po一次

一天連po 4次卡馬就會一直上升,一陣子就會到頂100了

安迪兒就想說那來個自動噗吧,應該蠻有趣的

查了一下官方Plurk API

發現Plurk API 支援蠻多的語法的

Implementations:

安迪兒就選了Python(為啥?因為Python比較簡短)

大家可以參考一下官方Table of contents下Python和java的範例 和很多的api屬性

Plurk dataUser data

 

製做plurk機器人整個的流程大約可以為

1.申請plurk api的 key(要有key和plurk帳號密碼才能玩)

2.安裝python和python編譯成exe檔的工具

3.修改範本程式

 

先介紹一下

1.申請plurk api

Get an API key

填入 你計畫要做什麼(隨便填)、你的full name(隨便填)、你的e-mail(填正確的)

按下 IssueAPI key 過一下就會收到mail(安迪兒是用gmail)

mail內容如下:

Hi andy
Your Plurk.com API key is:
xxxxxxxxxxxxxx

The documentation for Plurk.com can be found on:
http://www.plurk.com/API/
We hope you will use our API wisely and implement some cool stuff!
If you experience trouble please contact us on:
support@plurk.com
Kind regards,
Plurk Team

到這邊第一步就完成了。

Ps.單一API Key一天有請求上限五萬次的限制,超過的話要再申請別的api key

 

2.安裝python和python編譯成exe檔的工具

先於自已的電腦安裝 python (安迪兒是用2.7.1)

python 網頁:http://www.python.org/download/

Python 2.7.1 Windows installer (Windows binary -- does not include source)

 

3.安裝完python後就可以開始寫機器人了

安迪兒提供一下自己寫的測試程式

EX 1: [美男計時器]

code:

import urllib, urllib2, cookielib, time

OtTimstr=str(time.strftime('http://www.bijint.com/assets/pict/binan/240x320/%H%M.jpg', time.localtime(time.time())))
OtUrlstr='http://www-open-opensocial.googleusercontent.com/gadgets/proxy/refresh=3600&container=open&gadget=http%3A%2F%2Fbijint.com%2Fgadget%2Fbinan%2Fgadget.xml/'+OtTimstr+'?yyyymmdd=20101110&ext=.jpg'

constr='[咆嗚帥哥噗]'+OtUrlstr
print constr
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
api_key = '這邊填入自己申請的key'
get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
encode = urllib.urlencode

#--- Requests ----------------------------------------------
fp = opener.open(get_api_url('/Users/login'),
                 encode({'username': '這邊填入自己的plurk帳號',
                         'password': '這邊填入自己的plurk密碼',
                         'api_key': api_key}))
print fp.read()

fp = opener.open(get_api_url('/Timeline/plurkAdd'),
                 encode({'content': constr,
                         'qualifier': 'says',
                         'lang': 'tr_ch',
                         'api_key': api_key}))
print fp.read()

 

EX 2:[美女計時器]

code:

import urllib, urllib2, cookielib, time
#ISOTIMEFORMAT='%Y-%m-%d %X'
ISOTIMEFORMAT='http://www.clockm.com/tw/img/clk/hour/%H%M.jpg'
constr='[咆嗚美女噗]'+str(time.strftime(ISOTIMEFORMAT, time.localtime(time.time())))
print constr
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
api_key = '這邊填入自己申請的key'
get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
encode = urllib.urlencode

fp = opener.open(get_api_url('/Users/login'),
                 encode({'username': '這邊填入自己的plurk帳號'',
                         'password': '這邊填入自己的plurk密碼',
                         'api_key': api_key}))
print fp.read()

fp = opener.open(get_api_url('/Timeline/plurkAdd'),
                 encode({'content': constr,
                         'qualifier': 'says',
                         'lang': 'tr_ch',
                         'api_key': api_key}))
print fp.read()

 

寫完xxx.py檔,可以執行看看有沒有效果,正常的話,我們可以把它封成exe檔

可以參考下面作者的教學:

引用:如何使用py2exe將Python程式打包成.exe執行檔的教學

 

以上到這邊大家應該可以自動發噗了

安迪兒發出來的畫面大約是這樣的(1小時發1次)


 
關於計時器的圖,安迪兒是從美男計時器外貼碼 、美人克拉克 捉出來的大家有興趣可以看看~~

收工嘍