贴下自己写的采集LifeType到WordPress的代码,吼吼~~
2007-8-27 08:42将自己写的采集LifeType到WordPress的代码晒晒,有点乱,呵呵~~~反正自己用的,将就啦。。。
这个是主文件:
PHP代码(lt2wp.php)
- <?php
- /*
- Power by Sharmy
- #LifeType采集到WordPress程序
- #需要自己设置文章路径,标题等的截取方式
- #文章点击量需要安装CountPosts插件,或者不要文章点击量也可以
- */
- //error_reporting(’E_ALL’);
- include(‘lt2wp3_func.php’);
- //标题
- $ft["title"]["begin"]="<h3 class=\"title\">";
- $ft["title"]["end"]="</h3>";
- /*$th["title"]["百度"]="千度";
- $th["title"]["一下"]="二下";*/
- //内容
- $ft["content"]["begin"]="<div class=\"main\">";
- $ft["content"]["end"]="</div>";
- //发表时间
- $ft["posttime"]["begin"]="<span id=’postime’>";
- $ft["posttime"]["end"]="</span>";
- //查看次数
- $ft["viewtime"]["begin"]="<span id=’viewtimes’>(";
- $ft["viewtime"]["end"]=")</span>";
- //类别
- $ft["posttype"]["begin"]="postCategoryId=";
- $ft["posttype"]["end"]="&";
- //lt与wp分类的对应数组,下标为lt的分类id,数组值为wp的分类id
- $type = array(
- 2=>7,
- 3=>2,
- 4=>6,
- 5=>3,
- 6=>4,
- 8=>5,
- 10=>1,
- 11=>8
- );
- mysql_query("SET names ’utf8′");
- for($i=1;$i<278;$i++)
- {
- //此处url为文章的地址,articleId为变量,其初始值和终值需要自己查看
- $url="http://www.yakelie.com/blog/index.php?op=ViewArticle&articleId=".$i."&blogId=1";
- $stime=microtime(true);
- $rs=pick($url,$ft,$th);
- $etime=microtime(true);
- $content = wpautop($rs["content"]);
- echo "<br />$i——————————————-<br />";
- echo $title = strip_tags($rs["title"]);
- $viewtime = $rs["viewtime"];
- $posttype = $type[$rs["posttype"]];
- $summary = fetch_match_contents("","</p>",$rs["content"]);
- $posttime = $rs["posttime"].":00";
- if($title!=”&&$content!=”&&$posttime!=‘0000-00-00 00:00:00′) {
- //插入文章数据
- $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);";
- mysql_query($sql);
- //插入文章分类数据
- $postid = mysql_insert_id();
- if(!mysql_query("INSERT INTO `wp3_post2cat` (`rel_id`,`post_id`,`category_id`) VALUES (null,$postid,$posttype)")) exit("分类出错");
- }else{
- echo "发生错误在ID为$i的文章!";
- }
- }
- ?>
这个是一些函数文件
PHP代码
- <?php
- $host="localhost";
- $username="XXXXX";
- $password="XXXXX";
- $db_name="wp";
- $conn = mysql_connect($host,$username,$password) or die("MySQL can’t be connected!");
- mysql_select_db($db_name,$conn);
- //获取远程lt页面内容
- Function fetch_urlpage_contents($url){
- $c=file_get_contents($url);
- return $c;
- }
- //解析获取的内容
- Function fetch_match_contents($begin,$end,$c)
- {
- $begin=change_match_string($begin);
- $end=change_match_string($end);
- if(preg_match("/{$begin}(.*?){$end}/i",$c,$rs))
- {return $rs[1];}
- else {return "";}
- }
- //转换字符
- Function change_match_string($str){
- $old=array("/","$");
- $new=array("\/","\$");
- $str=str_replace($old,$new,$str);
- return $str;
- }
- //最后整理替换
- Function pick($url,$ft,$th)
- {
- $c=fetch_urlpage_contents($url);
- foreach($ft as $key => $value)
- {
- $rs[$key]=fetch_match_contents($value["begin"],$value["end"],$c);
- if(is_array($th[$key]))
- { foreach($th[$key] as $old => $new)
- {
- $rs[$key]=str_replace($old,$new,$rs[$key]);
- }
- }
- }
- return $rs;
- }
- ?>
通过RSS 2.0来获取此文章的最新评论.
dobby:
2008-5-25 15:38:17
不错,谢谢了。拿过去参考一下,我想写个采集f2blog到wp上的东西。
sharmy:
2008-5-25 15:58:35
等有空我把它写成一个插件