Changeset 837

Show
Ignore:
Timestamp:
07/05/07 23:49:50 (6 years ago)
Author:
max
Message:

imdb_augment_data: added a simple_search fallback for when the power search can't find anything useful.

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • postprocessors/imdb_augment_data

    r789 r837  
    2727 
    2828my $progname = "imdb_augment_data"; 
    29 my $version = "0.21"; 
     29my $version = "0.22"; 
    3030 
    3131use XMLTV; 
     
    3535use Data::Dumper; 
    3636use Storable; 
     37use HTML::TreeBuilder; 
    3738use Shepherd::Common; 
    3839 
     
    8788        'help'                  => \$opt->{help}, 
    8889        'test=s'                => \$opt->{test}, 
     90        'simpletest=s'          => \$opt->{simpletest}, 
    8991        'set=s'                 => \$opt->{set}, 
    9092        'verbose'               => \$opt->{help}, 
     
    142144 
    143145&run_test if (defined $opt->{test}); 
     146&simple_search_imdb(split(/,/, $opt->{simpletest})) if ($opt->{simpletest}); 
    144147&set_settings if (defined $opt->{set}); 
    145148 
    146149# set defaults 
    147150Shepherd::Common::set_default("debug", ((defined $opt->{debug} && $opt->{debug} > 0) ? 2 : 0)); 
     151Shepherd::Common::set_default("stats", \%stats); 
    148152Shepherd::Common::set_default("retry_delay", 10); 
    149153Shepherd::Common::set_default("delay", int(rand(4) + 3)) unless (defined $opt->{fast}); 
     
    280284        Shepherd::Common::log("  online IMDb search for '$title' with $post_fields"); 
    281285        my $html_data = Shepherd::Common::get_url(url => "http://www.imdb.com/List", 
    282                         method => "POST", postvars => $post_fields, stats => \%stats); 
     286                        method => "POST", postvars => $post_fields); 
    283287 
    284288        if (!$html_data) { 
     
    321325 
    322326############################################################################## 
     327# simple search 
     328#  
     329# The 'power search' seems to have a couple of annoying flaws, like 
     330# an inability to find AKA titles. So if our power search fails, we 
     331# fall back to the simple search. This seems to work very well, finding 
     332# many shows that the power search misses. 
     333 
     334sub simple_search_imdb 
     335{ 
     336    my ($title, $year) = @_; 
     337 
     338    &Shepherd::Common::log("  trying simple search for '$title' ($year)"); 
     339    my $url = sprintf "http://us.imdb.com/find?q=%s;s=tt", &Shepherd::Common::urlify($title); 
     340    my $html = &Shepherd::Common::get_url($url); 
     341 
     342    unless ($html)  
     343    { 
     344        $stats{failed_online_imdb_lookup}++; 
     345        &Shepherd::Common::log("simple search failed"); 
     346        return 0; 
     347    } 
     348 
     349    my $link; 
     350    my $tree = HTML::TreeBuilder->new_from_content($html); 
     351    HTMLPARSE: foreach my $block ($tree->look_down('_tag' => 'p')) 
     352    { 
     353        my $tag = $block->look_down('_tag' => 'b'); 
     354        next unless ($tag); 
     355        if ($tag->as_text eq 'Titles (Exact Matches)' or $tag->as_text eq 'Popular Titles') 
     356        { 
     357            foreach my $line (split(/<br>/, $block->as_HTML)) 
     358            { 
     359                if ($line =~ /^<a href="(\/title\/tt[0-9]+\/?)">(.*?)<\/a> \((\d{4})\)(.*)/ 
     360                        and 
     361                    (!$year or $year == $3) 
     362                        and 
     363                    (!$4 or $4 !~ /VG/)) 
     364                { 
     365                    if ($link) 
     366                    { 
     367                        # found multiple hits. That's bad. 
     368                        $link = undef; 
     369                        &Shepherd::Common::log("    found multiple hits, wanted 0.") if ($opt->{debug}); 
     370                        last HTMLPARSE; 
     371                    } 
     372                    # Bingo! Found a link 
     373                    &Shepherd::Common::log("    found link: $1") if ($opt->{debug}); 
     374                    $link = "http://www.imdb.com" . $1; 
     375                } 
     376            } 
     377        } 
     378    } 
     379    my $cache_name = 'simple-'.&Shepherd::Common::urlify($title).'-'.$year; 
     380 
     381    $data_cache->{movie_id_lookup}->{$cache_name}->{last_fetched} = time; 
     382 
     383    unless ($link) 
     384    { 
     385        &Shepherd::Common::log("    simple search failed."); 
     386        # negatively cache our failed lookup 
     387        $data_cache->{movie_id_lookup}->{$cache_name}->{url} = "-"; 
     388        return 0; 
     389    } 
     390    $data_cache->{movie_id_lookup}->{$cache_name}->{url} = $link; 
     391    $stats{imdb_lookup_added_positive_cache_entry}++; 
     392    return 1; 
     393} 
     394 
     395############################################################################## 
    323396# simple parser for imdb returned data: covers most data 
    324397 
     
    398471 
    399472        Shepherd::Common::log("  downloading online IMDb movie data for '$movie_title'"); 
    400         my $html_data = Shepherd::Common::get_url(url => $movie_url, stats => \%stats); 
     473        my $html_data = Shepherd::Common::get_url($movie_url); 
    401474 
    402475        if (!$html_data) { 
     
    712785                                } else { 
    713786                                        # search wasn't specific enough.  (more than 1 hit) 
    714                                         # just skip it 
    715                                         goto END; 
     787                                        # skip ahead to simple search 
     788                                        last; 
    716789                                } 
    717790                        } elsif ($data_cache->{movie_id_lookup}->{$post_fields}->{url} ne "-") { 
     
    722795                        } 
    723796                } 
     797        } 
     798 
     799        # Try the simple search if we have date info 
     800        if (!$found and $prog->{date}) { 
     801            # cached? 
     802            $post_fields = 'simple-'. &Shepherd::Common::urlify($movie_title) . '-'.$prog->{date}; 
     803            my $simple_search = $data_cache->{movie_id_lookup}->{$post_fields}; 
     804            if ($simple_search and $simple_search->{url}) { 
     805                if ($simple_search->{url} eq '-') { 
     806                    # negatively cached 
     807                    $stats{imdb_lookup_used_negative_cache_entry}++; 
     808                } else { 
     809                    # positively cached 
     810                    $stats{imdb_lookup_used_cache_entry}++; 
     811                    $found = 1; 
     812                } 
     813                &Shepherd::Common::log(sprintf "  used (%s cache) search: $post_fields", ($found ? 'positive' : 'negative')); 
     814            } else { 
     815                # not cached; look it up 
     816                $found = &simple_search_imdb($movie_title, $prog->{date}); 
     817            } 
     818        } else { 
     819            &Shepherd::Common::log("  no date info, not trying simple search") if ($opt->{debug}); 
    724820        } 
    725821 
  • status

    r836 r837  
    1515grabber         ten_website         1.01 
    1616reconciler      reconciler_mk2      0.30 
    17 postprocessor   imdb_augment_data   0.21 
     17postprocessor   imdb_augment_data   0.22 
    1818postprocessor   tvdb_augment_data   0.06 
    1919postprocessor   flag_aus_hdtv       0.18