声明:脚本以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"; } ?>

不会用啊不会用。
[回复]
我还是不用了。校内上好友得骂死我。
再严重点RT个敏感信息,被请去喝茶。。。
[回复]
话说你这Google广告感觉跟侧边栏有点不那么和谐。。。
还有很奇怪我居然会看到你网页上的广告,我安了ABP的。。。
[回复]
因为特殊加工过:-)
[回复]
很好很好阿,可惜自己没有crontab~~
[回复]
不会用啊不会用。。。
能把这个功能加到TWIP里吗
[回复]
php也可以当做shell script来用?
curl那里好像很复杂啊
[回复]
呃,这个要试一试
[回复]
我菜鸟一个,不知道怎么用
希望能集成到twip中,再做个同步到DIGU的就NB了
[回复]
$file = ‘用于记录你最后一次同步tweet id的文件路径。注意不要删除并让脚本能读取!’;
$cookie_file = ‘保留curl模拟登陆renren.com的cookie文件路径。’;
这两项怎么设置?能不能举个例子?
[回复]
$file=”/home/yegle/.lastsyncid”;
$cookie_file=”/home/yegle/.renren_cookie”;
这样:-)
[回复]
谢谢博主的方法
希望博主能写个同步到嘀咕的脚本,这样就可以同步更多的网站了
BTW,这个PHP文件放在什么位置? 只要丢在空间里就OK了?
[回复]