#!/usr/bin/perl

@keyword=('白倉由美','里中満智子','庄司陽子','池田理代子','大谷博子','花郁悠紀子','柿崎普美','佐藤史生','鈴原研一郎','高橋亮子','竹本泉','中森清子','灘しげみ','西谷祥子','藤原栄子','ぶりっこ','水野英子');

&rakuten('http://www.rakuten.co.jp/auction/genre.cgi?tz=101299&f=0',@keyword);
&yahoo('http://auction.yahoo.co.jp/jp/21636-category-leaf.html?apg=1&s1=end&o1=d&u=&alocal=1jp&auccat=',@keyword);

exit 0;


sub rakuten {
	my($url,@keyword) = @_;
	my($item,$name);

	open(F,"lynx -source '$url' |/usr/bin/nkf -eZ|") || die "rakuten: can't open $url";
	while (<F>) {
		chop;
		if (m!^<td width="35%"><a href="([^"]+)"><font size=-1>([^<]+)!) {
			$item = $1; $name = $2;
#			$item = "http://www.rakuten.co.jp$item" unless ($item =~ /^http/);
			if (find($name,"http://www.rakuten.co.jp$item",@keyword)) {
				print "$name\t$item\n";
			}
		}elsif (m!<td width="43%"><a href="([^"]+)"><font size=-1>([^<]+)!) {
			$item = $1; $name = $2;
			$item = "http://www.rakuten.co.jp$item" unless ($item =~ /^http/);
			if (find($name,$item,@keyword)) {
				print "$name\t$item\n";
			}
		}
	}
	close F;
}

sub yahoo {
	my($url,@keyword) = @_;
	my($item,$name);
	my($i);
	my(@f);

	while ($url ne '') {
		open(F,"lynx -source '$url' |/usr/bin/nkf -eZ|") || die "rakuten: can't open $url";
		$url = '';
		while (<F>) {
			chop;
			if (m!^<tr><td align=center><IMG SRC=http://auctions.yahoo.co.jp/i/auctions/cam.gif WIDTH=15 HEIGHT=15 ALT="\*"></td><td>!) {
				@f = split(/<a href=/);
				foreach $i (@f) {
					if ($i =~ m!(http://[^>]+)>([^<]+)!) {
						$item = $1; $name = $2;
						if (find($name,$item,@keyword)) {
							print "$name\t$item\n";
						}
					}
				}
			}elsif (m!<a href="([^"]+)">次の50件</a>!) {
				$url = "http://auctions.yahoo.co.jp$1";
			}
		}
	}
	close F;
}

sub find {
	my($name,$url,@keyword) = @_;
	my($i);

	foreach $i (@keyword) {
		return 1 if ($name =~ /$i/i);
	}
	open(S,"lynx -source '$url' |/usr/bin/nkf -eZ|") || die "sub: can't open $url";
	while (<S>) {
		chop;
		foreach $i (@keyword) {
			return 1 if (/$i/i);
		}
	}
	close S;
	return 0;
}
