使用sae定时任务实现终身自动发表微博(PHP实现)

来源:互联网 发布:mac 声音调节 编辑:程序博客网 时间:2024/05/16 18:01

使用sae定时任务实现终身自动发表微博(PHP实现)

实例环境:

新浪微博sae平台申请的二级开发域名,使用thinkphp框架。


关于thinkphp到sae的代码移植这里不讨论。

关于sae的授权认证,这里不讨论。

关于sae的mysql,kvdb如何使用,这里不讨论。

以上入门知识请参看:

http://doc.thinkphp.cn/manual/sae_intro.html


原理:

任务1:讲时间线的微博抓取,存到缓存;

任务2:从缓存取出第一条,发表,从缓存删除。





本应用预备的数据库:

sae_wid:

字段 类型 说明

wid   long long 授权帐号的uid

token var char 授权帐号的access_token

其他字段略(expired_in,auth_time,screen_name,...)


在ThinkPHP中直接加Action,不用加Tpl。

这里直接上代码:

/* GraspAction.php */

include_once( APP_PATH.'/Conf/config_svn.php' );

import('@.ORG.saetv2');


/**

 * 随机抓取微博

 */


class GraspAction extends Action {


protected function _initialize() {

header("Content-Type:text/html; charset=utf-8");

}


public $myacct = '14********';//微博

public $listen_list = array(

'微博昵称1',

'微博名称2',

'**笑话**',

'我的前任**',

'搞笑热咨询',

'趣闻搞笑',

'搞笑图片会',

);

// 从时间线抓取微博

public function mining()

{

$uid = $this->myacct;

// 从数据库查找 myacct 的 token

// 获取 uid 的 accessToken

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 将时间线的有名微博抓出来

$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);

$ms = $c->home_timeline();

if (isset($ms['error']))

{

$this->error($ms['error']);

}

dump(count($ms['statuses']));

$line = $ms['statuses'];

$houxuan = array();

for ($i=0; $i<count($line); ++$i)

{

if (in_array($line[$i]['user']['name'], $this->listen_list))

{

$houxuan[] = $line[$i];

}

}

// 存入缓存

F('weibohouxuan', json_encode($houxuan));

print_r('F(weibohouxuan)='.json_encode(F('weibohouxuan')));

}

// 将缓存的候选内容发表到微博,并删除

public function put()

{

// 获取 token

$uid = $this->myacct;

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 获取缓存内容列表

$houxuan = json_decode(F('weibohouxuan'), true);

// 取出一条 并从缓存列表删除

$one = array_shift($houxuan);

//dump($one);return false;

if (empty($one))

{

$this->error('No candicate weibo content!');

}

$text = $one['text'];

//dump($text);return false;

$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);

// 如果有图片,使用图片发布接口

if (isset($one['original_pic']) && !empty($one['original_pic']))

{

//$ret = $c->upload_url_text($text, $one['original_pic']);

$ret = $c->upload($text, $one['original_pic']);

print_r('put pic :'.json_encode($ret));

}

else // 无图片 使用无图片接口

{

$ret = $c->update($text);

print_r('put :'.json_encode($ret));

}

F('weibohouxuan', json_encode($houxuan));

}

// 将缓存的候选内容显示

public function show()

{

// 获取 token

$uid = $this->myacct;

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 获取缓存内容列表

$houxuan = json_decode(F('weibohouxuan'), true);

dump($houxuan);

return true;

}

}

/* GraspAction.php over */

下面是sae的cron设置,对应文件 config.yaml

name: dajiayiqier

version: 2

handle:

- directoryindex:  index.php 

accesskey: dajiayiqier.sinaapp.com

cron:

    - description: another cron put

    url: w/index.php/Grasp/put

    schedule: */55 * * * * 

    - description: another cron get

    url: w/index.php/Grasp/minnine

    schedule: 15 */6 * * * 

关于crontab格式,不介绍,贴吧中不允许发太多连接,自己查。


祝大家微博玩得愉快!


0 0
原创粉丝点击