175
176
$url = "$supplement_root/$name";
179
d( "Going to fetch $url" );
178
181
my $meta = read_meta( $directory, $name );
179
182
my $cached = read_cache( $directory, $name );
181
if( defined( $cached ) and defined( $meta->{'LastUpdated'} ) and
182
23*60*60 > (time - $meta->{'LastUpdated'} ) ) {
183
d("LastUpdated ok. Using cache.");
188
if( defined( $cached ) ) {
189
$p{'If-Modified-Since'} = $meta->{LastModified}
190
if defined $meta->{LastModified};
191
$p{'If-None-Match'} = $meta->{ETag}
186
if( defined( $meta->{Url} ) and ($meta->{Url} eq $url ) ) {
187
# The right url is stored in the cache.
189
if( defined( $cached ) and defined( $meta->{'LastUpdated'} )
190
and 1*60*60 > (time - $meta->{'LastUpdated'} ) ) {
191
d("LastUpdated ok. Using cache.");
195
if( defined( $cached ) ) {
196
$p{'If-Modified-Since'} = $meta->{'Last-Modified'}
197
if defined $meta->{'Last-Modified'};
198
$p{'If-None-Match'} = $meta->{ETag}
192
199
if defined $meta->{ETag};
195
203
my $resp = $ua->get( $url, %p );
197
205
if( $resp->code == RC_NOT_MODIFIED ) {
198
write_meta( $directory, $name, $resp );
206
write_meta( $directory, $url, $name, $resp, $meta );
199
207
d("Not Modified. Using cache.");
202
210
elsif( $resp->is_success ) {
203
write_meta( $directory, $name, $resp );
211
write_meta( $directory, $url, $name, $resp, $meta );
204
212
write_cache( $directory, $name, $resp );
205
213
d("Cache miss.");
206
214
return $resp->content;
208
216
elsif( defined( $cached ) ) {
209
print STDERR "XMLTV::Supplement: Failed to fetch $url. " .
210
"Using cached info.\n";
217
print STDERR "XMLTV::Supplement: Failed to fetch $url: " .
218
$resp->status_line . ". Using cached info.\n";
214
print STDERR "XMLTV::Supplement: Failed to fetch $url. ";
222
print STDERR "XMLTV::Supplement: Failed to fetch $url: " .
223
$resp->status_line . ".\n";
220
my( $directory, $file, $resp ) = @_;
229
my( $directory, $url, $file, $resp, $meta ) = @_;
222
231
my $metafile = cache_filename( $directory, "$file.meta" );
224
233
open OUT, "> $metafile" or die "Failed to write to $metafile";
225
234
print OUT "LastUpdated " . time() . "\n";
226
print OUT "Last-Modified " . $resp->header( 'Last-Modified' ) . "\n"
227
if defined $resp->header( 'Last-Modified' );
236
print OUT "Url $url\n";
238
if( defined $resp->header( 'Last-Modified' ) ) {
239
print OUT "Last-Modified " . $resp->header( 'Last-Modified' ) . "\n";
241
elsif( defined $meta->{'Last-Modified'} ) {
242
print OUT "Last-Modified " . $meta->{ 'Last-Modified' } . "\n";
228
245
print OUT "ETag " . $resp->header( 'ETag' ) . "\n"
229
246
if defined $resp->header( 'ETag' );
304
321
on disk in ~/.xmltv/supplement on Unix and in
305
322
CSIDL_LOCAL_APPDATA//xmltv/supplement on Windows.
307
If a file has been downloaded less than 23 hours ago, the file from
324
If a file has been downloaded less than 1 hour ago, the file from
308
325
the cache is used without contacting the server. Otherwise, if the
309
file has been downloaded more than 23 hours ago, then the module
326
file has been downloaded more than 1 hour ago, then the module
310
327
checks with the server to see if an updated file is available and
311
328
downloads it if necessary.