summaryrefslogtreecommitdiff
path: root/epg2html
blob: 5062e70fbeac75be7537d50fb1d296108c731360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/perl

# A simple EPG to HTML converter
#
# Converts the EPG data written by 'vdr' into the file /video/epg.data
# into a simple HTML programme listing, consisting of one file per channel
# plus an 'index.htm' file. All output files are written into the current
# directory.
#
# Usage: epg2html < /video/epg.data
#
# See the main source file 'vdr.c' for copyright information and
# how to reach the author.
#
# $Id: epg2html 2.2 2013/03/04 12:13:11 kls Exp $

@Index = ();
($Charset = $ENV{LANG}) =~ s/^[^.]*\.?(.*)/$1/;
$Charset = "utf-8" unless $Charset || $ENV{LANG} == 'C';

sub GetDay
{
  return substr(localtime(shift), 0, 10);
}

sub GetTime
{
  return substr(localtime(shift), 11, 5);
}

sub Tags
{
  my $s = shift;
  $s =~ s/\&/&amp;/g;
  $s =~ s/</&lt;/g;
  $s =~ s/>/&gt;/g;
  return $s;
}

while (<>) {
      chomp;
      if (/^C ([^ ]+) *(.*)/) {
         my $Channel = $2;
         (my $Page = $Channel) =~ y/\/ /-_/;
         $Page .= ".htm";
         $Channel = Tags($Channel);
         push(@Index, qq{<a href="$Page">$Channel</a><br>\n});
         my %Events = ();
         while (<>) {
               if (/^E (.*?) (.*?) ([^ ]*)/) {
                  (my $Time, $Duration) = ($2, $3);
                  my $Title = "", $Subtitle = "", $Description = "", $Vps = 0;
                  while (<>) {
                        if    (/^T (.*)/) { $Title       = Tags($1); }
                        elsif (/^S (.*)/) { $Subtitle    = Tags($1); }
                        elsif (/^D (.*)/) { $Description = Tags($1); $Description =~ s/\|/<br>/g; }
                        elsif (/^V (.*)/) { $Vps         = $1; }
                        elsif (/^e/) {
                           $Events{$Time} = [($Duration, $Title, $Subtitle, $Description, $Vps)];
                           last;
                           }
                        }
                  }
               elsif (/^c/) {
                  my @Schedule = ();
                  my $Day = "";
                  for $t (sort keys %Events) {
                      (my $Duration, $Title, $Subtitle, $Description, $Vps) = @{$Events{$t}};
                      my $d = GetDay($t);
                      if ($d ne $Day) {
                         push(@Schedule, "</table>\n") if ($Day && @Schedule);
                         push(@Schedule, "<h2>$d</h2>\n");
                         push(@Schedule, "<table cellspacing=2>\n");
                         $Day = $d;
                         }
                      my $Entry = $Title;
                      $Entry .= "<br><i>$Subtitle</i>" if $Subtitle;
                      $Entry .= "<br>$Description" if $Description;
                      $Entry .= "<br>(VPS = " . scalar localtime($Vps) . ")" if $Vps && $Vps != $t;
                      push(@Schedule, "<tr><td valign=top>" . GetTime($t) . "</td><td>$Entry</td></tr>\n");
                      }
                  push(@Schedule, "</table>\n") if (@Schedule);
                  open(PAGE, ">$Page") or die "$Page: $!\n";
                  print PAGE "<html>\n";
                  print PAGE "<head>\n";
                  print PAGE "<title>$Channel</title>\n";
                  print PAGE "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=$Charset\">\n";
                  print PAGE "</head>\n";
                  print PAGE "<body>\n";
                  print PAGE "<h1>$Channel</h1>\n";
                  print PAGE @Schedule;
                  print PAGE "</body>\n";
                  print PAGE "</html>\n";
                  close(PAGE);
                  last;
                  }
               }
         }
      }

open(INDEX, ">index.htm") or die "index.htm: $!\n";
print INDEX "<html>\n";
print INDEX "<head>\n";
print INDEX "<title>EPG Index</title>\n";
print INDEX "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=$Charset\">\n";
print INDEX "</head>\n";
print INDEX "<body>\n";
print INDEX sort { lc($a) cmp lc($b) } @Index;
print INDEX "</body>\n";
print INDEX "</html>\n";
close(INDEX);