Ticket #11487: dbox2m3u.php

File dbox2m3u.php, 4.4 KB (added by torbjorn.jansson@…, 13 years ago)

the php helper script to generate m3u files from enigma2

Line 
1<?php
2class channel
3{
4 public $name='';
5 public $srvref='';
6 public $channum=-1;
7 public $tsserviceid;
8 public $xmltvid;
9}
10$chanlist=array();
11
12//try to find xmltvid for all the channels in $chanlist array
13//sourceid is optional, if it is not >0 all existing channels will be searched
14//usefull if the dreambox channels overlap with one or more other sources
15function getxmltvids($dbhost,$dbuser,$dbpassword,$sourceid)
16{
17 global $chanlist;
18 $mysqli = new mysqli($dbhost,$dbuser,$dbpassword,"mythconverg");
19 if ($mysqli->connect_errno)
20 {
21 exit("Failed to connect to MySQL: " . $mysqli->connect_error);
22 }
23
24 $query="SELECT DISTINCT name,xmltvid FROM channel WHERE length(xmltvid)>0";
25 if(is_numeric($sourceid) && $sourceid>0)
26 {
27 $tmp=$mysqli->real_escape_string($sourceid);
28 $query=$query." AND sourceid=$tmp";
29 }
30
31 $res = $mysqli->query($query);
32 while($row=$res->fetch_assoc())
33 {
34 foreach($chanlist as $channel)
35 {
36 if($channel->name == $row["name"])
37 {
38 $channel->xmltvid=$row["xmltvid"];
39 }
40 }
41 }
42}
43
44//defaults
45$host='';
46$webifport=80;
47$streamport=8001;
48$bouquet='Favourites (TV)';
49$sourceid=0;
50
51//database access
52//change as needed
53$mysqlserver='127.0.0.1';
54$mysqluser='mythtv';
55$mysqlpass='mythtv';
56
57
58if(empty($_REQUEST))
59{
60 ?>
61 <form method="get">
62 <table>
63 <tr><td>Dreambox hostname:</td><td><input type="text" name="host" value="<?php echo $host?>"></td><td>mandatory</td></tr>
64 <tr><td>Dreambox webif port:</td><td><input type="text" name="webifport" value="<?php echo $webifport?>"></td><td>optional</td></tr>
65 <tr><td>Bouquet:</td><td><input type="text" name="bouquet" value="<?php echo $bouquet?>"></td><td>mandatory</td></tr>
66 <tr><td>Stream port:</td><td><input type="text" name="streamport" value="<?php echo $streamport?>"></td><td>optional</td></tr>
67 <tr><td>sourceid:</td><td><input type="text" name="sourceid" value="<?php echo $sourceid?>"></td><td>optional, used for matching channel names with xmltvids</td></tr>
68 <tr><td colspan=2><input type="submit"></td></tr>
69 </table>
70 </form>
71 <?php
72 exit();
73}
74else
75{
76 if(empty($_REQUEST["host"]) || empty($_REQUEST["bouquet"]))
77 {
78 exit('both host and bouquet must be specified');
79 }
80 $host=$_REQUEST["host"];
81 $bouquet=$_REQUEST["bouquet"];
82
83 if(!empty($_REQUEST["webifport"]) && is_numeric($_REQUEST["webifport"]))
84 {
85 $webifport=$_REQUEST["webifport"];
86 }
87 if(!empty($_REQUEST["streamport"]) && is_numeric($_REQUEST["streamport"]))
88 {
89 $streamport=$_REQUEST["streamport"];
90 }
91 if(!empty($_REQUEST["sourceid"]) && is_numeric($_REQUEST["sourceid"]))
92 {
93 $sourceid=$_REQUEST["sourceid"];
94 }
95}
96
97$fp=fopen("http://$host:$webifport/web/getallservices",'rb')
98 or exit("unable to open service list from dreambox");
99$contents=stream_get_contents($fp);
100fclose($fp);
101
102$doc=new DOMDocument();
103$doc->loadXML($contents);
104
105$xpath=new DOMXPath($doc);
106//$elements=$xpath->query('/e2servicelistrecursive/e2bouquet/e2servicename');
107$services=$xpath->query("/e2servicelistrecursive/e2bouquet/e2servicelist/e2service[../../e2servicename='$bouquet']");
108if(!is_null($services))
109{
110 //e2service
111 $cnt=1;
112 foreach($services as $service)
113 {
114
115 $srvref="";
116 $srvname="";
117 foreach($service->childNodes as $node)
118 {
119 if($node->nodeName=='e2servicereference')
120 {
121 $srvref=$node->nodeValue;
122 }
123 else if($node->nodeName=='e2servicename')
124 {
125 $srvname=$node->nodeValue;
126 }
127 }
128 $channel=new channel();
129 $channel->name=$srvname;
130 $channel->srvref=$srvref;
131 $channel->channum=$cnt;
132
133 //Try to find the serviceid.
134 //Note that if you have grouped several channels on the dreambox this
135 //wont work because the service reference from the dreambox doesn't
136 //contain the serviceid.
137 $srvrefparts=explode(":",$srvref);
138 if(count($srvrefparts)>4)
139 {
140 $tmp=hexdec($srvrefparts[3]);
141 if($tmp>0)
142 {
143 $channel->tsserviceid=$tmp;
144 }
145 }
146 getxmltvids($mysqlserver,$mysqluser,$mysqlpass,$sourceid);
147
148 $chanlist[]=$channel;
149 $cnt++;
150 }
151
152 //create the m3u file
153 //header('Content-type: audio/x-mpegurl');
154 //header('Content-Disposition: attachment; filename="channels.m3u"');
155 echo "#EXTM3U\n";
156 foreach($chanlist as $channel)
157 {
158 echo "#EXTINF:0,$channel->channum - $channel->name\n";
159 if(is_string($channel->xmltvid) && strlen($channel->xmltvid)>0)
160 {
161 echo "#EXTMYTHTV:xmltvid=$channel->xmltvid\n";
162 }
163 if(is_int($channel->tsserviceid))
164 {
165 echo "#EXTMYTHTV2:serviceid=$channel->tsserviceid\n";
166 }
167 echo "http://$host:$streamport/$channel->srvref\n";
168 }
169}
170
171?>