批量入库
因为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]);
}
文章评论