贴下自己写的采集LifeType到WordPress的代码,吼吼~~

2007-8-27  08:42

    将自己写的采集LifeType到WordPress的代码晒晒,有点乱,呵呵~~~反正自己用的,将就啦。。。

   这个是主文件:

PHP代码(lt2wp.php)
  1. <?php   
  2. /* 
  3. Power by Sharmy 
  4. #LifeType采集到WordPress程序 
  5. #需要自己设置文章路径,标题等的截取方式 
  6. #文章点击量需要安装CountPosts插件,或者不要文章点击量也可以 
  7. */  
  8.   
  9. //error_reporting(’E_ALL’);  
  10. include(‘lt2wp3_func.php’);  
  11. //标题  
  12. $ft["title"]["begin"]="<h3 class=\"title\">";  
  13. $ft["title"]["end"]="</h3>";  
  14. /*$th["title"]["百度"]="千度"; 
  15. $th["title"]["一下"]="二下";*/  
  16.   
  17. //内容  
  18. $ft["content"]["begin"]="<div class=\"main\">";  
  19. $ft["content"]["end"]="</div>";  
  20.   
  21. //发表时间  
  22. $ft["posttime"]["begin"]="<span id=’postime’>";  
  23. $ft["posttime"]["end"]="</span>";  
  24.   
  25. //查看次数  
  26. $ft["viewtime"]["begin"]="<span id=’viewtimes’>(";  
  27. $ft["viewtime"]["end"]=")</span>";  
  28.   
  29. //类别  
  30. $ft["posttype"]["begin"]="postCategoryId=";  
  31. $ft["posttype"]["end"]="&";  
  32.   
  33. //lt与wp分类的对应数组,下标为lt的分类id,数组值为wp的分类id  
  34. $type = array(  
  35.     2=>7,  
  36.     3=>2,  
  37.     4=>6,  
  38.     5=>3,  
  39.     6=>4,  
  40.     8=>5,  
  41.     10=>1,  
  42.     11=>8  
  43. );  
  44.   
  45. mysql_query("SET names ’utf8′");  
  46. for($i=1;$i<278;$i++)  
  47. {  
  48. //此处url为文章的地址,articleId为变量,其初始值和终值需要自己查看  
  49. $url="http://www.yakelie.com/blog/index.php?op=ViewArticle&articleId=".$i."&blogId=1";  
  50. $stime=microtime(true);  
  51. $rs=pick($url,$ft,$th);  
  52. $etime=microtime(true);  
  53. $content = wpautop($rs["content"]);  
  54. echo "<br />$i——————————————-<br />";  
  55. echo $title = strip_tags($rs["title"]);  
  56. $viewtime = $rs["viewtime"];  
  57.   
  58. $posttype = $type[$rs["posttype"]];  
  59. $summary = fetch_match_contents("","</p>",$rs["content"]);  
  60. $posttime = $rs["posttime"].":00";  
  61. if($title!=&&$content!=&&$posttime!=‘0000-00-00 00:00:00′) {  
  62. //插入文章数据  
  63.     $sql = "INSERT INTO `wp3_posts`(`ID`,`post_author`,`post_date`,`post_date_gmt`,`post_content`,`post_title`,`post_category`,`post_excerpt`,`post_status`,`comment_status`,`ping_status`,`post_password`,`post_name`,`to_ping`,`pinged`,`post_modified`,`post_modified_gmt`,`post_content_filtered`,`post_parent`,`guid`,`menu_order`,`post_type`,`post_mime_type`,`comment_count`) VALUES (NULL,1,’$posttime’,'$posttime’,'$content’,'$title’,0,’$summary’,'publish’,'open’,'open’,”,”,”,”,’2007-08-04 04:38:57′,’2007-08-04 04:38:57′,”,0,”,0,’post’,”,1);";  
  64.     mysql_query($sql);  
  65.   
  66. //插入文章分类数据  
  67.     $postid = mysql_insert_id();  
  68.     if(!mysql_query("INSERT INTO `wp3_post2cat` (`rel_id`,`post_id`,`category_id`) VALUES (null,$postid,$posttype)")) exit("分类出错");  
  69. }else{  
  70.     echo "发生错误在ID为$i的文章!";  
  71. }  
  72. }  
  73. ?>  

这个是一些函数文件

PHP代码
  1. <?php  
  2. $host="localhost";  
  3. $username="XXXXX";  
  4. $password="XXXXX";  
  5. $db_name="wp";  
  6. $conn = mysql_connect($host,$username,$passwordor die("MySQL can’t be connected!");  
  7. mysql_select_db($db_name,$conn);  
  8.   
  9. //获取远程lt页面内容
  10. Function fetch_urlpage_contents($url){  
  11. $c=file_get_contents($url);  
  12. return $c;  
  13. }  
  14.   
  15. //解析获取的内容
  16. Function fetch_match_contents($begin,$end,$c)  
  17. {  
  18. $begin=change_match_string($begin);  
  19. $end=change_match_string($end);  
  20. if(preg_match("/{$begin}(.*?){$end}/i",$c,$rs))  
  21. {return $rs[1];}  
  22. else {return "";}  
  23. }  
  24.   
  25. //转换字符
  26. Function change_match_string($str){  
  27. $old=array("/","$");  
  28. $new=array("\/","\$");  
  29. $str=str_replace($old,$new,$str);  
  30. return $str;  
  31. }  
  32.   
  33. //最后整理替换
  34. Function pick($url,$ft,$th)  
  35. {  
  36. $c=fetch_urlpage_contents($url);  
  37. foreach($ft as $key => $value)  
  38.     {  
  39. $rs[$key]=fetch_match_contents($value["begin"],$value["end"],$c);  
  40. if(is_array($th[$key]))  
  41.   { foreach($th[$keyas $old => $new)  
  42.    {  
  43.    $rs[$key]=str_replace($old,$new,$rs[$key]);  
  44.    }  
  45.   }  
  46.     }  
  47. return $rs;  
  48. }
  49. ?>  

 

此文一共有2条留言

  1. dobby:

    2008-5-25 15:38:17

    不错,谢谢了。拿过去参考一下,我想写个采集f2blog到wp上的东西。

  2. sharmy:

    2008-5-25 15:58:35

    等有空我把它写成一个插件

发表评论