Changeset 444

Show
Ignore:
Timestamp:
01/10/07 16:46:47 (6 years ago)
Author:
lincoln
Message:

commit channel-scanning debug output logic for mapping to new shepherd chan names

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • grabbers/yahoo7widget

    r442 r444  
    9797} 
    9898 
    99 die "no channel file specified, see --help for instructions\n", unless ((defined $opt->{channels_file}) || (defined $opt->{scanchan})); 
     99&scan_channels if (defined $opt->{scanchan}); 
     100die "no channel file specified, see --help for instructions\n", unless (defined $opt->{channels_file}); 
    100101 
    101102# 
     
    180181 
    181182###################################################################################################### 
     183# yahoo7widget times are always localtime on Eastern States .. (pure stupidity - they've missed the 
     184# whole point of what an epoch time is all about...). 
     185# we need to calculate the amount of time to offset the data to the user's local timezone. 
     186# 
     187# we do this by mapping differeht TZ settings and looking at the time. 
     188# make sure we set TZ back to something sane & relevant for this user 
    182189 
    183190sub setup_region_map 
    184191{ 
    185         # yahoo7widget times are always localtime on Eastern States .. map it to localtime in 
    186         # user's timezone 
    187192        my $local_time_offset = POSIX::strftime("%z",localtime(time)); 
    188193 
     
    197202        # set TZ variable back to an appropriate setting 
    198203        if ($opt->{region} =~ /^(74|108|81|107|82|83|85|86)$/) { 
    199                 $ENV{TZ} = "Australia/Adelaide";         # NT/SA 
     204                $ENV{TZ} = "Australia/Adelaide";        # NT/SA 
    200205        } elsif ($opt->{region} =~ /^(101|102)$/) { 
    201206                $ENV{TZ} = "Australia/Perth";           # WA 
     
    209214} 
    210215 
     216###################################################################################################### 
     217 
     218sub scan_channels 
     219{ 
     220        my %REGIONS = ( 
     221                126 => "ACT",               73 => "NSW: Sydney",            184 => "NSW: Newcastle", 
     222                66 => "NSW: Central Coast", 67 => "NSW: Griffith",          63 => "NSW: Broken Hill", 
     223                69 => "NSW: Northern NSW",  71 => "NSW: Southern NSW",      106 => "NSW: Remote and Central", 
     224                74 => "NT: Darwin",         108 => "NT: Remote & Central",  75 => "QLD: Brisbane", 
     225                78 => "QLD: Gold Coast",    79 => "QLD: Regional",          114 => "QLD: Remote & Central", 
     226                81 => "SA: Adelaide",       82 => "SA: Renmark",            83 => "SA: Riverland", 
     227                85 => "SA: South East SA",  86 => "SA: Spencer Gulf",       107 => "SA: Remote & Central", 
     228                88 => "Tasmania",           94 => "VIC: Melbourne",         93 => "VIC: Geelong", 
     229                90 => "VIC: Eastern Victoria", 95 => "VIC: Mildura/Sunraysia", 98 => "VIC: Western Victoria", 
     230                101 => "WA: Perth",         102 => "WA: Regional"); 
     231 
     232        my $now = time; 
     233 
     234        printf "\nScanning channels:\n\n"; 
     235 
     236        foreach my $r (sort { $a <=> $b } keys %REGIONS) { 
     237                sleep 5; 
     238 
     239                # 
     240                # get shepherd channels 
     241                # 
     242                my $ua2 = LWP::UserAgent->new(); 
     243                $ua2->agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322'); 
     244                $ua2->cookie_jar({}); 
     245                $ua2->get('http://www.yourtv.com.au'); 
     246                my $response = $ua2->get('http://www.yourtv.com.au/profile/ajax.cfm?action=channels&region_id='.$r); 
     247                my $page = $response->content; 
     248                if ($response->is_error()) { 
     249                        printf "Unable to download channel list for region $r from YourTV\n"; 
     250                        next; 
     251                } 
     252 
     253                # shepherd rules for station names 
     254                my (%shepherd_channels, $clist, $cn, $rq); 
     255                while ($page =~ /<label for="venue_id.*?>(.*?)<\/label>/sg) { 
     256                        my $channel = $1; 
     257                        $channel =~ s/\s{2,}//g; 
     258                        if ($channel =~ /(.*) (\(.*\))/) { 
     259                                ($cn, $rq) = ($1, $2); 
     260                        } else { 
     261                                ($cn, $rq) = ($channel, ""); 
     262                        } 
     263         
     264                        if ($clist->{$cn}) {    # Is there already a channel with this name? 
     265                                $clist->{$cn} = [ "(".$REGIONS{$r}.")" ] if (@{$clist->{$cn}} == 1 and $clist->{$cn}[0] eq ''); 
     266                                $rq = $REGIONS{$r} if ($rq eq ''); 
     267                                die "Bad channel list in region $r!" if (grep($rq eq $_, @{$clist->{$cn}})); 
     268                                push @{$clist->{$cn}}, $rq; 
     269                        } else { 
     270                                $clist->{$cn} = [ $rq ]; 
     271                        } 
     272                } 
     273         
     274                foreach $cn (keys %$clist) { 
     275                        if (@{$clist->{$cn}} == 1) { 
     276                                $shepherd_channels{$cn} = 1; 
     277                        } else { 
     278                                foreach $rq (@{$clist->{$cn}}) { 
     279                                        $shepherd_channels{"$cn $rq"} = 1; 
     280                                } 
     281                        } 
     282                } 
     283 
     284                # 
     285                # get widget channels 
     286                # 
     287                my $data = get_url("http://au.tv.yahoo.com/widget.html?rg=".$r."&st=".$now."&et=".($now+86400), "region ".$r." (".$REGIONS{$r}.")"); 
     288                my %seen_widget_chan; 
     289                my $parser = new XML::DOM::Parser; 
     290                my $tree = $parser->parse($data); 
     291                my $tree_channels = $tree->getElementsByTagName("venue"); 
     292                for (my $i = 0; $i < $tree_channels->getLength; $i++) { 
     293                        my $channel = $tree_channels->item($i)->getAttributeNode("co_short")->getValue; 
     294 
     295                        # for this channel get every programme ('event') 
     296                        my $events = $tree_channels->item($i)->getElementsByTagName("event"); 
     297 
     298                        if (!defined $shepherd_channels{$channel}) { 
     299                                $shepherd_channels{$channel} = 0;       # shepherd doesn't know about this channel, widget does 
     300                        } elsif ($shepherd_channels{$channel} == 1) { 
     301                                $shepherd_channels{$channel} = 2;       # both shepherd & widget know about channel 
     302                        } elsif ($shepherd_channels{$channel} == 2) { 
     303                                $shepherd_channels{$channel} = 3;       # shepherd/widget knew about channel but was duplicated! 
     304                        } elsif ($shepherd_channels{$channel} == 0) { 
     305                                ;                                       # aiee. a duplicate of a channel that shepherd doesn't know about! 
     306                        } else { 
     307                                die "unhandled shepherd_channels case for '$channel' value ".$shepherd_channels{$channel}; 
     308                        } 
     309 
     310                        printf "  %20s (%d programmes)%s%s%s\n", 
     311                                $channel, $events->getLength, 
     312                                (defined $seen_widget_chan{$channel} ? "\t[Duplicate in Widget]" : ""), 
     313                                ($shepherd_channels{$channel} == 0 ? "\t[Only known to Widget]" : ""), 
     314                                ($shepherd_channels{$channel} == 2 ? "\t[Known to both Widget/Shepherd (good!)]" : ""), 
     315                                ($shepherd_channels{$channel} == 3 ? "\t[Known to both Widget/Shepherd but duplicate in Widget]" : ""); 
     316 
     317                        $seen_widget_chan{$channel}++; 
     318                } 
     319 
     320                # any channels in Shepherd that Widget didn't return? 
     321                foreach my $ch (keys %shepherd_channels) { 
     322                        printf "  %20s [Only known to Shepherd]\n",$ch if ($shepherd_channels{$ch} == 1); 
     323                } 
     324 
     325                printf "\n"; 
     326        } 
     327 
     328        exit(0); 
     329} 
    211330 
    212331######################################################################################################