#批量入庫 因為maccms自帶的採集採集起來很慢,而且很多資源站的採集介面不能對內容排序,導致最舊的數據最後入庫。用java寫個採集程式,採集完入庫的時候發現不能一次性入庫多個數據,導致入庫也很慢,所以就在入庫控制器寫了個方法批量入庫。 Receive.php public function fo ...
批量入庫
因為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)) {
clearstatcache(true, $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;
echo json_encode(["time" => (int)date("Ymd", time()), "url" => $url, "count" => $count]);
exit;
}
echo json_encode(["time" => (int)date("Ymd", time()), "url" => "", "count" => 0]);
}