声明:脚本以CC by-nc-sa协议公布,如需转载请给出原文链接并保留脚本头部的版权声明。
声明:本人不对可能发生的您的renren.com好友被刷屏、脚本错误反复刷屏、您的好友解除与你的好友关系、您的renren.com帐号被举报等等任何不良后果负责~
100129:修正脚本的bug
网上已经有一个twitter2renren的同步网站了,地址在这里:http://twitter2renren.appspot.com/
问题有两个,一个是据说现在已经无法同步了,另一个是这个网站需要输入twitter用户名密码,不支持OAuth,这让我很不放心。于是决定自己写个脚本。
自己动手丰衣足食,不过也有问题,一是一般只用于解决自己的问题,其他人可能还需要修改才能用,二是一般都是无比quick and dirty,懒得写注释和注意排版。这次这个代码好像好多人想用,所以已经尽量考虑比较多的特殊情况,排版也稍微好看一点了……
使用说明:
根据代码中的提示,修改几个变量的值
然后尝试运行一遍脚本,如果没有报错的话,你会看到最近的推显示在屏幕上。这是转发twitter到renren.com后根据renren返回的json解码得到的信息。
根据你执行脚本时的速度选择crontab的时间间隔。我这里选的是5分钟。执行crontab -e
添加以下代码:
*/5 * * * * /path/to/twitter2renren.php 2>&1 >>/path/to/logfile
Done!
以下为代码:
< ?php // //twitter2renren.php //By @yegle, yegle.net //Licensed under CC by-nc-sa // $file = '用于记录你最后一次同步tweet id的文件路径。注意不要删除并让脚本能读取!'; $cookie_file = '保留curl模拟登陆renren.com的cookie文件路径。'; $name_pwd = '你的twitter用户名密码。格式为:yourusername:yourpassword'; $hashtag = '如果你希望在你的推里包含特殊字符串时才转过去,请设置该变量,否则只转发不是以@开头的推'; $renren_username = 'renren.com登录用户名'; $renren_password = 'renren.com登录密码'; //配置完毕,请停止编辑文件! //That's it!Stop editing anything below unless you know what you are doing! if(file_exists($file)){ $lastid = intval(file_get_contents($file)); } else{ touch($file); $lastid = 0; } $timeline_url = 'http://'.$name_pwd.'@twitter.com/statuses/user_timeline.json?count=200'; if($lastid!==0) $timeline_url.='&since_id='.$lastid; $timeline = file_get_contents($timeline_url); $arr = json_decode($timeline,TRUE); if(empty($arr)) exit(); $new_id = $arr[0]['id']; if($new_id == '') exit(); file_put_contents($file,$new_id); $post_arr = array(); foreach($arr as $tweet){ if(strpos($tweet['text'],'@')!==0){ if($hashtag!=='' && strpos($tweet['text'],$hashtag)!==FALSE){ $post_arr[] = $tweet['text'].' [twitter]'; } } } $post_arr = array_reverse($post_arr); $renren_login = 'http://passport.renren.com/PLogin.do'; $ch = curl_init($renren_login); curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); curl_setopt($ch,CURLOPT_POST,TRUE); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE); curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.$renren_username.'&password='.$renren_password.'&autoLogin=true&origURL=http://www.renren.com/Home.do&domain=renren.com'); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_exec($ch); curl_close($ch); $renren_status = 'http://status.renren.com/doing/update.do'; foreach($post_arr as $item){ $post = 'c='.urlencode($item).'&raw='.urlencode($item).'&isAtHome=0'; $ch = curl_init($renren_status); curl_setopt($ch,CURLOPT_POST,TRUE); curl_setopt($ch,CURLOPT_POSTFIELDS,$post); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE); curl_setopt($ch,CURLOPT_REFERER,'http://status.renren.com/ajaxproxy.htm'); $ret = curl_exec($ch); curl_close($ch); $ret = json_decode($ret,TRUE); echo $ret['msg']."\n"; } ?>

近期评论