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
原始数据
按行查看
历史