Browse Source

api/addfav for theme tajian is ready

master
filesite 1 year ago
parent
commit
39588b1eb5
  1. 3
      .gitignore
  2. 1
      conf/app.php
  3. 136
      themes/tajian/controller/ApiController.php

3
.gitignore vendored

@ -3,4 +3,5 @@ test/content/ @@ -3,4 +3,5 @@ test/content/
runtime/
www/navs/
www/girls/
www/videos/
www/videos/
www/tajian/

1
conf/app.php

@ -52,6 +52,7 @@ $configs = array( @@ -52,6 +52,7 @@ $configs = array(
'tajian' => array(
'data_dir' => 'data/', //数据目录
'tag_dir' => 'tags/', //tag分类目录
'task_dir' => 'task/', //分享视频下载任务文件保存目录
),
//目前支持的皮肤

136
themes/tajian/controller/ApiController.php

@ -37,4 +37,140 @@ Class ApiController extends SiteController { @@ -37,4 +37,140 @@ Class ApiController extends SiteController {
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}
/*
* 参数:
* content: 从抖音或其它平台复制出来的视频分享内容,或者视频网址
* title: 视频标题
* tag: 分类名称
* tagid: 分类id
* 其中title、tag和tagid为可选值。
*/
public function actionAddfav() {
$content = $this->post('content', '');
$title = $this->post('title', '');
$tag = $this->post('tag', '');
$tagid = $this->post('tagid', '');
$code = 1;
$msg = '';
$err = '';
//方便测试
if (empty($content)) {
$content = $this->get('content', '');
}
if (empty($tag)) {
$tag = $this->get('tag', '');
}
if (empty($content)) {
$code = 0;
$err = '请粘贴填写分享内容!';
}
$tagName = '';
if (!empty($tag) || !empty($tagid)) { //检查分类名称或id是否存在
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
//获取tags分类
$tags = $this->getTags($dirTree);
if (!empty($tagid) && empty($tags[$tagid])) { //检查tagid是否存在
$code = 0;
$err = "分类ID {$tagid} 不存在!";
}
if (!empty($tag)) { //检查tag是否存在
$tag_exists = false;
foreach($tags as $id => $item) {
if ($item['name'] == $tag) {
$tag_exists = true;
$tagName = $tag;
break;
}
}
if ($tag_exists == false) {
$code = 0;
$err = "分类 {$tag} 不存在!";
}
}
if (empty($tagName) && !empty($tags[$tagid])) {
$tagName = $tags[$tagid]['name'];
}
}
if ($code == 1) { //保存视频
$msg = $this->saveShareVideo($content, $title, $tagName) ? '视频保存完成。' : '视频保存失败!';
}
return $this->renderJson(compact('code', 'msg', 'err'));
}
protected function getVideoId($url) {
return md5($url);
}
//保存分享视频
protected function saveShareVideo($content, $title, $tagName) {
$done = true;
$newVideo = array();
preg_match("/https:\/\/[\w\.]+\/\w+/i", $content, $matches);
if (!empty($matches)) {
$newVideo['url'] = $matches[0];
$done = $done && $this->saveBotTask($newVideo['url']);
}
if (!empty($tagName)) {
$done = $done && $this->saveVideoToTag($newVideo['url'], $tagName);
}
return $done;
}
//保存分享视频到任务文件
protected function saveBotTask($url) {
$task_dir = __DIR__ . '/../../../runtime/' . FSC::$app['config']['tajian']['task_dir'];
if (!is_dir($task_dir)) {
mkdir($task_dir, 0755, true);
}
$video_id = $this->getVideoId($url);
$filepath = "{$task_dir}/{$video_id}.task";
return file_put_contents($filepath, $url) !== false;
}
//保存分享视频到tag分类
//TODO: 如果高并发,需要避免数据被覆盖的问题
protected function saveVideoToTag($url, $tagName) {
$tag_dir = __DIR__ . '/../../../www/'
. FSC::$app['config']['content_directory']
. '/' . FSC::$app['config']['tajian']['tag_dir'];
if (!is_dir($tag_dir)) {
mkdir($tag_dir, 0755, true);
}
$video_id = $this->getVideoId($url);
$filepath = "{$tag_dir}/{$tagName}.txt";
if (file_exists($filepath)) {
$content = file_get_contents($filepath);
$videos = explode("\n", $content);
$last_id = array_pop($videos);
if (!empty($last_id)) {
array_push($videos, $last_id);
}
if (!in_array($video_id, $videos)) {
array_push($videos, $video_id);
}
return file_put_contents($filepath, implode("\n", $videos)) !== false;
}else {
return file_put_contents($filepath, $vidoe_id) !== false;
}
}
}

Loading…
Cancel
Save