summaryrefslogtreecommitdiff
path: root/Tools/epg2timers/loadvdr.pl
blob: 485b0a4a5b91fb74cf00bfcd1a361d659045159d (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
#!/usr/bin/perl
#
# 0.01 loadvdr (peter)
# 0.02 delete old entries before updating (peter) 
# 0.03 dumped Net::Telnet because of lost connections
#
# please submit diffs to petera@gmx.net
#
# ./epg2timers < merkliste.html | perl -w loadvdr.pl
#
#

use Socket;
use Getopt::Std;

@resp = ();

$Dest = "localhost";
$Port = 2001;

$Timeout = 10; # max. seconds to wait for response

$SIG{ALRM} = sub { Error("timeout"); };
alarm($Timeout);

$iaddr = inet_aton($Dest)                   || Error("no host: $Dest");
$paddr = sockaddr_in($Port, $iaddr);

$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || Error("socket: $!");
connect(SOCK, $paddr)                       || Error("connect: $!");
select(SOCK); $| = 1;
Receive_void();

Send("lstt");

foreach $item (reverse @resp){
  if ($item =~ /^250.(\d{1,2}).*\(epg2timers\)/) {
    Send_void("DELT $1");
  }
}

while (defined ($line = <STDIN>)) {
  chomp $line;
  Send_void("UPDT $line");
}

Send("quit");
close(SOCK)                                 || Error("close: $!");



sub Send
{
  my $cmd = shift || Error("no command to send");
  print SOCK "$cmd\r\n";
  Receive();
}

sub Send_void
{
  my $cmd = $_[0];
  print SOCK "$cmd\r\n";
  Receive_void();
}

sub Receive
{
  while (<SOCK>) {
        chomp;
        push @resp,$_;
        last if substr($_, 3, 1) ne "-";
        }
}

sub Receive_void
{
  while (<SOCK>) {
        last if substr($_, 3, 1) ne "-";
        }
}

sub Error
{
  print STDERR "@_\n";
  close(SOCK);
  exit 0;
}