[download]

local/bin/pochta

   1 #!/bin/bash
   2 
   3 f=~/.cache/pochta
   4 
   5 if [[ ! $1 ]]; then
   6     [[ -s $f ]] || exit 1
   7     cat $f | xargs -rI % $0 % | sort -k 2
   8     exit
   9 fi
  10 
  11 grep -q $1 $f ||
  12 echo    $1 >> $f
  13 
  14 f+=-$1
  15 
  16 [[ -s $f ]] && (( $(date +%s) - $(date -r $f +%s) < 60 * 30 )) ||
  17 curl -so $f https://tracking.russianpost.ru/rtm34 \
  18 -H 'Content-Type: application/soap+xml' \
  19 -H 'SOAPAction: https://tracking.russianpost.ru/rtm34?wsdl' \
  20 -d @<(cat <<XML # {{{
  21 <?xml version="1.0" encoding="utf-8"?>
  22 <soap:Envelope
  23   xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
  24   xmlns:oper="http://russianpost.org/operationhistory"
  25   xmlns:data="http://russianpost.org/operationhistory/data"
  26   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  27 >
  28   <soap:Header />
  29   <soap:Body>
  30     <oper:getOperationHistory>
  31 
  32       <data:AuthorizationHeader soapenv:mustUnderstand="1">
  33         <data:login>$(pass tracking.pochta.ru | sed 's/^[^:]*: //; 2p; d')</data:login>
  34         <data:password>$(pass tracking.pochta.ru | sed 1q)</data:password>
  35       </data:AuthorizationHeader>
  36 
  37       <data:OperationHistoryRequest>
  38         <data:Barcode>$1</data:Barcode>
  39         <data:MessageType>0</data:MessageType>
  40         <data:Language>RUS</data:Language>
  41       </data:OperationHistoryRequest>
  42 
  43     </oper:getOperationHistory>
  44   </soap:Body>
  45 </soap:Envelope>
  46 XML
  47 ) # }}}
  48 
  49 while IFS=\> read -d \< tag value; do
  50     [[ $tag == ns3:Name     ]] && state=$value
  51     [[ $tag == ns3:NameRU   ]] && country=$value
  52     [[ $tag == ns3:Barcode  ]] && barcode=$value
  53     [[ $tag == ns3:OperDate ]] || continue
  54 
  55     date=$(date -d $value +'%F %H:%M')
  56     printf '%s\t%s\t%s\t%s\n' "$barcode" "$date" "$country" "$state"
  57 done < $f