Advertisement
StSav012

rp5 site parser

Nov 25th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.49 KB | None | 0 0
  1. lang="ru"
  2. place="Нижний Новгород"
  3.  
  4. file=$(curl -s -o - "http://pda.rp5.ru/?lang=${lang}&q=${place}")
  5. links=$(echo ${file} | grep -o -e "p class=\"navy\">.*<form id=\"f\"" | grep -o -e "=[^=]*" | tail -n +2 | head -n -1)
  6. code=$(echo ${links} | grep -Eo -e "\"[[:digit:]]+" | grep -Eo -e "[[:digit:]]+" | head -n 1)
  7. name=$(echo ${links} | grep -Eo -e "${lang}\">[^<]+" | grep -Eo -e ">.+" | grep -Eo -e "[^>]+" | head -n 1)
  8.  
  9. file=$(wget -q -O - "http://rp5.ru/${code}/${lang}")
  10.  
  11. table6=$(echo ${file} | grep -Pzo "ftab_6_content(.|\n)*ftab_3_content")
  12. dates6=($(echo ${table6} | grep -o -e "monthDay[^\<]*" | grep -o -e "[^>]*$"))
  13. times6=($(echo ${table6} | grep -Eo -e "\"[nd][[:digit:]]? underlineRow\">[[:digit:]]+" | grep -o -e "[^>]*$"))
  14. temperatures6=($(echo ${table6} | grep -Eo -e "class=\"t_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  15. clouds6=($(echo ${table6} | grep -Eo -e "class=\"cc_0\"[^o]*" | grep -Eo -e "c[dn][[:digit:]]" | grep -Eo -e "[[:digit:]]+"))
  16. conditions6=($(echo ${table6} | grep -Eo -e "class=\"pr_0\"[^/]*" | grep -Eo -e "wp_[^\"_]*"))
  17. pressure6=($(echo ${table6} | grep -Eo -e "class=\"p_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  18.  
  19. table3=$(echo ${file} | grep -Pzo "ftab_3_content(.|\n)*ftab_1_content")
  20. dates3=($(echo ${table3} | grep -o -e "monthDay[^\<]*" | grep -o -e "[^>]*$"))
  21. times3=($(echo ${table3} | grep -Eo -e "\"[nd][[:digit:]]? underlineRow\">[[:digit:]]+" | grep -o -e "[^>]*$"))
  22. temperatures3=($(echo ${table3} | grep -Eo -e "class=\"t_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  23. clouds3=($(echo ${table3} | grep -Eo -e "class=\"cc_0\"[^o]*" | grep -Eo -e "c[dn][[:digit:]]" | grep -Eo -e "[[:digit:]]+"))
  24. conditions3=($(echo ${table3} | grep -Eo -e "class=\"pr_0\"[^/]*" | grep -Eo -e "wp_[^\"_]*"))
  25. pressure3=($(echo ${table3} | grep -Eo -e "class=\"p_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  26.  
  27. table1=$(echo ${file} | grep -Pzo "ftab_1_content(.|\n)*html")
  28. dates1=($(echo ${table1} | grep -o -e "monthDay[^\<]*" | grep -o -e "[^>]*$"))
  29. times1=($(echo ${table1} | grep -Eo -e "\"[nd][[:digit:]]? underlineRow\">[[:digit:]]+" | grep -o -e "[^>]*$"))
  30. temperatures1=($(echo ${table1} | grep -Eo -e "class=\"t_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  31. clouds1=($(echo ${table1} | grep -Eo -e "class=\"cc_0\"[^o]*" | grep -Eo -e "c[dn][[:digit:]]" | grep -Eo -e "[[:digit:]]+"))
  32. conditions1=($(echo ${table1} | grep -Eo -e "class=\"pr_0\"[^/]*" | grep -Eo -e "wp_[^\"_]*"))
  33. pressure1=($(echo ${table1} | grep -Eo -e "class=\"p_0\">[^/]*" | grep -Eo -e ">[-[:digit:]]+" | grep -Eo -e "[-[:digit:]]+"))
  34.  
  35. if [ ${#dates1[1]} -eq 2 ] && [ ${dates1[1]:0:1} == ${dates1[3]:0:1} ]
  36. then
  37.     dates1[1]=${dates1[3]}
  38. fi
  39. if [ ${#dates1[-1]} -eq 2 ] && [ ${dates1[-1]:0:1} == ${dates1[-3]:0:1} ]
  40. then
  41.     dates1[-1]=${dates1[-3]}
  42. fi
  43. if [ ${#dates3[1]} -eq 2 ] && [ ${dates3[1]:0:1} == ${dates3[3]:0:1} ]
  44. then
  45.     dates3[1]=${dates3[3]}
  46. fi
  47. if [ ${#dates3[-1]} -eq 2 ] && [ ${dates3[-1]:0:1} == ${dates3[-3]:0:1} ]
  48. then
  49.     dates3[-1]=${dates3[-3]}
  50. fi
  51. if [ ${#dates6[1]} -eq 2 ] && [ ${dates6[1]:0:1} == ${dates6[3]:0:1} ]
  52. then
  53.     dates6[1]=${dates6[3]}
  54. fi
  55. if [ ${#dates6[-1]} -eq 2 ] && [ ${dates6[-1]:0:1} == ${dates6[-3]:0:1} ]
  56. then
  57.     dates6[-1]=${dates6[-3]}
  58. fi
  59.  
  60. if [ ${#dates3[-1]} -eq 2 ]
  61. then
  62.     k=1
  63.     while [ ${#dates6[-k]} -eq 2 ]
  64.     do
  65.         k=$[$k+1]
  66.     done
  67.     if [ ${dates3[-1]:0:1} == ${dates6[-k]:0:1} ]
  68.     then
  69.         dates3[-1]=${dates6[-1]}
  70.     fi
  71. fi
  72. if [ ${#dates1[-1]} -eq 2 ]
  73. then
  74.     k=1
  75.     while [ ${#dates3[-k]} -eq 2 ]
  76.     do
  77.         k=$[$k+1]
  78.     done
  79.     if [ ${dates1[-1]:0:1} == ${dates3[-k]:0:1} ]
  80.     then
  81.         dates1[-1]=${dates3[-1]}
  82.     fi
  83. fi
  84.  
  85. i=0
  86. j=0
  87. k=0
  88. while [ $j -lt ${#times1[@]} ]
  89. do
  90.     forecast[$k]=$(printf '%s\t%s\t%s\t%s\t%s\t%s\n' "${dates1[i]} ${dates1[i+1]}" "${times1[j]}" "${temperatures1[j]}" "${clouds1[j]}" "${pressure1[j]}" "${conditions1[j+j]} ${conditions1[j+j+1]}")
  91.     k=$[$k+1]
  92.     j=$[$j+1]
  93.     if [ ! -z ${times1[j]} ] && [ ${times1[j]} -lt ${times1[j-1]} ]
  94.     then
  95.         i=$[$i+2]
  96.     fi
  97. done
  98.  
  99. i=0
  100. j=0
  101. while [ $j -lt ${#times3[@]} ]
  102. do
  103.     if [ ${dates3[i]} -gt ${dates1[-2]} ] && [ ${dates3[i+1]} == ${dates1[-1]} ]
  104.     then
  105.         break
  106.     fi
  107.     if [ ${dates3[i]} -eq ${dates1[-2]} ] && [ ${times3[j]} -gt ${times1[-1]} ]
  108.     then
  109.         break
  110.     fi
  111.     j=$[$j+1]
  112.     if [ ! -z ${times3[j]} ] && [ ${times3[j]} -lt ${times3[j-1]} ]
  113.     then
  114.         i=$[$i+2]
  115.     fi
  116. done
  117. while [ $j -lt ${#times3[@]} ]
  118. do
  119.     forecast[$k]=$(printf '%s\t%s\t%s\t%s\t%s\t%s\n' "${dates3[i]} ${dates3[i+1]}" "${times3[j]}" "${temperatures3[j]}" "${clouds3[j]}" "${pressure3[j]}" "${conditions3[j+j]} ${conditions3[j+j+1]}")
  120.     k=$[$k+1]
  121.     j=$[$j+1]
  122.     if [ ! -z ${times3[j]} ] && [ ${times3[j]} -lt ${times3[j-1]} ]
  123.     then
  124.         i=$[$i+2]
  125.     fi
  126. done
  127.  
  128. i=0
  129. j=0
  130. while [ $j -lt ${#times6[@]} ]
  131. do
  132.     if [ ${dates6[i]} -gt ${dates3[-2]} ] && [ ${dates6[i+1]} == ${dates3[-1]} ]
  133.     then
  134.         break
  135.     fi
  136.     if [ ${dates6[i]} -eq ${dates3[-2]} ] && [ ${times6[j]} -gt ${times3[-1]} ]
  137.     then
  138.         break
  139.     fi
  140.     j=$[$j+1]
  141.     if [ ! -z ${times6[j]} ] && [ ${times6[j]} -lt ${times6[j-1]} ]
  142.     then
  143.         i=$[$i+2]
  144.     fi
  145. done
  146. while [ $j -lt ${#times6[@]} ]
  147. do
  148.     forecast[$k]=$(printf '%s\t%s\t%s\t%s\t%s\t%s\n' "${dates6[i]} ${dates6[i+1]}" "${times6[j]}" "${temperatures6[j]}" "${clouds6[j]}" "${pressure6[j]}" "${conditions6[j+j]} ${conditions6[j+j+1]}")
  149.     k=$[$k+1]
  150.     j=$[$j+1]
  151.     if [ ! -z ${times6[j]} ] && [ ${times6[j]} -lt ${times6[j-1]} ]
  152.     then
  153.         i=$[$i+2]
  154.     fi
  155. done
  156.  
  157. echo ${name}
  158. printf "%s\n" "${forecast[@]}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement