#!/usr/bin/perl

# Given a search term on the command line, uses Yahoo to do an image search, and prints the 30
# largest images.

    use strict;
    use warnings;

    use Data::Dumper;

    use lib '/home/interiot/cpan/lib/';
    use Yahoo::Search;


#@ARGV = ("funberry.com", "inurl:gallery.php4");
@ARGV = ("sexandsubmission.com");

my @results = lots_of_results( {
                                      Doc => "site:" . join(" ", @ARGV),
                                      AppId => "MTPyI6rIkY2GolYjRfTohdNsOC8GS5vv",
                                      # The following args are optional.
                                      # (Values shown are package defaults).
                                      Mode         => 'all', # all words
                                      Start        => 0,
                                      Count        => 50,
                                      Type         => 'any', # all types
                                      AllowAdult   => 1, # yes porn, please
                                      AllowSimilar => 1, # yes dups, please
                                      Language     => undef,
                                     },
        50,
        1000);

#print Dumper \@results; exit;

my @urls = sort map {$_->{Url}} @results;
print join("\n", @urls), "\n"; exit;

#my %seen;
#@urls = grep {!m#gallery=(\d+)/# || !$seen{$1}++} @urls;

my %galleries = map {m#/g/(\d+/\d+)/i/#; $1=>1} @urls;
print join(" ", sort keys %galleries), "\n"; exit;

#print join("\n", @urls), "\n";

#print join(" ", keys %seen), "\n";




sub lots_of_results {
    my $passthrough_options_hashref = shift;
    my $max_count_per_query = shift || 50;
    my $total_count = shift || 100;

    my @all_results;

    my @first_passthrough;
    if ($passthrough_options_hashref->{Image}) {
        push(@first_passthrough, 'Image', delete $passthrough_options_hashref->{Image});
    } elsif ($passthrough_options_hashref->{Doc}) {
        push(@first_passthrough, 'Doc', delete $passthrough_options_hashref->{Doc});
    }

    for (my $start=0; $start<$total_count; $start+=$max_count_per_query) {
        $passthrough_options_hashref->{Count} = $max_count_per_query;
        $passthrough_options_hashref->{Start} = $start;
        my @results = Yahoo::Search->Results(@first_passthrough, %$passthrough_options_hashref);
        die $@ if $@; # report any errors
        foreach my $result (@results) {
            push(@all_results, {$result->Data});
        }
    }

    return @all_results;
}
