|
@@ -17,6 +17,8 @@ use App\Models\PostData;
|
|
use App\Models\PostImgs;
|
|
use App\Models\PostImgs;
|
|
use App\Models\PostLog;
|
|
use App\Models\PostLog;
|
|
use App\Models\PostStatistics;
|
|
use App\Models\PostStatistics;
|
|
|
|
+use App\Models\PostStore;
|
|
|
|
+use App\Models\PostStoreImgs;
|
|
use App\Models\Topic;
|
|
use App\Models\Topic;
|
|
use App\Service\RabbitMqUtil;
|
|
use App\Service\RabbitMqUtil;
|
|
use App\Traits\PostTrait;
|
|
use App\Traits\PostTrait;
|
|
@@ -46,6 +48,8 @@ class PostRepository
|
|
Behavior $behavior,
|
|
Behavior $behavior,
|
|
CategoryTopic $categoryTopic,
|
|
CategoryTopic $categoryTopic,
|
|
PostStatistics $postStatistics,
|
|
PostStatistics $postStatistics,
|
|
|
|
+ PostStore $postStore,
|
|
|
|
+ PostStoreImgs $postStoreImgs,
|
|
Topic $topic)
|
|
Topic $topic)
|
|
{
|
|
{
|
|
$this->post = $post;
|
|
$this->post = $post;
|
|
@@ -58,6 +62,8 @@ class PostRepository
|
|
$this->categoryTopic = $categoryTopic;
|
|
$this->categoryTopic = $categoryTopic;
|
|
$this->topic = $topic;
|
|
$this->topic = $topic;
|
|
$this->postStatistics = $postStatistics;
|
|
$this->postStatistics = $postStatistics;
|
|
|
|
+ $this->postStore = $postStore;
|
|
|
|
+ $this->postStoreImgs = $postStoreImgs;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1143,5 +1149,166 @@ class PostRepository
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 添加网站内容
|
|
|
|
+ */
|
|
|
|
+ public function createStore($startPage, $endPage, $size)
|
|
|
|
+ {
|
|
|
|
+ set_time_limit(1800);
|
|
|
|
+ for($i=$startPage;$i<=$endPage;$i++){
|
|
|
|
+ $this->addContent($i, $size);
|
|
|
|
+ usleep(100000);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取网站内容
|
|
|
|
+ */
|
|
|
|
+ public function addContent($page, $size)
|
|
|
|
+ {
|
|
|
|
+ $lists = $this->getAcFunList($page, $size);
|
|
|
|
+ if(empty($lists)){
|
|
|
|
+ return Response::create([
|
|
|
|
+ 'message' => '获取数据失败',
|
|
|
|
+ 'status_code' => 500
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ $data = [];
|
|
|
|
+ foreach($lists as $item){
|
|
|
|
+ $res = $this->getData($item['id'], $item['title']);
|
|
|
|
+ if($res && $res['content'] && $res['images']){
|
|
|
|
+ $data[] = array_merge(['source_id' => $item['id']], $res);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(empty($data)){
|
|
|
|
+ return Response::create([
|
|
|
|
+ 'message' => '处理数据失败',
|
|
|
|
+ 'status_code' => 500
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ $date = Carbon::now()->toDateTimeString();
|
|
|
|
+ $successNo = 0;
|
|
|
|
+ $failNo = 0;
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ foreach($data as $item){
|
|
|
|
+ if($this->postStore->where('source', 'AcFun')->where('source_id', $item['source_id'])->exists()){
|
|
|
|
+ $failNo++;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ $storeData = [
|
|
|
|
+ 'source' => 'Acfun',
|
|
|
|
+ 'source_id' => $item['source_id'],
|
|
|
|
+ 'title' => $item['title'],
|
|
|
|
+ 'content' => $item['content'],
|
|
|
|
+ 'img' => $item['images'][0],
|
|
|
|
+ ];
|
|
|
|
+ $postStore = $this->postStore->create($storeData);
|
|
|
|
+
|
|
|
|
+ $storeImgsData = [];
|
|
|
|
+ foreach($item['images'] as $img){
|
|
|
|
+ $storeImgsData[] = [
|
|
|
|
+ 'post_store_id' => $postStore->id,
|
|
|
|
+ 'img' => $img,
|
|
|
|
+ 'created_at' => $date,
|
|
|
|
+ 'updated_at' => $date,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ $this->postStoreImgs->insert($storeImgsData);
|
|
|
|
+ $successNo++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ Log::info('第'.$page.'页添加内容数据成功'.$successNo.',失败'.$failNo);
|
|
|
|
+ return Response::create();
|
|
|
|
+
|
|
|
|
+ } catch (QueryException $exception) {
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ Log::debug('添加内容数据失败:' . $exception->getMessage());
|
|
|
|
+ return Response::create([
|
|
|
|
+ 'message' => '添加内容数据失败',
|
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
|
+ 'status_code' => 500
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取网站内容
|
|
|
|
+ */
|
|
|
|
+ public function getData($id, $title = '')
|
|
|
|
+ {
|
|
|
|
+ $url = 'https://www.acfun.cn/a/ac'.$id;
|
|
|
|
+ $webpage = file_get_contents($url);
|
|
|
|
+ preg_match_all('/parts[\s\S]*?}]/i', $webpage, $res);
|
|
|
|
+ if(!isset($res[0][0])){
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ $data = $res[0][0];
|
|
|
|
+ preg_match_all('/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i ', $data, $contents);
|
|
|
|
+
|
|
|
|
+ if(isset($contents[0])){
|
|
|
|
+ $content = implode($contents[0]);
|
|
|
|
+ }else{
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ $content = strip_tags($content);
|
|
|
|
+ $content = str_replace([' '],'',$content);
|
|
|
|
+ preg_match_all('/src=(.*)>/U', $data, $imageArray);
|
|
|
|
+ $images = [];
|
|
|
|
+ if(isset($imageArray[0])){
|
|
|
|
+ foreach($imageArray[0] as $img){
|
|
|
|
+ $imgArr = explode('"',$img);
|
|
|
|
+ if(isset($imgArr[1])){
|
|
|
|
+ $imgArr[1] = trim($imgArr[1], '\\');
|
|
|
|
+ $images[] = $imgArr[1];
|
|
|
|
+ }else{
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return [
|
|
|
|
+ 'title' => $title,
|
|
|
|
+ 'content' => $content,
|
|
|
|
+ 'images' => $images,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取网站内容
|
|
|
|
+ */
|
|
|
|
+ public function getStore($request)
|
|
|
|
+ {
|
|
|
|
+ $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
|
|
+ $where = [];
|
|
|
|
+ if (isset($request['title'])) {
|
|
|
|
+ $where[] = ['title', 'like', "%{$request['title']}%"];
|
|
|
|
+ }
|
|
|
|
+ if (isset($request['content'])) {
|
|
|
|
+ $where[] = ['content', 'like', "%{$request['content']}%"];
|
|
|
|
+ }
|
|
|
|
+ if (isset($request['source'])) {
|
|
|
|
+ $where[] = ['source', $request['source']];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isset($request['is_used'])) {
|
|
|
|
+ $where[] = ['is_used', $request['is_used']];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return $this->postStore
|
|
|
|
+ ->where($where)
|
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
|
+ ->paginate($perPage);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 网站内容详情
|
|
|
|
+ */
|
|
|
|
+ public function getStoreDetail($request)
|
|
|
|
+ {
|
|
|
|
+ return $this->postStore->find($request['id']);
|
|
|
|
+ }
|
|
}
|
|
}
|