-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.pl
185 lines (142 loc) · 3.1 KB
/
daemon.pl
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl
use strict;
use Tie::File;
use TTConfig;
use Recording;
use CommandLineController;
use Data::Dumper;
$|++;
my $home = $ENV{'HOME'};
my $c = $TTConfig::config;
my $commands = CommandLineController->new();
my $vs = $c->vstream();
my $mode = $c->dlmode_auto(); # ty,tmf,mpg,vob,m2v,avi,mp4
our @toDownload = ();
our @favs = ();
our @newRecordings = ();
# Download listings from tivo
@newRecordings = get_new_recordings();
#print "Here are the new recs:\n".Dumper(@newRecordings);
# Parse new recs to see if there are favs in there
#chomp(@favs = `cat $home/Library/Preferences/tivotool.favs`);
tie @favs, 'Tie::File', "$home/Library/Preferences/tivotool.favs" or die("$! $?");
get_favorites_todownload();
#print "Favs:\n";
#print Dumper(@favs);
#print "Here is the toDownload queue:\n";
#print Dumper(@toDownload);
# Get all the download commands.
# run a code block againt each item in list, return to a new list
my @downCmds = map { build_command($_) } @toDownload;
#print Dumper(@downCmds);
foreach my $cmd (@downCmds)
{
`$cmd`;
}
untie @favs;
#print "Done.\n";
sub get_new_recordings
{
my $seen_file = "$home/Library/Preferences/tivotool.seen";
my $listings_cmd = "$vs tivo:https://".$c->tivoip()."/llist";
my @old_recs = `cat $seen_file`;
chomp @old_recs;
my @recs = `$listings_cmd | sort | uniq`;
chomp @recs;
my @new = ();
open (SEEN, ">> $seen_file") or die($!);
foreach (@recs)
{
my $rec = $_;
/^(\d+)\|(\d+)\/(\d+)\/(\d+).*$/;
my $id = $1.$2.$3.$4; # fsid + date
print SEEN $id."\n";
my $found = 0;
foreach (@old_recs)
{
if ($id == $_)
{
$found++;
}
}
if ($found == 0)
{
#print "New: $id \n";
push(@new, $rec)
}
}
close SEEN or die($!);
`sort $seen_file -o $seen_file`;
`uniq $seen_file > $seen_file.saved`;
`mv $seen_file.saved $seen_file`;
return @new;
}
sub get_favorites_todownload
{
foreach (@newRecordings)
{
our $newRecording = $_;
chomp $newRecording;
foreach (@favs)
{
our $fav = $_;
my @fields = split(/\|/, $newRecording);
if ($fields[4] eq $fav)
{
# the new recording is a favorite
my $alreadyQueued = undef;
foreach (@toDownload)
{
$alreadyQueued = 1 if ($_->fsid() == $fields[0])
}
unless ($alreadyQueued)
{
$newRecording =~ m/^(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)/;
next unless ($1);
my $r = Recording->new();
$r->fsid($1);
$r->date($2);
$r->parts($3);
$r->station($4);
$r->show($5);
$r->episode($6);
push @toDownload, $r;
}
}
}
}
}
sub build_command
{
my $rec = shift;
my $cmd = "";
if ($mode==0)
{
$cmd = $commands->build_tycommand($rec);
}
elsif ($mode==1)
{
$cmd = $commands->build_tmfcommand($rec);
}
elsif ($mode==2)
{
$cmd = $commands->build_mpg2command_v($rec);
}
elsif ($mode==3)
{
$cmd = $commands->build_vobcommand_v($rec);
}
elsif ($mode==4)
{
$cmd = $commands->build_m2vcommand_h($rec);
}
elsif ($mode==5)
{
$cmd = $commands->build_avicommand($rec);
}
elsif ($mode==6)
{
$cmd = $commands->build_mp4command($rec);
}
return $cmd;
}