文章抓取
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
3.0 KiB

<?php
namespace app\controller;
use app\BaseController;
use app\model\{Article, Expert};
use think\facade\Db;
class ScriptController extends BaseController
{
public function getArticle(){
//获取已就绪文章列表
$list = Article::where('status', 2)->field("*")->select()->toArray();
if(empty($list)){
}
//创建待处理目录
$fileName = date('Y-m-d_H:i:s');
$pathSrc = getcwd() . '/uploads/' . $fileName;
mkdir($pathSrc, 0777, true);
foreach ($list as $value) {
if(empty($value['account']))continue;
$uuid = strtoupper(md5($value['id']));
//创建文件夹
$artSrc = $pathSrc . '/' . $uuid;
if (!is_dir($artSrc)) { //若目录不存在则创建之
mkdir($artSrc, 0777, true);
}
//url图片保存本地
if(empty($value['cover'])){
copy(getcwd() . '/static/empty.png', $artSrc . '/b_封面图.jpg');
}else{
$this->down_img($artSrc, $value['cover']);
}
//文章内容
$wordname = $artSrc . "/a_文章.doc";
//html标签类文章转成word
$html = $value['content'];
$this->start();
echo $html;
$this->save($wordname);
//设置信息
file_put_contents($artSrc . '/c_信息.txt', $value['id'] . "\r\n" .$value['title']);
//设置账号
file_put_contents($artSrc . '/d_账号.txt', $this->getAccounts($value['account']));
//更改状态为已下载
Article::where('id', $value['id'])->update(['status' => 3]);
}
}
function start()
{
ob_start();
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">';
}
function save($path)
{
echo "</html>";
$data = ob_get_contents();
ob_end_clean();
$this->wirtefile($path, $data);
}
function wirtefile($fn, $data)
{
$fp = fopen($fn, "wb");
fwrite($fp, $data);
fclose($fp);
}
function down_img($path,$img_url){
ob_start();
readfile($img_url);
$img=ob_get_contents();
ob_end_clean();
$filename = '/b_封面图.jpg';
$fp2=@fopen($path . $filename,'a');
fwrite($fp2,$img);
fclose($fp2);
unset($img,$url);
}
private function getAccounts($accountIdStr){
$content = [];
$accountIds = explode(',', $accountIdStr);
$accounts = Expert::where('id', 'in', $accountIds)->field("*")->select()->toArray();
foreach($accounts as $item){
$content[] = $item['platform_code'] . '|' . $item['script_x'] . '|' . $item['script_y'];
}
return implode("\r\n", $content);
}
}