#!/usr/bin/perl

   $time1=time;
   $oqsl=$oqs{'24x7'};
# Whole string
   $ttok1='<A HREF=\"/info.jsp\?pid=';
   $ttok2='<TR CLASS=\"cont\"><TD CLASS=\"bottrack\">';
# Item Name
   $ttok3='H1 CLASS=title>';
   $ttok4='</H1></A>';
# Item Author
   $ttok5="P CLASS=sela>";
   $ttok6="</P></TD>";
# Item Price
   $ttok7="LASS=red>";
   $ttok8="\&nbsp\;руб";
# Item Reference 
   $ttok9="HREF=\"";
   $ttok10="\"><IMG SRC";
#   $ttok14="\" TITLE";
# Item delivery conditions
   $ttok11="поставки: <FONT CLASS=cont>";
   $ttok12="</FONT><BR></TD";
# Item picture
   $ttok15="IMG SRC=\"/images/picture.jsp?fid=";
   $ttok16="\" ALT=\"Просмотреть";
   $ttok151="<IMG SRC=\"";
   $ttok161="\" WIDTH=70 HEIGHT=110 ALT=\"Просмотреть";
# Token to correct record
   $ttok13="</FONT";

   $pushline=0;
   $lpath=$oq{'24x7'};
   $shop="24x7";
   undef @output;
   plog("start query 24");
   $resultio=IOsocket();
   $inside=0;
   $readytoprocess=0;
   $pstring="";
#       print ("<!-- Start token is $ttok1 -->\n");
   $olen=$#output +1;
   plog("24x7 answer has -  $olen lines");

if($olen) {

   foreach $outline (@output) {
       $outline=~ s/\n//;
       $outline=~ s/\r//;
#       print ("<!-- +++ $outline -->\n");
     if ($outline =~ /$ttok1/) { 
         $inside=1;
#       print ("<!-- Start token found -->\n");
     };
     if ($inside == 1) { 
        $pstring.=$outline;
        $pstring=~ s/\n//g;
        $pstring=~ s/\r//g;
#       print ("<!-- $pstring -->\n");
     };
     if (($inside == 1) && ($outline =~ /$ttok2/)) { 
              $inside=0;
              $readytoprocess=1;
#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);
# print "<!-- Item Name=$itemname -->\n";
           $ltk=$ttok5;
           $rtk=$ttok6;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemauthor=substr $pstring,$p1,$p2;
           $itemauthor=~ s/<BR>/ /;
# print "<!-- Item Author=$itemauthor -->\n";
           $ltk=$ttok7;
           $rtk=$ttok8;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemprice=substr $pstring,$p1,$p2;
# print "<!-- Item Price=$itemprice -->\n";
           $ltk=$ttok9;
           $rtk=$ttok10;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemref=substr $pstring,$p1,$p2;
#           if($itemref=~ /TITLE/go) {
#              $rtk=$ttok14;
#              $p2=index($pstring,$rtk)-$p1;
#              $itemref=substr $pstring,$p1,$p2;
#           };
# print "<!-- Item reference=$itemref -->\n";
           $ltk=$ttok11;
           $rtk=$ttok12;
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           $itemdelivery=substr $pstring,$p1,$p2;
# print "<!-- Item Delivery=$itemdelivery -->\n";
           if ($itemdelivery =~ /$ttok13/) { 
              ($itemdelivery)=split (/$ttok13/, $itemdelivery);

           };
           $tkf=0;
           $ltk=$ttok15;
           $rtk=$ttok16;
           if((index($pstring,$ltk) < 0) || (index($pstring,$rtk)< 0)) {
             $ltk=$ttok151;
             $rtk=$ttok161;
             $tkf=1;
           };
           $p1=index($pstring,$ltk)+length($ltk);
           $p2=index($pstring,$rtk)-$p1;
           if($tkf) { 
                $itempic="http://www.24x7.ru".substr $pstring,$p1,$p2;
                
           } else {
                $itempic="http://www.24x7.ru/images/picture.jsp?fid=".substr $pstring,$p1,$p2;
           };
           $badtok1=">";
           $badtok2="<";
           if(($itemname =~ /$badtok1/) || ($itemname =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
              print "<!-- Error processing $shop -->\n";
              last;
           };
           if(($itemauthor =~ /$badtok1/) || ($itemauthor =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
              print "<!-- Error processing $shop -->\n";
              last;
           };
           if(($itemprice =~ /$badtok1/) || ($itemprice =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
              print "<!-- Error processing $shop -->\n";
              last;
           };
           if(($itemref =~ /$badtok1/) || ($itemref =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
              print "<!-- Error processing $shop -->\n";
              last;
           };
           if(($itemdelivery =~ /$badtok1/) || ($itemdelivery =~ /$badtok2/)) {
              push(@showline,"***! Обработка страницы прервана. Неопознанный формат. ***\n");
              print "<!-- Error processing $shop -->\n";
              last;
           };
           if(($itempic =~ /$badtok1/) || ($itempic =~ /$badtok2/)) {
              $itempic="/images/blank_cover.gif";
           };
           $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;};

                  $strtop="$itemprice       ||$itemname||$itemauthor||http://www.24x7.ru$itemref\&banner_id=metasearch||$itemdelivery||$shop||".$oqsl."\&banner_id=metasearchmain||$itempic\n";
                  push(@showline2,$strtop);
           };
           $pstring="";
           $readytoprocess=0;
     };
   };


};

print "<!-- end 27x4 process -->";
$time2=time;

1;
