~ubuntu-branches/ubuntu/trusty/freeguide/trusty

« back to all changes in this revision

Viewing changes to xmltv/share/perl/5.8.8/XMLTV/Supplement.pm

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Watkins
  • Date: 2008-09-07 15:49:32 UTC
  • mfrom: (1.2.6 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080907154932-2jvgv76btq068fe0
Tags: 0.10.9-1
* New upstream release. (Closes: #492789)
* Moved package from contrib to main. (Closes: #492544)
* Added lintian override for 'build-depends-without-arch-dep ant', as ant is
  used in the clean target.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
=cut
45
45
 
46
46
sub d {
47
 
#  print STDERR "$_[0]\n";
 
47
  print STDERR "XMLTV::Supplement: $_[0]\n" if $ENV{XMLTV_SUPPLEMENT_VERBOSE};
48
48
}
49
49
 
50
50
my $cachedir;
68
68
    $cachedir = File::Spec->catfile( $ENV{HOME}, ".xmltv", "supplement" );
69
69
  }
70
70
 
71
 
  d( "XMLTV::Supplement: Using cachedir '$cachedir'" );
 
71
  d( "Using cachedir '$cachedir'" );
72
72
  create_dir( $cachedir );
73
73
}
74
74
 
139
139
 
140
140
  my $result;
141
141
 
 
142
  d( "Reading $filename" );
142
143
  eval { $result = read_file( $filename ) };
143
144
 
144
145
  if( not defined( $result ) ) {
175
176
    $url = "$supplement_root/$name";
176
177
  }
177
178
 
 
179
  d( "Going to fetch $url" );
 
180
 
178
181
  my $meta = read_meta( $directory, $name );
179
182
  my $cached = read_cache( $directory, $name );
180
183
 
181
 
  if( defined( $cached ) and defined( $meta->{'LastUpdated'} ) and
182
 
      23*60*60 > (time - $meta->{'LastUpdated'} ) ) {
183
 
    d("LastUpdated ok. Using cache.");
184
 
    return $cached;
185
 
  }
186
 
 
187
184
  my %p;
188
 
  if( defined( $cached ) ) {
189
 
    $p{'If-Modified-Since'} = $meta->{LastModified} 
190
 
      if defined $meta->{LastModified};
191
 
    $p{'If-None-Match'} = $meta->{ETag}
 
185
 
 
186
  if( defined( $meta->{Url} ) and ($meta->{Url} eq $url ) ) {
 
187
    # The right url is stored in the cache.
 
188
 
 
189
    if( defined( $cached ) and defined( $meta->{'LastUpdated'} ) 
 
190
        and 1*60*60 > (time - $meta->{'LastUpdated'} ) ) {
 
191
      d("LastUpdated ok. Using cache.");
 
192
      return $cached;
 
193
    }
 
194
 
 
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};
 
200
    }
193
201
  }
194
202
 
195
203
  my $resp = $ua->get( $url, %p );
196
204
 
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.");
200
208
    return $cached;
201
209
  }
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;
207
215
  }
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";
211
219
    return $cached;
212
220
  }
213
221
  else {
214
 
    print STDERR "XMLTV::Supplement: Failed to fetch $url. ";
 
222
    print STDERR "XMLTV::Supplement: Failed to fetch $url: " . 
 
223
        $resp->status_line . ".\n";
215
224
    exit 1;
216
225
  }
217
226
}
218
227
 
219
228
sub write_meta {
220
 
  my( $directory, $file, $resp ) = @_;
 
229
  my( $directory, $url, $file, $resp, $meta ) = @_;
221
230
 
222
231
  my $metafile = cache_filename( $directory, "$file.meta" );
223
232
 
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' );
 
235
 
 
236
  print OUT "Url $url\n";
 
237
 
 
238
  if( defined $resp->header( 'Last-Modified' ) ) { 
 
239
    print OUT "Last-Modified " . $resp->header( 'Last-Modified' ) . "\n";
 
240
  }
 
241
  elsif( defined $meta->{'Last-Modified'} ) {
 
242
    print OUT "Last-Modified " . $meta->{ 'Last-Modified' } . "\n";
 
243
  }
 
244
 
228
245
  print OUT "ETag " . $resp->header( 'ETag' )  . "\n"
229
246
      if defined $resp->header( 'ETag' );
230
247
  close( OUT );
304
321
on disk in ~/.xmltv/supplement on Unix and in
305
322
CSIDL_LOCAL_APPDATA//xmltv/supplement on Windows.
306
323
 
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.
312
329
 
321
338
  XMLTV_SUPPLEMENT=/usr/share/xmltv
322
339
  XMLTV_SUPPLEMENT=http://supplementary.xmltv.se
323
340
 
 
341
The XMLTV_SUPPLEMENT_VERBOSE environment variable can be used to get
 
342
more debug output from XMLTV::Supplement.
 
343
 
 
344
  XMLTV_SUPPLEMENT_VERBOSE=1
 
345
 
324
346
=head1 COPYRIGHT
325
347
 
326
348
Copyright (C) 2007 Mattias Holmlund.