#/usr/bin/perl

   $time1=time;
   $oqsl=$oqs{'Amazon'};
# Whole string
   $ttok1="<b><a href=/exec/obidos/ASIN/";
   $ttok2="<br><br>";
   $ttokp1="<b><a href=/exec/obidos/ASIN/";
# Item Name
   $ttok3=">";
   $ttok4="</a></b>";
# Item Author
   $ttok5="helvetica size=-1>by ";
   $ttok6="</font>";
# Item Price
   $ttok7="class=price>\$";
   $ttok8="</b><br><b>";
# Item Reference 
   $ttok9="asin.";
   $ttok10="\" value=\"1\"";
# Item delivery conditions

# Token to skip record
   $ttok13="Out of Print--Limited Availability";
# Item picture conditions
   $ttok15="<a href=/exec/obidos/ASIN/";
   $ttok16="/qid=";

#   $itempic="images/blank_cover.gif";

   $pushline=0;
   $lpath=$oq{'Amazon'};
   $shop="Amazon";
   undef @output;
#   &IOsocket;
   $resultio=IOsocket();

   $inside=0;
   $readytoprocess=0;
   $pstring="";
   $olen=$#output +1;
#   plog("Amazoni answer has -  $olen lines");

if($olen) {

   foreach $outline (@output) {
     if ($outline =~ /$ttok1/) { 
        $inside=1;
#        plog("Start token found");
     };
     if ($inside == 1) { 
        $pstring.=$outline;
        $pstring=~ s/\n//g;
        $pstring=~ s/\r//g;
#       print ("<!--string $pstring -->\n");
     };
#	 
     if (($inside == 1) && ($outline =~ /$ttok2/)) { 
              $inside=0;
              $readytoprocess=1;
     $pstring=~ s/$ttokp1/ /;
       
#print "\n<!-- Ready to process set -->\n<!-- $pstring -->\n";
     };
	 
	 
     if ($readytoprocess == 1) {
#           $pstring.="\n";
#           $pstring=~ s/<I> <B>NEW!<\/B><\/I>//g;
#           $pstring=~ s/<B>//;
#           $pstring=~ s/<\/B>//;
#########################  Тягаем имя
           $ltk=$ttok3;
           $rtk=$ttok4;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemname=substr($pstring,$p1,$p2);
           $itnc=index($itemname,$ttok14);
#plog("Item Name=$itemname");
#########################  Тягаем автора
           $ltk=$ttok5;
           $rtk=$ttok6;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemauthor=substr $pstring,$p1,$p2;
           $itemauthor=~ s/<BR>/ /;
#plog("Item Author: \n\n\n\n\n\n$itemauthor\n\n\n\n\n\n\n");
#########################  Тягаем прайс
           $ltk=$ttok7;
           $rtk=$ttok8;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemprice=substr $pstring,$p1,$p2;
           $itemprice=~ s/,/./;
		   $itemprice=$itemprice*31;
#plog("$p1\n$p2\nItem Price=$itemprice\n$pstring");
#########################  Тягаем реферер
           $ltk=$ttok9;
           $rtk=$ttok10;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemref=substr $pstring,$p1,$p2;
		   
#plog("Item reference=$itemref");
#           $ltk=$ttok11;
#           $rtk=$ttok12;
#           $p1=index($pstring,$ltk)+length($ltk);
#           $p2=index($pstring,$rtk)-$p1;
#           $itemdelivery=substr $pstring,$p1,$p2;
            $itemdelivery="В результатах поиска не указана. Смотри описание товара.";
print "<!-- Item Delivery=$itemdelivery -->\n";

           $ltk=$ttok15;
           $rtk=$ttok16;

		   $itempic="http://images.amazon.com/images/P/".$itemref.".01.MZZZZZZZ.jpg";
           $badtok1=">";
           $badtok2="<";
           if(($itemname =~ /$badtok1/) || ($itemname =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
#              plog("Error processing $shop name - $itemname");
              last;
           };
           if(($itemauthor =~ /$badtok1/) || ($itemauthor =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
#              plog("Error processing $shop author $itemauthor");
			  $itemauthor = "";
#              last;
           };
           if(($itemprice =~ /$badtok1/) || ($itemprice =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
#              plog("Error processing $shop price - $itemprice");
              last;
           };
           if(($itemref =~ /$badtok1/) || ($itemref =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
#              plog("Error processing $shop ref - $itemref");
              last;
           };
           if(($itemdelivery =~ /$badtok1/) || ($itemdelivery =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
#              plog("Error processing $shop delivery");
              last;
           };
           $lists++;
           if ($itemname ne "") {
            if (index($itemprice,".") <  0) { $itemprice=$itemprice.".";};
            if (index($itemprice,".") == 1) { $itemprice="      ".$itemprice;};
            if (index($itemprice,".") == 2) { $itemprice="     ".$itemprice;};
            if (index($itemprice,".") == 3) { $itemprice="    ".$itemprice;};
            if (index($itemprice,".") == 4) { $itemprice="   ".$itemprice;};
            if (index($itemprice,".") == 5) { $itemprice="  ".$itemprice;};
            if (index($itemprice,".") == 6) { $itemprice=" ".$itemprice;};

                  push(@showline2,"$itemprice       ||$itemname||$itemauthor||http://www.amazon.com/exec/obidos/ASIN/$itemref||$itemdelivery||$shop||$oqsl||$itempic\n");
           };
           $pstring="";
           $readytoprocess=0;
     };
   };
};
print "<!-- end Amazon process -->";
$time2=time;

# return 0;

1;


