找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 4775|回复: 14

[其它网络视频相关] 导出tvheadend的epg提供给diyp使用

[复制链接]
发表于 2022-8-30 22:31 | 显示全部楼层 |阅读模式
本帖最后由 wsgtrsys 于 2022-8-30 22:36 编辑

自己安装了tvheaded从有线的数据流中自动获取到epg数据。安装了diyp后,也从tvheadend串流观看电视。
但diyp需要的epg结构和tvheadend的不一样,需要进一步加工后使用。
程序用到了mysql、php、ngnix、memcache等软件。
先在mysql中建立数据库,并建立两张表。

  1. CREATE DATABASE `myepg` CHARACTER SET utf8 COLLATE utf8_general_ci;
  2. use myepg;
  3. CREATE TABLE IF NOT EXISTS `epg_channel` (
  4.   `name` varchar(16) NOT NULL,
  5.   `channel_id` varchar(40) NOT NULL,
  6. index(name)
  7. );

  8. CREATE TABLE IF NOT EXISTS `epg_programme` (
  9.   `title` varchar(100) NOT NULL,
  10. `sdate` varchar(16) NOT NULL,
  11.    `sstart` varchar(16) NOT NULL,
  12.   `sstop` varchar(16) NOT NULL,
  13.   `channel` varchar(40) NOT NULL,
  14. `sdesc` varchar(100) NOT NULL,
  15. INDEX(sdate),
  16. INDEX(channel)
  17. );
复制代码


然后在web目录中,建立两个php文件,一个负责下载tvheadend数据,并导入数据库。

  1. <?php

  2. $displayname = 'display-name';
  3. $send_mysql  = 1;

  4. function getContent($url, $username, $password)
  5. {
  6.     $process = curl_init($url);
  7.     // curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
  8.     curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
  9.     $data = curl_exec($process);
  10.     curl_close($process);
  11.     return $data;
  12. }


  13. $data = getContent("http://192.168.88.17:9981/xmltv/channels", "a", "a");
  14. $xml  = simplexml_load_string($data);
  15. $conn = mysqli_connect("localhost", "test", "test", "myepg");


  16. $result = mysqli_query($conn, 'TRUNCATE TABLE `epg_channel`;');
  17. $result = mysqli_query($conn, 'TRUNCATE TABLE `epg_programme`;');


  18. foreach ($xml->children() as $xmldata) {
  19.     if ($xmldata->getName() == "channel") {
  20.         if ($send_mysql == 1) {
  21.             $sql = "INSERT INTO epg_channel(name,channel_id) VALUES ('" . $xmldata->$displayname . "','" . $xmldata->attributes()->id . "')";
  22.             
  23.             $result = mysqli_query($conn, $sql);
  24.             
  25.             if (!empty($result)) {
  26.             } else {
  27.                 $error_message = mysqli_error($conn) . "\n";
  28.             }
  29.         }
  30.     }
  31.    
  32.     if ($xmldata->getName() == "programme") {
  33.                
  34.                 $start_time = substr($xmldata->attributes()->start, 8, 2) . ":" . substr($xmldata->attributes()->start, 10, 2);
  35.         $stop_time  = substr($xmldata->attributes()->stop, 8, 2) . ":" . substr($xmldata->attributes()->stop, 10, 2);
  36.         $jm_date = substr($xmldata->attributes()->stop, 0, 4) . "-" . substr($xmldata->attributes()->stop, 4, 2) . "-" . substr($xmldata->attributes()->stop, 6, 2);
  37.         

  38.                  if ($send_mysql == 1) {
  39.             $sql = "INSERT INTO epg_programme(channel,sdate,sstart,sstop,title,sdesc) VALUES ('" . $xmldata->attributes()->channel . "','" . $jm_date . "','". $start_time . "','". $stop_time ."','" . $xmldata->title . "','')";
  40.             
  41.             $result = mysqli_query($conn, $sql);
  42.             
  43.             if (!empty($result)) {
  44.             } else {
  45.                 $error_message = mysqli_error($conn) . "\n";
  46.             }
  47.         }
  48.     }
  49.    
  50. }
  51. ?>

复制代码



第二个文件负责处理diyp的数据请求

  1. <?php

  2. $displayname = 'display-name';
  3. $riqi =$_GET['date'];
  4. $ch=$_GET['ch'];
  5. $is_found   = 0;
  6. $send_mysql = 1;




  7. //http://epg.51zmt.top:8000/api/diyp/?ch=%E5%87%A4%E5%87%B0%E4%B8%AD%E6%96%87&date=2022-08-16
  8. function getContent($s_ch, $s_date)
  9. {
  10.     $process = curl_init("http://epg.51zmt.top:8000/api/diyp/"."?ch=".$s_ch ."&date=" . $s_date);
  11.     curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
  12.     $data = curl_exec($process);
  13.     curl_close($process);
  14.     return $data;
  15. }

  16. $memcache = new Memcache;
  17. $memcache->connect('localhost', 11211) or die ("Could not connect Memcache");


  18. $key = md5($ch.$riqi);
  19. $cache_result = array();
  20. $cache_result = $memcache->get($key);

  21. if($cache_result){
  22.         echo $cache_result;
  23. }else
  24. {

  25. $conn = mysqli_connect("localhost", "test", "test", "myepg");

  26. $sql = "SELECT channel_id  FROM epg_channel where name='" . $ch . "' limit 1";
  27. $retval = mysqli_query($conn, $sql);
  28. if (mysqli_num_rows($retval) <= 0) {
  29.         $data = getContent($ch, $riqi);
  30.         $memcache->set($key, $data, MEMCACHE_COMPRESSED, 1200);
  31.         echo $data;
  32.         return;               
  33. }


  34. $sql = "SELECT * FROM epg_programme WHERE channel = (SELECT channel_id  FROM epg_channel where name='" . $ch . "' limit 1) AND sdate = '" . $riqi . "'";
  35. // echo $sql. "\n";

  36. $retval = mysqli_query($conn, $sql);
  37. if (mysqli_num_rows($retval) > 0) {
  38.     while ($row = mysqli_fetch_assoc($retval)) {
  39.         
  40.         $epg_datas[] = array(
  41.             "start" => $row['sstart'],
  42.             "end" => $row['sstop'],
  43.             "title" => $row['title'],
  44.             "desc" => ""
  45.         );
  46.         $is_found    = 1;
  47.     }   
  48. }

  49. if ($is_found == 1) {
  50.     $age = array(
  51.         "channel_name" => "",
  52.         "date" => "$riqi",
  53.         "epg_data" => $epg_datas
  54.     );
  55.         $datas = json_encode($age, JSON_UNESCAPED_UNICODE);
  56.         $memcache->set($key, $datas, MEMCACHE_COMPRESSED, 1200);
  57.     echo ($datas);
  58.        
  59. } else {
  60.     $epg_datas[] = array(
  61.         "start" => "00:00",
  62.         "end" => "23:59",
  63.         "title" => "未知节目",
  64.         "desc" => ""
  65.     );
  66.     $age         = array(
  67.         "channel_name" => "",
  68.         "date" => "$riqi",
  69.         "epg_data" => $epg_datas
  70.     );
  71.     echo (json_encode($age, JSON_UNESCAPED_UNICODE));
  72. }
  73. }

  74. ?>
复制代码


把第一个php加入到crontab任务中,每天凌晨更新epg到mysql数据库。
第二php响应diyp的请求,首先在memcache中查询,如果没有再查mysql,如果再没有就转发51zmt的数据并缓存。。。。太污了。。
至于为什么要用mysql和memcache,因为如果直接查xml或者mysql实在太慢了,加了cache就是秒回了。

点评

测试速度没有?保存到文件和保存到数据库哪个快?读取文件和读取数据库哪个快?  发表于 2022-8-31 11:39
我的恩山、我的无线 The best wifi forum is right here.
发表于 2022-8-30 22:56 | 显示全部楼层
厉害这个必须赞
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 08:31 | 显示全部楼层
厉害了,有成品吗?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 09:02 | 显示全部楼层
跟看天书一样...

点评

一样看天书  详情 回复 发表于 2022-8-31 14:59
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 10:29 | 显示全部楼层
请假一下,哪个$send_mysql=1是起什么作用的?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 10:51 | 显示全部楼层
本帖最后由 1234sunzhe 于 2023-7-18 15:57 编辑

支持一下,可惜没有办有线,也没有小黑盒
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 12:04 | 显示全部楼层
小白一个,看得一知半解,但结果是还是不会,正宗小白,呵呵
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 14:59 | 显示全部楼层
sammar 发表于 2022-8-31 09:02
跟看天书一样...

一样看天书
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 15:29 | 显示全部楼层
感谢楼主分享,看看怎么样
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2022-8-31 16:51 | 显示全部楼层
直接读xml文件非常慢的,查询一次要2-3秒。mysql有索引,有缓存,快数百倍。

点评

是我没有搞来吗?能不能给个教程  详情 回复 发表于 2022-8-31 18:19
不是这个意思,是保存每个频道的数据到文件,不是保存xml,xml太大  发表于 2022-8-31 17:37
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-8-31 18:19 | 显示全部楼层
wsgtrsys 发表于 2022-8-31 16:51
直接读xml文件非常慢的,查询一次要2-3秒。mysql有索引,有缓存,快数百倍。

是我没有搞来吗?能不能给个教程
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2022-9-27 20:16 | 显示全部楼层
太麻烦了,直接写一个SH脚本wget TVH的EPG地址 ,后面指定保存成XML的文件   然后定时执行就行,哪有你这么麻烦。

如果还不明白,那你还是别用这个EPG,直接网上免费现成的用



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2023-11-28 23:52 | 显示全部楼层
太强大了,终于找到diyp的epg格式样例,也学到php如何处理xml,感谢
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2024-3-1 17:33 | 显示全部楼层
为何打开第二个PHP是302 Found
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 12:41

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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

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