#!/usr/bin/perl -w ###################################################################### # Copyright (C) 2004 Martijn van Oosterhout # # # # This program is free software; you can redistribute it and/or # # modify it under the terms of the GNU General Public License # # as published by the Free Software Foundation; either version # # 2 of the License, or (at your option) any later version. # # # ###################################################################### use strict; use DBI; use LWP::UserAgent; use HTTP::Request::Common; my @fields = qw(Actor Advisory Bits Daynum Description Duration Episode Episodic Genres Time Title); my %ignore = ( 'WIN-HobD' => 1, 'SBS-MelD' => 1, 'SBS-HobD' => 1, 'HSV7D' => 1, 'GTV9D' => 1, 'ATV10D' => 1, 'ABV2D' => 1, 'ABT2D' => 1, ); my %map; my $ua = LWP::UserAgent->new(); my $db = DBI->connect( "dbi:Pg:dbname=kleptog", "kleptog", "", { AutoCommit => 1, RaiseError => 1 } ) or die $DBI::errstr; my $channels = $db->selectall_hashref( "select lower(code) as code, channel_id, serverid from channels", "code" ); foreach my $code (keys %$channels) { $map{$channels->{$code}{serverid}} = $channels->{$code}{code}; } opendir DH, "data" or die; my @files = readdir DH; closedir DH; foreach my $file (@files) { next if not -f "data/$file"; next if $file =~ /\+2_/; next if $file =~ /^index.html/; next if $file eq "robots.txt"; next if -e "processed/$file"; # next unless $file =~ /^TDT_/; my ($station,$day,$daynum,$month) = ($file =~ m,(?:^|/)([^/_]+)_(\w+)_(\d+)_(\w+)$,); if( not defined $month ) { warn "Enable to extract info from filename [$file]\n"; next; } next if $ignore{$station}; print "Processing $file...\n"; my $channel_id = $channels->{lc $station}{channel_id}; if( not defined $channel_id ) { warn "Station $station not found\n"; my $res = $db->selectall_arrayref( "select serverid from oldchannels where code = ?", undef, $station ); if( scalar( @$res ) == 1 ) { my $server_id = $res->[0][0]; if( defined $map{$server_id} ) { print "But maybe it maps to $map{$server_id}\n"; $channel_id = $channels->{$map{$server_id}}{channel_id}; } else { print "And server_id $server_id not in new system\n"; next; } } else { next; } } my $request = POST "http://kleptog.org/cgi-bin/oztivo.pl", Content_Type => 'form-data', Content => [ page => 'channel', type => 'view', data => $channel_id, datafile => [ "data/$file" ], submit => 'Upload', ]; my $response = $ua->request($request); open FH, ">processed/$file.out" or die; print FH $response->as_string; close FH; rename "data/$file", "processed/$file" or die; next; ########## This code no longer used ############ open( FH, " ) { # print STDERR "."; chomp; s/[\r\n]//g; if( /^$/ ) { $hash{"channel_id"} = $channel_id; StoreHash( \%hash ); %hash = (); next; } if( /^(\w+): (.*)$/ ) { $hash{ lc $1 } = $2; } else { die "Unrecognised input: [$_]\n"; } } StoreHash( \%hash ) if scalar(keys %hash); close FH; } sub StoreHash { my $hash = shift; my $count = scalar(@fields); # print "$hash->{Time}\n"; my @data = map { $hash->{lc $_} } @fields; # print "[", join(",",@data),"]\n"; $db->do( "insert into guide_data (channel_id, ".join(", ", @fields ).") values (?".(",?" x $count).")", undef, $hash->{channel_id}, @data ); }