找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 774|回复: 6

[iptv信源 资源分享或寻求] 求大佬修改一下这个代码以生成标准的xmltv

[复制链接]
发表于 2023-3-31 16:16 | 显示全部楼层 |阅读模式
悬赏20恩山币未解决
本帖最后由 coolguy007 于 2023-3-31 16:51 编辑

这是获取BBC各个电台节目单并生成epg.xml的php代码。但是里面缺少了一些元素,比如channel id display name等信息
  1. <?php
  2. // 设置错误报告级别为0,不报告任何错误
  3. error_reporting(0);
  4. // 设置响应的内容类型为XML,并指定字符集为UTF-8
  5. header('Content-Type: text/xml;charset=UTF-8', true, 200);

  6. // 频道信息的数组,键为频道名称,值为频道ID
  7. $channels = array(
  8.     'BBC Radio 1' => 'p00fzl8v',
  9.     'BBC Radio 1Xtra' => 'p00fzl9b',
  10.     'BBC Radio 2' => 'p00fzl86',
  11.     'BBC Radio 3' => 'p00fzl8q',
  12.     'BBC Radio 4' => 'p00fzl8j',
  13.     'BBC Radio 4 Extra' => 'p00fzl9g',
  14.     'BBC Radio 5 Live' => 'p00fzl94',
  15.     'BBC Radio 5 Live Sports Extra' => 'p00fzl9z',
  16.     'BBC Radio 6 Music' => 'p00fzl89',
  17.     'BBC Asian Network' => 'p00fzl9m',
  18.     'BBC World Service' => 'p00fzl9p'
  19. );

  20. // XML文档的根元素名称,此处为“tv”
  21. $xml_root_element_name = 'tv';

  22. // 初始化XML文档对象
  23. $xml = new SimpleXMLElement('<'.$xml_root_element_name.'/>');

  24. // 遍历所有频道和日期,将电子节目指南信息添加到XML文档对象中
  25. foreach ($channels as $channel => $id) {
  26.     for ($i = 0; $i < 7; $i++) {
  27.         $date = date("Y/m/d", strtotime("+" . $i . " day"));
  28.         $result = getepg($id, $date);
  29.         $starttime = json_decode(explode("|", $result)[0]);
  30.         $endtime = json_decode(explode("|", $result)[1]);
  31.         $titles = json_decode(explode("|", $result)[2]);
  32.         $descs = json_decode(explode("|", $result)[3]);
  33.         for ($j = 0; $j < count($starttime); $j++) {
  34.             // 创建XML元素并设置属性和子元素
  35.             $programme = $xml->addChild('programme');
  36.             $programme->addAttribute('start', $starttime[$j].' +0800');
  37.             $programme->addAttribute('stop', $endtime[$j].' +0800');
  38.             $programme->addAttribute('channel', $channel);
  39.             $title = $programme->addChild('title', $titles[$j]);
  40.             $title->addAttribute('lang', 'en');
  41.             $desc = $programme->addChild('desc', $descs[$j]);
  42.             $desc->addAttribute('lang', 'en');
  43.         }
  44.     }
  45. }

  46. // 将XML文档对象输出为字符串
  47. $re = $xml->asXML();

  48. // 将XML字符串保存到文件中
  49. $xml_file = 'epg.xml';
  50. file_put_contents(__DIR__ . '/' . $xml_file, $re);

  51. // 输出XML字符串
  52. print_r($re);


  53. function getepg($id, $date)
  54. {
  55.     $url = "https://www.bbc.co.uk/schedules/" . $id . "/" . $date . "?utcoffset=%2B08%3A00";
  56.     $header = array(
  57.         'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
  58.         'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  59.         'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
  60.         'Accept-Encoding: gzip, deflate, br',
  61.         'Connection: keep-alive',
  62.         'Referer: https://www.bbc.co.uk/schedules/' . $id,
  63.     );
  64.     $ch = curl_init();
  65.     curl_setopt($ch, CURLOPT_URL, $url);
  66.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  67.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  68.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  69.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  70.     curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate, br');
  71.     $result = curl_exec($ch);
  72.     curl_close($ch);
  73.     $result = str_replace('@', '', '{"@context":"' . explode('</script>', explode('{"@context":"', $result)[1])[0]);
  74.     $result = json_decode($result)->graph;
  75.     for ($i = 0; $i < count($result); $i++) {
  76.         $starttime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->startDate)[0]) . ' +0800'));
  77.         $endtime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->endDate)[0]) . ' +0800'));
  78.         $titles[] = $result[$i]->partOfSeries->name;
  79.         $descs[] = $result[$i]->description;
  80.     }
  81.     return json_encode($starttime) . "|" . json_encode($endtime) . "|" . json_encode($titles) . "|" . json_encode($descs);
  82. }
复制代码


我的恩山、我的无线 The best wifi forum is right here.
发表于 2023-3-31 17:49 | 显示全部楼层
https://www.bbc.co.uk/schedules/
这个地址都访问不了,接口请求结果都木有。咋改?不如让chatgpt帮忙吧。

点评

放到我服务器,可以的  详情 回复 发表于 2023-3-31 21:43
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2023-3-31 18:31 来自手机 | 显示全部楼层
看不到,算了,别人写吧
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2023-3-31 20:39 | 显示全部楼层
getepg方法里的url访问不通,算了,别人写吧
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2023-3-31 21:42 | 显示全部楼层
  1. <?php
  2. // 设置错误报告级别为0,不报告任何错误
  3. error_reporting(0);
  4. // 设置响应的内容类型为XML,并指定字符集为UTF-8
  5. header('Content-Type: text/xml;charset=UTF-8', true, 200);

  6. // 频道信息的数组,键为频道名称,值为频道ID
  7. $channels = array(
  8.     'BBC Radio 1' => 'p00fzl8v',
  9.     'BBC Radio 1Xtra' => 'p00fzl9b',
  10.     'BBC Radio 2' => 'p00fzl86',
  11.     'BBC Radio 3' => 'p00fzl8q',
  12.     'BBC Radio 4' => 'p00fzl8j',
  13.     'BBC Radio 4 Extra' => 'p00fzl9g',
  14.     'BBC Radio 5 Live' => 'p00fzl94',
  15.     'BBC Radio 5 Live Sports Extra' => 'p00fzl9z',
  16.     'BBC Radio 6 Music' => 'p00fzl89',
  17.     'BBC Asian Network' => 'p00fzl9m',
  18.     'BBC World Service' => 'p00fzl9p'
  19. );

  20. // XML文档的根元素名称,此处为“tv”
  21. $xml_root_element_name = 'tv';

  22. // 初始化XML文档对象
  23. $xml = new SimpleXMLElement('<'.$xml_root_element_name.'/>');

  24. // 遍历所有频道和日期,将电子节目指南信息添加到XML文档对象中
  25. foreach ($channels as $channel => $id) {
  26.     for ($i = 0; $i < 7; $i++) {
  27.         $date = date("Y/m/d", strtotime("+" . $i . " day"));
  28.         $result = getepg($id, $date);
  29.         $starttime = json_decode(explode("|", $result)[0]);
  30.         $endtime = json_decode(explode("|", $result)[1]);
  31.         $titles = json_decode(explode("|", $result)[2]);
  32.         $descs = json_decode(explode("|", $result)[3]);
  33.         for ($j = 0; $j < count($starttime); $j++) {
  34.             // 创建XML元素并设置属性和子元素
  35.             $programme = $xml->addChild('programme');
  36.             $programme->addAttribute('start', $starttime[$j].' +0800');
  37.             $programme->addAttribute('stop', $endtime[$j].' +0800');
  38.             $programme->addAttribute('channel', $channel);
  39.             $title = $programme->addChild('title', $titles[$j]);
  40.             $title->addAttribute('lang', 'en');
  41.             $desc = $programme->addChild('desc', $descs[$j]);
  42.             $desc->addAttribute('lang', 'en');
  43.         }
  44.     }
  45. }

  46. // 将XML文档对象输出为字符串
  47. $re = $xml->asXML();

  48. // 将XML字符串保存到文件中
  49. $xml_file = 'epg.xml';
  50. file_put_contents(__DIR__ . '/' . $xml_file, $re);

  51. // 输出XML字符串
  52. print_r($re);


  53. function getepg($id, $date)
  54. {
  55.     $url = "https://www.bbc.co.uk/schedules/" . $id . "/" . $date . "?utcoffset=%2B08%3A00";
  56.     $header = array(
  57.         'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
  58.         'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  59.         'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
  60.         'Accept-Encoding: gzip, deflate, br',
  61.         'Connection: keep-alive',
  62.         'Referer: https://www.bbc.co.uk/schedules/' . $id,
  63.     );
  64.     $ch = curl_init();
  65.     curl_setopt($ch, CURLOPT_URL, $url);
  66.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  67.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  68.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  69.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  70.     curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate, br');
  71.     $result = curl_exec($ch);
  72.     curl_close($ch);
  73.     $result = str_replace('@', '', '{"@context":"' . explode('</script>', explode('{"@context":"', $result)[1])[0]);
  74.     $result = json_decode($result)->graph;
  75.     for ($i = 0; $i < count($result); $i++) {
  76.         $starttime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->startDate)[0]) . ' +0800'));
  77.         $endtime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->endDate)[0]) . ' +0800'));
  78.         $titles[] = $result[$i]->partOfSeries->name;
  79.         $descs[] = $result[$i]->description;
  80.     }
  81.     return json_encode($starttime) . "|" . json_encode($endtime) . "|" . json_encode($titles) . "|" . json_encode($descs);
  82. }
复制代码
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2023-3-31 21:43 | 显示全部楼层
slipdal 发表于 2023-3-31 17:49
https://www.bbc.co.uk/schedules/
这个地址都访问不了,接口请求结果都木有。咋改?不如让chatgpt帮忙吧。 ...

放到我服务器,可以的
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2023-3-31 22:45 来自手机 | 显示全部楼层
爬虫就行了这个不难
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-3-30 10:15

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表