Last active
January 8, 2024 06:13
-
-
Save tofuliang/4d3fac057d68a6eb50ee7fe21960b42b to your computer and use it in GitHub Desktop.
convert raw iptv list to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* guangzhou.txt 格式如下: | |
* | |
* 广东珠江超清<=>rtsp://183.59.156.50/PLTV/88888905/224/3221229854/10000100000000060000000009467088_0.smil | |
* 广东南方卫视<=>rtsp://183.59.156.50/PLTV/88888905/224/3221226975/00000100000000060000000000554907_0.smil | |
* 广东少儿<=>rtsp://183.59.156.50/PLTV/88888905/224/3221226989/00000100000000060000000000554886_0.smil | |
*/ | |
$data = file_get_contents('guangzhou.txt'); | |
$lines = explode("\n", $data); | |
$arr = [ | |
'CCTV5+' => 'CCTV5+', | |
'CCTV-10' => 'CCTV-10科教', | |
'CCTV-11' => 'CCTV-11戏曲', | |
'CCTV-12' => 'CCTV-12社会与法', | |
'CCTV-13' => 'CCTV-13新闻', | |
'CCTV-14' => 'CCTV-14少儿', | |
'CCTV-15' => 'CCTV-15音乐', | |
'CCTV-17' => 'CCTV-17农业', | |
'CCTV-1' => 'CCTV-1综合', | |
'CCTV-2' => 'CCTV-2财经', | |
'CCTV-3' => 'CCTV-3综艺', | |
'CCTV-4' => 'CCTV-4中文国际', | |
'CCTV-5' => 'CCTV-5体育', | |
'CCTV-6' => 'CCTV-6电影', | |
'CCTV-7' => 'CCTV-7国防军事', | |
'CCTV-8' => 'CCTV-8电视剧', | |
'CCTV-9' => 'CCTV-9纪录', | |
]; | |
$exclude = [ | |
['广州教育'], | |
['购物'], | |
['动画'], | |
['卡通'], | |
"收视指南", | |
"魅力时尚", | |
"靓妆", | |
"茶", | |
"早期教育", | |
"女性时尚", | |
"电视指南", | |
]; | |
$titleArr = array_keys($arr); | |
$groups = []; | |
function cmp($a, $b) | |
{ | |
if ( | |
(strpos($b, '超清') !== false && strpos($a, '超清') !== false) || | |
(strpos($b, '高清') !== false && strpos($a, '高清') !== false) || | |
(strpos($b, '清') === false && strpos($a, '清') === false)) { | |
return 0; | |
} elseif ( | |
(strpos($b, '超清') !== false && strpos($a, '超清') === false) || | |
(strpos($b, '清') !== false && strpos($a, '清') === false)) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
function cmp2($b, $a) | |
{ | |
$b = trim($b); | |
$a = trim($a); | |
$order = [ | |
'广州', | |
'珠江', | |
'江门', | |
'广东', | |
'粤', | |
'CCTV', | |
'卫视', | |
]; | |
$aindex = $bindex = []; | |
foreach ($order as $index => $match) { | |
if (strpos($a, $match) !== false) { | |
$aindex[] = $index; | |
} | |
} | |
foreach ($order as $index => $match) { | |
if (strpos($b, $match) !== false) { | |
$bindex[] = $index; | |
} | |
} | |
$ac = count($aindex); | |
$bc = count($bindex); | |
// 比较谁符合的优先词多 | |
if ($ac === $bc && $ac > 0) { // 有符合的优先词且一样多 | |
if (($i = (array_sum($bindex) - array_sum($aindex))) === 0) { // 符合的优先词完全一样 | |
if (mb_substr($a, 0, 3) === mb_substr($b, 0, 3)) { // 前3个字符一样 | |
$i = strnatcasecmp($b, $a); // 由第4个字符决定排序 | |
} else { | |
if (($i = (strlen($a) - strlen($b))) === 0) { // 前3个字符不一样但长度一样 | |
$i = strnatcasecmp($b, $a); // 由第4个字符决定排序 | |
} | |
} | |
} | |
/* | |
else{ | |
// 符合的优先词不完全一样,由符合字符优先级排序 | |
}*/ | |
} else { // 无符合的优先词 或 有符合的优先词且不一样多 | |
if (($i = ($ac - $bc)) == 0) { // 无符合的优先词 | |
if (mb_substr($a, 0, 3) === mb_substr($b, 0, 3)) { // 前3个字符一样 | |
$i = strnatcasecmp($b, $a); // 由第4个字符决定排序 | |
} else { | |
if (($i = (strlen($a) - strlen($b))) === 0) { // 前3个字符不一样但长度一样 | |
$i = strnatcasecmp($b, $a);// 由第4个字符决定排序 | |
} | |
} | |
} | |
// else { | |
// 有符合的优先词且不一样多,符合多的排前 | |
// var_dump($aindex,$bindex); | |
// } | |
} | |
return $i; | |
} | |
function checkTitle($arr, $title) | |
{ | |
foreach ($arr as $t) { | |
if (strpos($title, $t) !== false) { | |
return $t; | |
} | |
} | |
return false; | |
} | |
function checkHide($exclude, $title): bool | |
{ | |
foreach ($exclude as $e) { | |
if (is_array($e)) { | |
if (strpos($title, $e[0]) !== false) { | |
return true; | |
} | |
} | |
if ($e == $title) { | |
return true; | |
} | |
} | |
return false; | |
} | |
foreach ($lines as $line) { | |
if (empty($line)|| stripos($line, 'AVS') !== false) { | |
continue; | |
} | |
$line = preg_replace("/CCTV\s+(-\d+)/", "CCTV$1", $line); | |
$line = str_replace("-测试", "", $line); | |
$line = str_replace("测试", "", $line); | |
[$title, $url] = explode("<=>", $line); | |
if (($short = mb_ereg_replace("\s*(高|超高|超])清$", "", $title)) !== $title) { | |
if (!isset($arr[$short]) && stripos($short, 'CCTV') === false) { | |
// echo $short.PHP_EOL; | |
$arr[$short] = $short; | |
$titleArr[] = $short; | |
} | |
} | |
$url = urldecode($url); | |
$parts = parse_url($url); | |
parse_str($parts['query'], $query); | |
$url = $parts["scheme"] . '://' . $parts["host"] .':' . $parts["port"]. $parts["path"]; | |
// $url = $parts["scheme"] . '://' . $parts["host"] .':' . $parts["port"]. $parts["path"] . '?accountinfo=' . $query["accountinfo"] . '&tenantId=' . $query["tenantId"]; | |
if ($t = checkTitle($titleArr, $title)) { | |
$groups[$t][$title] = $url; | |
} else { | |
$groups[$title][] = $url; | |
} | |
} | |
foreach ($groups as $stdTitle => $group) { | |
uksort($group, 'cmp'); | |
$title = $arr[$stdTitle] ?? $stdTitle; | |
$list[] = $title . '^' . implode('#', $group); | |
$updateCondition[] = "UPDATE tvdata SET url='" . implode('#', $group) . "' WHERE title='{$title}';"; | |
} | |
usort($list, 'cmp2'); | |
$json = []; | |
foreach ($list as $item) { | |
// echo $item.PHP_EOL; | |
[$title, $urls] = explode('^', $item); | |
if (checkHide($exclude, $title)) { | |
continue; | |
} | |
$json['全国'][] = [ | |
"epg" => $title, | |
"title" => $title, | |
"url" => $urls, | |
]; | |
} | |
echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment