Perl + DOM

  Amazon ISBN



  
#!/usr/local/bin/perl

$SubscriptionId = '登録ID';

use HTML::Entities;
use XML::DOM;
use Encode;

require HTTP::Request;
require LWP::UserAgent;

$METHOD = $ENV{'REQUEST_METHOD'};
$METHOD =~ tr/a-z/A-Z/;
$QUERY_STRING = $ENV{'QUERY_STRING'};

if ( $METHOD eq "POST" ) {
	read(STDIN, $Form, $ENV{'CONTENT_LENGTH'});
	@Fields_Data = split(/&/, $Form);
}

@Fields_Data2 = split(/&/, $QUERY_STRING);

foreach $Field_Data ( @Fields_Data ) {
	($Name, $Value) = split(/=/, $Field_Data);
	$Value =~ tr/+/ /;
	$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$_POST{$Name} = $Value;
}
foreach $Field_Data2 ( @Fields_Data2 ) {
	($Name, $Value) = split(/=/, $Field_Data2);
	$Value =~ tr/+/ /;
	$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$_POST{$Name} = $Value;
	$_GET{$Name} = $Value;
}

if ( $_POST{'send'} eq 'Direct XML' ) {
	DirectXML();
	exit();
}
if ( $_POST{'send'} eq 'テキスト表示' ) {
	TextXML();
	exit();
}
if ( $_POST{'send'} eq 'タグ名指定' ) {

	print "Content-Type: text/html; Charset=utf-8\n";
	print "\n";

	$req =
		"http://webservices.amazon.co.jp/onca/xml?" .
		"Service=AWSECommerceService&" .
		"SubscriptionId=$SubscriptionId&" .
		"Operation=ItemLookup&" .
		"ResponseGroup=Large&" .
		"ItemId=" . $_POST{'ISBN'};

	$request = HTTP::Request->new("GET", $req );
	$ua = LWP::UserAgent->new;
	$response = $ua->request($request);

	$parser = new XML::DOM::Parser;
	$doc = $parser->parse($response->content);

	$nodes = $doc->getElementsByTagName ($_POST{'TAGNAME'});
	if ( $nodes->getLength() == 0 ) {
		$message = "入力されたタグは存在しません";
		Encode::from_to($message, "shift_jis", "utf8");
		print $message;
		exit();
	}

	binmode(STDOUT, ":utf8");

	$ret = "";
	PrintNode( $nodes );
	print "<pre>";
	print $ret;
	print "</pre>";

	exit();
}

print "Content-Type: text/html; Charset=shift_jis\n";
print "\n";
print "対象外";

sub DirectXML {

	print "Content-Type: text/xml; Charset=utf-8\n";
	print "\n";

	$req =
			"http://webservices.amazon.co.jp/onca/xml?" .
			"Service=AWSECommerceService&" .
			"SubscriptionId=$SubscriptionId&" .
			"Operation=ItemLookup&" .
			"ResponseGroup=Large&" .
			"ItemId=" . $_POST{'ISBN'};

	$request = HTTP::Request->new("GET", $req );
	$ua = LWP::UserAgent->new;
	$response = $ua->request($request);

	local($page) = $response->content;

	$page =~ s/$SubscriptionId/MySubscriptionId/g;

	print $page;

}

sub TextXML {

	print "Content-Type: text/html; Charset=utf-8\n";
	print "\n";

	$req =
			"http://webservices.amazon.co.jp/onca/xml?" .
			"Service=AWSECommerceService&" .
			"SubscriptionId=$SubscriptionId&" .
			"Operation=ItemLookup&" .
			"ResponseGroup=Large&" .
			"ItemId=" . $_POST{'ISBN'};

	$request = HTTP::Request->new("GET", $req );
	$ua = LWP::UserAgent->new;
	$response = $ua->request($request);

	local($page) = $response->content;

	$page =~ s/$SubscriptionId/MySubscriptionId/g;
	$page =~ s/>/>\n/g;
	print "<pre>";
	print HTML::Entities::encode($page,"<>");
	print "</pre>";

}

sub PrintNode {

	local($nodes) = $_[0];
	local($nCount) = $nodes->getLength();

	local($i,$j);
	local($node);
	local($nodeList);
	local($attList,$attCount);
	local($utf8);

	for ( $i = 0; $i < $nCount; $i++ ) {
		$node = $nodes->item($i);
		if ( $node->hasChildNodes ) {

			$nodeList = $node->getChildNodes();
			PrintNode( $nodeList );
		}
		else {
			$ret .= $node->getParentNode->getNodeName . "|";
			$ret .= $node->getParentNode->getNodeType . "|";
			$ret .= $node->getNodeName . "|";
			$ret .= $node->getNodeType . "|";

			$attList = $node->getParentNode->getAttributes;
			$attCount = $attList->getLength;
			if ( $attCount != 0 ) {
				@list = $attList->getValues;
				$ret .=  "(";
				for( $j = 0; $j < $attCount; $j++ ) {
					if ( $j != 0 ) {
						$ret .= "|";
					}
					$ret .= $attList->item($j)->getNodeName;
					$ret .= "|";
					$utf8 = $attList->item($j)->getNodeValue;
					utf8::decode($utf8);
					$ret .= $utf8;

				}
				$ret .= ")";
			}

			$utf8 = $node->getNodeValue;
			utf8::decode($utf8);
			$ret .= $utf8 . "\n";
		}
	}

}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ