maccms10二开批量入库和资源打包-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

maccms10二开批量入库和资源打包

批量入库

因为maccms自带的采集采集起来很慢,而且很多资源站的采集接口不能对内容排序,导致最旧的数据最后入库。用java写个采集程序,采集完入库的时候发现不能一次性入库多个数据,导致入库也很慢,所以就在入库控制器写了个方法批量入库。

网站建设、成都做网站,成都做网站公司-创新互联已向成百上千家企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。

Receive.php

public function foo()
{
	$info = $this->_param;
	if (!array_key_exists("vods", $info) || empty($info["vods"]))
	{
	    echo json_encode(['code'=>2002,'msg'=>"vods is required"],JSON_UNESCAPED_UNICODE);
            exit;
	}
        $vods = json_decode($info["vods"], true);
        
        if (null == $vods)
        {
            echo json_encode(['code'=>2002,'msg'=>"the format of vods is incorrect"],JSON_UNESCAPED_UNICODE);
            exit;
        }
        
        $inter = mac_interface_type();
        $res = [];
    	foreach ($vods as $vod)
    	{
    		if(empty($vod['type_id'])) {
            	$vod['type_id'] = $inter['vodtype'][$vod['type_name']];
        }
        	
        $data['data'][] = $vod;
        	
    }
    $res[] = model('Collect')->vod_data([],$data,1); 
    echo json_encode($res,JSON_UNESCAPED_UNICODE);
}

资源打包

有时候需要给其他程序全量下载视频数据,有个接口会很方便

Provide.php

// format: json返回json格式数据;无或line或其他返回的数据,是一行一条数据,且每一行都是合法的json格式
public function bar()
{
        // token暂时无用
        $token = $this->_param['token'];
        $format = $this->_param['format'] == "json" ? "json" : "line";
        $suffix = $format == "json" ? ".json" : ".txt";

        // category暂时无用
        $category = $this->_param['category'];
        if (empty($category)) {
        	$category = "";
        }
        else {
        	$temp = explode(",", $category);
        	$category = "";
        	foreach ($temp as $k=>$v) {
        		if (is_numeric($v)) {
        			$category .= (($k==0 ? "" : ",") . $v);
        		}
        	}
        }
        
        $path = ROOT_PATH . "data".$suffix;
        $url = request()->domain() . "/" . "data".$suffix;
        if (is_file($path)) {
        	$mtime = (int)date("Ymd", filemtime($path));
        	$today = (int)date("Ymd", time());
        	if (filesize($path) > 0 && $mtime - $today == 0)
        	{
        		echo json_encode(["time" => $mtime, "url" => $url]);
        		exit;
        	}
        }
        
        $fp = new \SplFileObject($path, "a+b");
        if ($fp->flock(LOCK_EX)) {
			if ($fp->getSize() > 0) {
				$fp->ftruncate(0);
				$fp->fflush();
			}
				
			$count = Db::name('Vod')->count();
			$i = 0;
        	$size = 2000;
        	$temp = $format == "json" ? array() : "";
        	while ($i<$count) {
        		$size = ($count-$i>=$size) ? $size : $count-$i;
        		$limit = $i . "," . $size;
        		// vod_class as class,
        		$list = Db::name('Vod')->field("vod_id as id,type_id as type,vod_name as name,vod_pic as pic,vod_play_url as playurl")->limit($limit)->select();
        		if ($format == "json") {
        			$temp = array_merge($temp,$list);
        		} else {
        			$temp = "";
        			foreach ($list as $k=>$v) {
        				if ($i >0 || ($i==0 && $k>0)) {
        					$temp .= "\r\n";
        				}
        				// JSON_UNESCAPED_UNICODE
						$temp .= json_encode($v);
					}
					$fp->fwrite($temp);
        			$fp->fflush();
        		}
        		$i+=$size;
        	}
        	if ($format == "json")
        	{
        		// JSON_UNESCAPED_UNICODE
        		$fp->fwrite(json_encode($temp));
        		$count = count($temp);
        		$fp->fflush();
        	} else {
        		$fp->seek(PHP_INT_MAX);
        		$count = $fp->key() + 1;
        	}
        	$fp->flock(LOCK_UN);
		$fp = null;
                clearstatcache(true, $path);
		echo json_encode(["time" => (int)date("Ymd", time()), "url" => $url, "count" => $count]);
		exit;
	}
        echo json_encode(["time" => (int)date("Ymd", time()), "url" => "", "count" => 0]);
}

当前文章:maccms10二开批量入库和资源打包
本文来源:http://kswsj.cn/article/dsogogd.html

其他资讯