s.php,s.php · yelstr/crawl - Gitee.com

这段PHP代码实现了一个定时任务,用于批量获取电影详情并更新到数据库中。通过HTTP GET请求获取电影的详细信息,然后解析数据提取播放URL,再将这些信息插入到`ymr_video_detail`表中。整个过程处理了错误日志记录,并使用了swoole_process创建子进程来提高效率。
摘要由CSDN通过智能技术生成

require_once __DIR__ . '/vendor/autoload.php';

const LOG_FILE = __DIR__ . '/runtime/d_log.log';

function http_get($url, $timeout = 30)

{

$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$res = curl_exec($ch);

curl_close($ch);

return $res;

}

function get_detail_data($res)

{

$m_str = '';

preg_match("/url:\s.*/i", $res, $match);

if ($match) {

$m_str = str_replace(["\r", "'"], '', $match[0]);

$m_str = substr($m_str, 5);

}

return $m_str;

}

function insert_data($data)

{

$user = 'root';

// $pwd = 'wu198702';

$pwd = '123456';

$db = 'ymr';

$mysql = new mysqli('127.0.0.1', $user, $pwd, $db, 3306);

$q_sql = sprintf('select id from ymr_video_detail where local_video_id="%s" and play_url="%s"',$data['local_video_id'],$data['play_url']);

$q = $mysql->query($q_sql);

$q_res = $q->fetch_all();

if ($q_res) {

return 0;

}

$sql = sprintf('insert into ymr_video_detail

(local_video_id,`name`,play_url,created_by,modified_by)

value("%s","%s","%s","","")

', $data['local_video_id'] ?? '', $data['name'] ?? '', $data['play_url'] ?? '');

$mysql->query($sql);

if ($mysql->errno) {

$msg = sprintf('mysql error:%s,[insert_error] videoId:%s,sql:%s', $mysql->error, $data['local_video_id'], $sql);

file_put_contents(LOG_FILE, $msg, FILE_APPEND);

}

return $mysql->affected_rows;

}

function get_movies($id = 0)

{

$user = 'root';

// $pwd = 'wu198702';

$pwd = '123456';

$db = 'ymr';

$mysql = new mysqli('127.0.0.1', $user, $pwd, $db, 3306);

$sql = sprintf("select id,video_id,`name`,source_play_url from ymr_video where id>%d limit 100", $id);

$result = $mysql->query($sql);

if ($mysql->errno) {

$msg = sprintf('mysql error:%s', $mysql->error);

file_put_contents(LOG_FILE, $msg, FILE_APPEND);

}

return $result->fetch_all(MYSQLI_ASSOC);

}

class MovieDetailClean

{

public $base_url = 'http://www.bdfuli.com/dianying/index_';

public $log_file = __DIR__ . '/runtime/d_log.log';

public $pid;

public $start_id = 0;

public $step = 100;

public function __construct($start_id)

{

$this->start_id = $start_id;

try {

$this->pid = posix_getpid();

$this->run();

} catch (\Exception $e) {

die('ALL ERROR: ' . $e->getMessage());

}

}

public function run()

{

$process = new swoole_process(function () {

while (1) {

$movies = get_movies($this->start_id);

if (!$movies) {

break;

}

try {

foreach ($movies as $movie) {

if (!$movie['source_play_url']) {

continue;

}

$res = http_get($movie['source_play_url']);

$playUrl = get_detail_data($res);

$data['local_video_id'] = $movie['id'];

$data['name'] = $movie['name'];

$data['play_url'] = $playUrl;

$a = insert_data($data);

}

$this->start_id += $this->step;

} catch (\Throwable $e) {

$str = sprintf("[%s] error_msg:%s\n", date('Y-m-d H:i:s'), $e->getMessage());

file_put_contents(LOG_FILE, $str, FILE_APPEND);

}

}

});

$process->start();

}

}

if ($argc < 2) {

exit('input param error!');

}

$start_id = $argv[1];

//更新视频源播放地址

new MovieDetailClean($start_id);

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值