#!/usr/bin/perl

# Given a list of FHG's hosted on http://nextdoornikki.com/, attempts to produce a list of the full IMG urls

    use strict;
    use warnings;

    use Data::Dumper;
    use LWP::Simple;

open FIN, "<fhg_list.txt" or die;

open FOUT, ">/home/interiot/public_html/apps/porn/url_lists/next_door_nikki.txt" or die;

while (<FIN>) {
    s/\s+$//s;
    my $url = $_;

    my ($fhg_name) = ($_ =~ m#nextdoornikki\.com/hosted/gal/([^/]+)/#si);

    print STDERR "\tfetching $url"; select STDERR; $|++;
    my $html = get($url);
    print STDERR "\n"; $|++;
    select STDOUT;

    #print $html; exit;

    #my @img_ids = ($html =~ m#/hosted/gal/[^/]*/thumbnails/tn_ndn_(\d+)#g);
    #my @img_ids = ($html =~ m#thumbnails/tn_(?:ndn_)?(\d+)#g);

    #my @thumbs = ($html =~ m#thumbnails/([^/'"<>0-9]*[0-9]+)#g);
    #my @thumbs = ($html =~ m#view\.php.*?(?:thumbnails|images)/([^/'"<>0-9]*[0-9]+)\.jpg#g);
    #my @thumbs = ($html =~ m#img src=["']?[^"]*/([^/'"<>0-9]*[0-9]+[^/'"<>0-9]*)\.jpg"[^<>]*class="hgpic"#g);
    my @thumbs = ($html =~ m#img src=["']?[^"]*/([^/'"<>0-9]*[0-9]+[^/'"<>0-9]*)\.jpg#g);

    if (!@thumbs) {
        print STDERR "No thumbnails found!\n";
        print "$html\n\n\n$url\n"; exit;
        next;
    }

    my %ignore = map {$_,1} qw[
        index_04
    ];

    #print Dumper \@thumbs; exit;
    #print Dumper \@img_ids; exit;
    #foreach my $id (@img_ids) {
        #print "${fhg_name}_$id.jpg http://nextdoornikki.com/hosted/gal/$fhg_name/images/ndn_$id.jpg\n";
    foreach my $thumb (@thumbs) {
        next if ($ignore{$thumb});

        my ($id) = ($thumb =~ /(\d+)/s);

        my $full;
        if ($thumb =~ /^tn_\d+/) {
            $full = "$id.jpg";
        } elsif ($thumb =~ /^tn_ndn_\d+/) {
            $full = "ndn_$id.jpg";
        } elsif ($thumb =~ /^\d+tn/) {
            $full = "$id.jpg";
        } elsif ($thumb =~ /^tn_(DSC_\d+)/) {
            $full = "$1.jpg";
        } else {
            print "Unable to parse thumb '$thumb' on\n$url\n";
            exit;
        }

        # extraneous prefix
        (my $disp_fhg_name = $fhg_name) =~ s/^ndn_//;

        my $out = "${disp_fhg_name}_$id.jpg http://nextdoornikki.com/hosted/gal/$fhg_name/images/$full\n";
        print FOUT $out;
        print STDOUT $out;

    }

}
