BlueRey

car-api

Jun 23rd, 2020 (edited)
1,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.93 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. search=$(echo $1 | sed 's/\ /|/g')
  4.  
  5. carPlate=""
  6. isNum=false
  7. tmpFile="/tmp/output.txt"
  8. url="https://data.gov.il/api/3/action/datastore_search?resource_id=053cea08-09bc-40ec-8f7a-156f0677aff3&limit=99999"
  9. url_Degem="https://data.gov.il/api/3/action/datastore_search?resource_id=142afde2-6228-49f9-8a29-9b6c3a0cbe40"
  10. error_cmd="Usage: $0 <search> (-c [count per year] / -r [models] / -n [count total] / -f [info] / -a [info for all])"
  11.  
  12. if [ $# == 0 ]; then
  13.   echo $error_cmd
  14.   exit 1
  15. fi
  16.  
  17. case "$2" in
  18.         -c)
  19.                 curl -s "$url&q=$search" | jq .result.records[].shnat_yitzur | sort  > $tmpFile
  20.                 if [[ -f $tmpFile ]]; then
  21.                         years=$(awk -F" " '{print $1}' $tmpFile | sed 's/"//g' | sort | uniq)
  22.                         for y in $years;
  23.                         do
  24.                                 count=$(sed 's/"//g' $tmpFile | awk '$1=='$y'' | sort -n | wc -l)
  25.                                 echo $y - $count
  26.                         done
  27.                         echo -e "\nTotal: " $(wc -l < $tmpFile)
  28.                         rm $tmpFile
  29.                 else
  30.                         echo "Error: there is no results for the query"
  31.                 fi
  32.         ;;
  33.         -r)
  34.                 curl -s "$url&q=$search" | jq .result.records[].ramat_gimur | sed 's/\ /-/g' | sort  > $tmpFile
  35.                 if [[ -f $tmpFile ]]; then
  36.                         years=$(awk -F" " '{print $1}' $tmpFile | sort | uniq | sed 's/\"//g')
  37.                         for y in $years;
  38.                         do
  39.                                 count=$(grep $y $tmpFile | sort -n | wc -l)
  40.                                 echo $y - $count
  41.                         done
  42.                         echo -e "\nTotal: " $(wc -l < $tmpFile)
  43.                         rm $tmpFile
  44.                 else
  45.                         echo "Error: there is no results for the query"
  46.                 fi
  47.         ;;
  48.         -n)
  49.                 curl -s "$url&q='$search'" | jq ".result.records[]._id" | wc -l
  50.         ;;
  51.         -f)
  52.                 curl -s "$url&q='$search'" | jq ".result.records[] | del(._id,.rank,.tozeret_nm,.baalut,.sug_degem,.ramat_eivzur_betihuty,.kvutzat_zihum)"
  53.         ;;
  54.         -a)
  55.                 if ! [[ "$1" =~ ^[0-9]{7,8}$ ]]; then
  56.                         echo "Please enter vaild plate number"
  57.                         exit 1;
  58.                 fi
  59.                 output=$(curl -s "$url&q='$search'" | jq ".result.records[] | select(.mispar_rechev == $search)")
  60.                 if [ ${#1} == 7 ]; then
  61.                         carPlate=$(echo $1 | sed -re 's/([0-9])([0-9]{5})($|[^0-9])/\1-\2\3/' | sed -re 's/([0-9])([0-9]{2})($|[^0-9])/\1-\2\3/')
  62.                 elif [ ${#1} == 8 ]; then
  63.                         carPlate=$(echo $1 | sed -e 's/[0-9]\{3\}/&-/g')
  64.                 fi
  65.  
  66.                 if ! [[ -z $output ]]; then
  67.                         year=$(echo $output | awk -F"," '{print $11}' | awk '{print $2}')
  68.                         degem=$(echo $output | awk -F"," '{print $6}' | awk '{print $2}')
  69.                         model=$(echo $output | awk -F"," '{print $24}' | awk '{print $2}')
  70.                         gimur=$(echo $output | awk -F"," '{print $8}' | awk '{print $2}')
  71.                         testu=$(echo $output | awk -F"," '{print $14}' | awk '{print $2}')
  72.                         degemNum=$(echo $output | awk -F"," '{print $7}' | awk '{print $2}')
  73.                                                 vinNum=$(echo $output | awk -F"," '{print $16}' | awk '{print $2}')
  74.                         output2=$(curl -s "$url_Degem&q='$degem'" | jq ".result.records[] | select(.shnat_yitzur == $year) | select(.degem_nm == $degemNum) | {kvuzat_agra_cd,nefah_manoa,automatic_ind,koah_sus}")
  75.                         if ! [[ -z "$output2" ]]; then
  76.                                 kbutza=$(echo $output2 | awk -F"," '{print $1}' | awk '{print $3}')
  77.                                 gear=$(echo $output2 | awk -F"," '{print $3}' | awk '{print $2}')
  78.                                 cc=$(echo $output2 | awk -F"," '{print $2}' | awk '{print $2}')
  79.                                 hq=$(echo $output2 | awk -F"," '{print $4}' | awk '{print $2}')
  80.                                 if [ "$gear" == "0" ]; then gear="Manual"; else gear="Automatic"; fi
  81.                                 if [ -z "$carPlate" ]; then carnum=$search; else carnum=$carPlate; fi
  82.                                 echo -e "$model - $gimur - $cc cc - $hq hp - $year\n$carnum\nVIN number: $vinNum\nGear: $gear\nCode degem: $degemNum\nKbutzat rishoi: $kbutza\nTest until: $testu" | sed 's/"//g; s/T00:00:00//g'
  83.                         else
  84.                                 echo "Error: there is no results for the query"
  85.                         fi
  86.                 else
  87.                         echo "Error: there is no results for the query"
  88.                 fi
  89.         ;;
  90.         *)
  91.                 echo $error_cmd
  92.                 exit 1
  93.         ;;
  94. esac
Advertisement
Add Comment
Please, Sign In to add comment