@related: xml msvc cmake

ArpalHands's

#!/usr/bin/env bash

g_decimal_symbol=","
g_target_dimension="185/60 R15"
g_website="http://www.pneus-online.fr/pneu-ete.html"

cleanup()
{
  rm -f "${tmp_container[@]}"
}

generate_header()
{
  header_container=(
    "marque"
    "lien"
    "modele"
    "moyenne"
    "qualite_prix"
    "indice_rachat"
    "sol_sec"
    "sol_humide"
    "sol_enneige"
    "aquaplanning"
    "stabilite_virage"
    "confort_conduite"
    "confort_bruit"
    "tenue_route"
    "usure"
    "dimension"
    "charge"
    "vitesse"
    "consommation"
    "adherence"
    "bruit"
    "prix"
    $'\n'
  )
}

generate_stylesheet()
{
  cat << "EOF" > "${stylesheet1}"
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output method="text"/>

  <xsl:template match="text()|@*"/>

  <xsl:template match="table[@id='tableau']">
    <xsl:text>xsl_marque_container=()&#10;</xsl:text>            
    <xsl:text>xsl_lien_container=()&#10;</xsl:text>              
    <xsl:text>xsl_modele_container=()&#10;</xsl:text>            
    <xsl:text>xsl_moyenne_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_qualite_prix_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_indice_rachat_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_sol_sec_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_sol_humide_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_sol_enneige_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_aquaplanning_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_stabilite_virage_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_confort_conduite_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_confort_bruit_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_tenue_route_container=()&#10;</xsl:text>                       
    <xsl:text>xsl_usure_container=()&#10;</xsl:text>                       
    <xsl:for-each select="tbody/tr">
      <xsl:text>xsl_marque_container+=("</xsl:text>            
      <xsl:value-of select="td[position() = 1]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_lien_container+=("http://www.pneus-online.fr</xsl:text>              
      <xsl:value-of select="td[position() = 2]/a/@href"/>  
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_modele_container+=("</xsl:text>            
      <xsl:value-of select="td[position() = 2]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_moyenne_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 3]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_qualite_prix_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 5]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_indice_rachat_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 6]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_sol_sec_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 7]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_sol_humide_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 8]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_sol_enneige_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 9]"/>          
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_aquaplanning_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 10]"/>         
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_stabilite_virage_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 11]"/>         
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_confort_conduite_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 12]"/>         
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_confort_bruit_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 13]"/>         
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_tenue_route_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 14]"/>         
      <xsl:text>")&#10;</xsl:text>                         
      <xsl:text>xsl_usure_container+=("</xsl:text>                       
      <xsl:value-of select="td[position() = 15]"/>         
      <xsl:text>")&#10;</xsl:text>                         
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
EOF

  cat << "EOF" > "${stylesheet2}"
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output method="text"/>

  <xsl:template match="text()|@*"/>

  <xsl:template match="div[@class = 'bodyContent1BlockResultats']">
    <xsl:text>xsl_description_container=()&#10;</xsl:text>
    <xsl:text>xsl_consommation_container=()&#10;</xsl:text>
    <xsl:text>xsl_adherence_container=()&#10;</xsl:text>
    <xsl:text>xsl_bruit_container=()&#10;</xsl:text>
    <xsl:text>xsl_prix_container=()&#10;</xsl:text>
    <xsl:for-each select="div[@class = 'resultatProduitAuto']">
      <xsl:text>xsl_description_container+=("</xsl:text>
      <xsl:value-of select="div/p"/>
      <xsl:text>")&#10;</xsl:text>
      <xsl:text>xsl_consommation_container+=("</xsl:text>            
      <xsl:value-of select="div/div/img[position() = 2]/@src"/>  
      <xsl:text>")&#10;</xsl:text>                               
      <xsl:text>xsl_adherence_container+=("</xsl:text>               
      <xsl:value-of select="div/div/img[position() = 4]/@src"/>  
      <xsl:text>")&#10;</xsl:text>                               
      <xsl:text>xsl_bruit_container+=("</xsl:text>                   
      <xsl:value-of select="div/div/img[position() = 6]/@src"/>  
      <xsl:text>")&#10;</xsl:text>                               
      <xsl:text>xsl_prix_container+=("</xsl:text>
      <xsl:value-of select="table/tr/td/div[@class = 'resultatProduitPrixDevise nobr']"/>
      <xsl:text>")&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
EOF
}

apply_decimal_symbol_rule()
{
  local tmp_container=(
    xsl_moyenne_container
    xsl_qualite_prix_container
    xsl_indice_rachat_container
    xsl_sol_sec_container
    xsl_sol_humide_container
    xsl_sol_enneige_container
    xsl_aquaplanning_container
    xsl_stabilite_virage_container
    xsl_confort_conduite_container
    xsl_confort_bruit_container
    xsl_tenue_route_container
    xsl_usure_container
  )
  local container
  for container in "${tmp_container[@]}"
  do
    eval "${container}"'=("${'"${container}"'[@]/./${g_decimal_symbol}}")'
    eval "${container}"'=("${'"${container}"'[@]/,/${g_decimal_symbol}}")'
  done
}
 
display()
{
  local consommation="${xsl_consommation_container[idx2]%.*}"
  consommation="${consommation#*-}"
  consommation="${consommation:= }"
  local adherence="${xsl_adherence_container[idx2]%.*}"
  adherence="${adherence#*-}"
  adherence="${adherence:= }"
  local bruit="${xsl_bruit_container[idx2]%.*}"
  bruit="${bruit#*-}"
  bruit="${bruit:= }"
  local prix="$(echo "${xsl_prix_container[idx2]}" | awk '{print $1}' | tr '.,' "${g_decimal_symbol}${g_decimal_symbol}")"
  local tmp_container=(
    "${xsl_marque_container[idx1]}"
    "${xsl_lien_container[idx1]}"
    "${xsl_modele_container[idx1]}"
    "${xsl_moyenne_container[idx1]}"
    "${xsl_qualite_prix_container[idx1]}"
    "${xsl_indice_rachat_container[idx1]}"
    "${xsl_sol_sec_container[idx1]}"
    "${xsl_sol_humide_container[idx1]}"
    "${xsl_sol_enneige_container[idx1]}"
    "${xsl_aquaplanning_container[idx1]}"
    "${xsl_stabilite_virage_container[idx1]}"
    "${xsl_confort_conduite_container[idx1]}"
    "${xsl_confort_bruit_container[idx1]}"
    "${xsl_tenue_route_container[idx1]}"
    "${xsl_usure_container[idx1]}"
    "${dimension}"
    "${charge}"
    "${vitesse}"
    "${consommation}"
    "${adherence}"
    "${bruit}"
    "${prix}"
  )
  IFS=$'\t'
  echo -n -e "${header_container[*]}"
  unset header_container
  echo -e "${tmp_container[*]}"
  unset IFS
}

main()
{
  trap cleanup EXIT
  local header_container=()
  generate_header
  tmp_container+=("$(mktemp -u)")
  local stylesheet1="${tmp_container[${#tmp_container[@]}-1]}"
  tmp_container+=("$(mktemp -u)")
  local stylesheet2="${tmp_container[${#tmp_container[@]}-1]}"
  generate_stylesheet
  source <(xsltproc --html "${stylesheet1}" "${g_website}" 2> /dev/null)
  source <(
    local is_valid=$(
      local container
      for container in "${!xsl_@}"
      do
        eval echo '"${#'"${container}"'[@]}"'
      done | sort -u | wc -l
    )
    [ "${is_valid}" = "1" ]
    is_valid="$?"
    declare -p is_valid
  )
  [ "${is_valid}" = "0" ] || { echo "nok"; return; }
  apply_decimal_symbol_rule
  local idx1
  for ((idx1=0; idx1<"${#xsl_lien_container[@]}"; ++idx1))
  do
    source <(
      local url="${xsl_lien_container[idx1]}"
      unset "${!xsl_@}"
      source <(xsltproc --html "${stylesheet2}" "${url}" 2> /dev/null)
      source <(
        local is_valid=$(
          local container
          for container in "${!xsl_@}"
          do
            eval echo '"${#'"${container}"'[@]}"'
          done | sort -u | wc -l
        )
        [ "${is_valid}" = "1" ]
        is_valid="$?"
        declare -p is_valid
      )
      if [ "${is_valid}" = "0" ]
      then
        declare -p "${!xsl_@}"
      fi
      declare -p is_valid
    )
    [ "${is_valid}" = "0" ] || { echo "nok"; continue; }
    local idx2
    for ((idx2=0; idx2<"${#xsl_description_container[@]}"; ++idx2))
    do
      local dimension="$(echo "${xsl_description_container[idx2]}" | grep -o '[[:digit:]]\{3\}/[[:digit:]]\{2\} R[[:digit:]]\{2\}')"
      if [ "${dimension}" = "${g_target_dimension}" ]
      then
        local charge="$(echo "${xsl_description_container[idx2]}" | grep -o 'Charge : [[:digit:]]\+ ' | grep -o '[[:digit:]]\+')"
        local vitesse="$(echo "${xsl_description_container[idx2]}" | grep -o 'Vitesse : .' | sed 's/Vitesse : //')"
        display
      fi
    done
  done
}

main "$@"
xsl.txt · Last modified: 2020/09/08 20:51 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki