Tag Archive for 'renren'

Twitter2Renren PHP同步脚本(20100828更新)

又更新了一下,使用m.renren.com进行状态发布

脚本名称正式定为twitter2renren.php

使用方法详见这篇日志

代码:

< ?php
 
//
//twitter2renren.php
//By @yegle, yegle.net
//Licensed under CC by-nc-sa
//Version:20100828
//
 
$file = '/home/yegle/.twitter2renren';
$cookie_file = '/home/yegle/.twitter2renren.cookie';
$twitter_username = '';
$twitter_pwd = '';
$hashtag = '';
$renren_username = '';
$renren_password = '';
 
//配置完毕,请停止编辑文件!
if(file_exists($file)){
    $lastid = intval(file_get_contents($file));
}
else{
    touch($file);
    $lastid = 0;
}
 
 
$timeline_url = 'http://'.$twitter_username.':'.$twitter_pwd.'@twitter.com/statuses/user_timeline.rss?count=200';
if($lastid!==0) $timeline_url.='&since_id='.$lastid;
 
 
$timeline = file_get_contents($timeline_url);
 
$obj = simplexml_load_string($timeline);
 
$items = $obj->channel->item;
if(empty($items)){
    echo "no newer tweet\n";
    exit();
}
if(sizeof($items)==1){
    $new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items->link));
}
else{
    $new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items[0]->link));
}
if($new_id == ''){
    echo "error\n";
    exit();
}
file_put_contents($file,$new_id);
$post_arr = array();
foreach($items as $tweet){
    $text = substr($tweet->title,strlen($twitter_username)+2);
    if(strpos($text,'@')!==0 && ( empty($hashtag) || strpos($text,$hashtag)!==FALSE)){
        $text = str_replace("RT: @","转自",$text);
         $post_arr[] = $text.' [twitter]';
    }
}
 
if(empty($post_arr)){
    echo "no need.\n";
    exit();
}
 
$post_arr = array_reverse($post_arr);
 
foreach($post_arr as $item){
    $post = 'sour=home&status='.urlencode($item).'&update=发布';
 
    $renren_login = "http://3g.renren.com/login.do?fx=0&autoLogin=true";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
    curl_setopt($ch,CURLOPT_URL,$renren_login);
    curl_setopt($ch,CURLOPT_POST,TRUE);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
    curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.$renren_username.'&password='.$renren_password.'&login=登录');
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    #curl_setopt($ch,CURLOPT_VERBOSE,TRUE);
    $str = curl_exec($ch);
    curl_close($ch);
    $pattern = '/action="([^"]*)"/';
    preg_match($pattern,$str,$matches);
 
 
    $ch = curl_init($matches[1]);
    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);
}
?>

Twitter同步人人脚本(Updated at 2010-04-12)

使用方法详见这篇日志
人人很贱地加了个参数,以前的脚本就失效了.没啥,在强大的正则面前,任何企图让自动化脚本失效的图谋都会被粉碎!

其实这个升级版里的正则还可以更漂亮一点…懒得优化了…

另外这个版本会把你的官方RT也转发到renren上去

Update at 2010-04-14,修改几个小bug
Update at 2010-05-27,修改几个小bug

< ?php
 
//
//twitter2renren.php
//By @yegle, yegle.net
//Licensed under CC by-nc-sa
//
 
$file = '用于记录你最后一次同步tweet id的文件路径。注意不要删除并让脚本能读取!';
$cookie_file = '保留curl模拟登陆renren.com的cookie文件路径。';
$twitter_username = '你的twitter用户名,注意是用户名,不是邮箱名';
$twitter_pwd = '你的twitter密码';
$hashtag = '如果你希望在你的推里包含特殊字符串时才转过去,请设置该变量,否则只转发不是以@开头的推';
$renren_username = 'renren.com登录用户名';
$renren_password = 'renren.com登录密码';
 
//配置完毕,请停止编辑文件!
if(file_exists($file)){
    $lastid = intval(file_get_contents($file));
}
else{
    touch($file);
    $lastid = 0;
}
 
 
$timeline_url = 'http://'.$twitter_username.':'.$twitter_pwd.'@twitter.com/statuses/user_timeline.rss?count=200';
if($lastid!==0) $timeline_url.='&since_id='.$lastid;
 
 
$timeline = file_get_contents($timeline_url);
 
$obj = simplexml_load_string($timeline);
 
$items = $obj->channel->item;
if(empty($items)){
    echo "no newer tweet\n";
    exit();
}
if(sizeof($items)==1){
    $new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items->link));
}
else{
    $new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items[0]->link));
}
if($new_id == ''){
    echo "error\n";
    exit();
}
file_put_contents($file,$new_id);
$post_arr = array();
foreach($items as $tweet){
    $text = substr($tweet->title,strlen($twitter_username)+2);
    if(strpos($text,'@')!==0 && ( empty($hashtag) || strpos($text,$hashtag)!==FALSE)){
        $text = str_replace("RT: @","转自",$text);
         $post_arr[] = $text.' [twitter]';
    }
}
 
if(empty($post_arr)){
    echo "no need.\n";
    exit();
}
 
//$post_arr = array_reverse($post_arr);
 
$renren_login = "http://passport.renren.com/PLogin.do";
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($ch,CURLOPT_URL,$renren_login);
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_setopt($ch,CURLOPT_VERBOSE,TRUE);
$str = curl_exec($ch);
$pattern = "/get_check:'([^']*)'/";
preg_match($pattern,$str,$matches);
$get_check = $matches[1];
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&publisher_form_ticket='.$get_check;
 
    $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";
}
 
?>

twitter同步到renren的php脚本

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