Project

General

Profile

Feature #1908 » xmltv_generator.php

Tvheadend XMLTV Generator v1.0 - Zdeněk Kopřivík, 2014-05-13 14:00

 
1
<?php
2
// channel_name, channel_id, src_id, src_channel_id
3
$channels = array(
4
    array("CT 1", "ct1.cz", 1, 7),
5
    array("CT 2", "ct2.cz", 1, 1),
6
    array("CT 4", "ct4.cz", 1, 5),
7
    array("CT 24", "ct24.cz", 1, 10),
8
    array("CT 1 HD", "cthd.cz", 0, 23),
9
    array("CT 4 HD", "ctsporthd.cz", 0, 24)
10
);
11

    
12
// url, username, password
13
$sources = array(
14
    array("http://address1:9981/epg", "user1", "password1"),
15
    array("http://address2:9981/epg", "user2", "password2")
16
);
17

    
18
$json = array();
19

    
20
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE tv SYSTEM "xmltv.dtd"><tv></tv>');
21
$xml->addAttribute('generator-info-name', 'Tvheadend XMLTV Generator v1.0');
22

    
23
function getContent($url, $username, $password, $limit)
24
{
25
    $process = curl_init($url . "?start=0&limit=".$limit);
26
    curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
27
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
28
    $data = curl_exec($process);
29
    curl_close($process);
30
    return $data;
31
}
32

    
33
function descMerge($description, $ext_text)
34
{
35
    if($description && $ext_text && substr($ext_text, 0, 1) != ' ')
36
        return $description . ' ' . $ext_text;
37
    else
38
        return $description . $ext_text;
39
}
40

    
41
foreach($channels as $channel)
42
{
43
    $child = $xml->addChild('channel');
44
    $child->addAttribute('id', $channel[1]);
45
    $child->addChild('display-name', $channel[0]);
46
}
47

    
48
foreach($sources as $source)
49
{
50
    $data = getContent($source[0], $source[1], $source[2], 0);
51
    $limit = json_decode($data, true);
52
    $data = getContent($source[0], $source[1], $source[2], $limit['totalCount']);
53
    $json[] = json_decode($data, true);
54
}
55

    
56
foreach($channels as $channel)
57
{
58
    foreach($json[$channel[2]]['entries'] as $entry)
59
    {
60
        if($entry['channelid'] == $channel[3])
61
        {
62
            $programme = $xml->addChild('programme');
63
            $programme->addAttribute('channel', $channel[1]);
64
            $programme->addAttribute('start', date("YmdHis O", $entry['start']));
65
            $programme->addAttribute('stop', date("YmdHis O", $entry['end']));
66
            $child = $programme->addChild('title');
67
            $programme->title = htmlspecialchars($entry['title']);
68
            $child->addAttribute('lang', 'cs');
69
            $child = $programme->addChild('desc');
70
            $programme->desc = descMerge(htmlspecialchars($entry['description']), htmlspecialchars($entry['ext_text']));
71
            $child->addAttribute('lang', 'cs');
72
        }
73
    }
74
}
75

    
76
$dom = new DOMDocument;
77
$dom->loadXML($xml->asXML());
78
$dom->formatOutput = true;
79
print($dom->saveXML());
80
?>
    (1-1/1)