Advertisement
sjakub

ECAS Fetch

Dec 1st, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.23 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # A bash script for automatic fetching ECAS case history.
  4. #
  5. #
  6. #
  7. # DISCLAIMER:
  8. #
  9. #
  10. # !!! YOU USE THIS SCRIPT AT YOUR OWN RISK !!!
  11. #
  12. # I have tried it and it works for me, but I don't guarantee
  13. # that it will work for you!
  14. #
  15. # Moreover, I am not responsible if it breaks your system,
  16. # removes the website access to your application status,
  17. # locks you out completely and/or in any way gets you in trouble with CIC,
  18. # authorities or anybody else.
  19. #
  20. # You are responsible for any and all consequences of using this script!
  21. #
  22. #
  23. # AGAIN:
  24. #
  25. # !!! YOU USE THIS SCRIPT AT YOUR OWN RISK !!!
  26. #
  27. #
  28. #
  29. #
  30. # Requirements: curl, awk, grep, sed, cut.
  31. #
  32. # First, make sure to configure the script!
  33. #
  34. # Be very careful filling out all the details,
  35. # any extra spaces WILL prevent it from working properly!
  36. #
  37.  
  38. #
  39. # CONFIGURATION
  40. #
  41.  
  42. # Country code.
  43. # To get this code, go to: https://services3.cic.gc.ca/ecas/authenticate.do?app=ecas
  44. # Open the page source and find your country's name - the code will be right above it.
  45. # For example: <option value="461">United States of America</option>
  46. # So for the USA you should use 461 code.
  47. COUNTRY="461"
  48.  
  49. # The type of the identifier:
  50. # 1 - Client ID Number / Unique Client Identifier
  51. # 2 - Receipt Number (IMM 5401)
  52. # 3 - Application Number / Case Number
  53. # 4 - Record of Landing Number
  54. # 5 - Permanent Resident Card Number
  55. # 6 - Citizenship Receipt Number
  56. # 7 - Citizenship File Number / Group Number
  57. # 8 - Confirmation of Permanent Residence Number
  58. ID_TYPE="1"
  59.  
  60. # Identifier of that type (so the UCI number if '1' above, etc.)
  61. ID="12345678"
  62.  
  63. # Surname/Family Name:
  64. SURNAME="Surname"
  65.  
  66. # Date of birth (in YYYY-MM-DD format):
  67. DOB="2015-11-30"
  68.  
  69.  
  70. # Set this to '1' to mark the fact that you finished configuring this script:
  71. CONFIGURED="0"
  72.  
  73.  
  74. #
  75. # END-OF-CONFIGURATION
  76. #
  77. # (Don't change anything below this line!!!)
  78. #
  79.  
  80.  
  81. if [ ! "${CONFIGURED}" = "1" ]; then
  82.   echo
  83.   echo "Please configure the script first!"
  84.   echo
  85.   exit 1
  86. fi
  87.  
  88. function showError()
  89. {
  90.   echo
  91.   echo "There was an error: $1"
  92.   echo "Make sure that all the details and identifiers are correct "
  93.   echo "and that you can access the data using regular browser!"
  94.   echo
  95. }
  96.  
  97. URL="https://services3.cic.gc.ca/ecas"
  98.  
  99. COOKIE=$( curl -v "${URL}/authenticate.do?app=ecas" 2>&1 |
  100.             grep 'Set-Cookie: ' | cut -d ' ' -f 3 )
  101.  
  102. if [ "${COOKIE}" = "" ]; then
  103.   showError "Could not read the session cookie"
  104.   exit 1
  105. fi
  106.  
  107. AUTH_DATA="lang=&_page=_target0&app=ecas"
  108. AUTH_DATA="${AUTH_DATA}&identifierType=${ID_TYPE}"
  109. AUTH_DATA="${AUTH_DATA}&identifier=${ID}"
  110. AUTH_DATA="${AUTH_DATA}&surname=${SURNAME}"
  111. AUTH_DATA="${AUTH_DATA}&dateOfBirth=${DOB}"
  112. AUTH_DATA="${AUTH_DATA}&countryOfBirth=${COUNTRY}"
  113. AUTH_DATA="${AUTH_DATA}&_submit=Continue"
  114.  
  115. curl "${URL}/authenticate.do" -H "Cookie: ${COOKIE}" \
  116.   -H 'Content-Type: application/x-www-form-urlencoded' --data "${AUTH_DATA}"
  117.  
  118. CASE_LIST=$( curl "${URL}/viewcasestatus.do?app=ecas" \
  119.   -H "Cookie: ${COOKIE}" 2>/dev/null )
  120.  
  121. CASE_PREFIX='<a href="viewcasehistory.do?id='
  122.  
  123. CASE_IDS=$( echo "${CASE_LIST}" | grep "${CASE_PREFIX}" | cut -d '=' -f 3 | cut -d '&' -f 1 )
  124.  
  125. if [ "${CASE_IDS}" = "" ]; then
  126.   showError "Could not get the list of case IDs - most likely the authentication failed"
  127.   exit 1
  128. fi
  129.  
  130. for CID in ${CASE_IDS}; do
  131.   C_STATUS=$( echo "${CASE_LIST}" | grep "${CASE_PREFIX}${CID}" |
  132.                 cut -d '>' -f 2-99 | sed 's|</b>|-|g' | sed 's|<[^>]*>||g' )
  133.  
  134.   C_NAME=$( echo "${CASE_LIST}" | grep -B 4 "${CASE_PREFIX}${CID}" |
  135.                 head -n 2 | sed 'N;s/\n/ /' | awk '{$2=$2};1' )
  136.  
  137.   DATA=$( curl \
  138.             "${URL}/viewcasehistory.do?id=${CID}&type=citCases&source=db&app=ecas&lang=en" \
  139.             -H "Cookie: ${COOKIE}" 2>/dev/null )
  140.  
  141.   DATA_OK=$( echo "${DATA}" | grep '<li class="margin-bottom-medium">' |
  142.                sed 's|.*<li class="margin-bottom-medium">|* |' | sed 's|</b>|-|g' | sed 's|<[^>]*>||g' )
  143.  
  144.   echo
  145.   echo "Case ${CID}; Name: ${C_NAME}; Status: ${C_STATUS}"
  146.  
  147.   if [ "${DATA_OK}" = "" ]; then
  148.     showError "Could not parse the details of case ${CID}"
  149.   else
  150.     ENTR=$( echo "${DATA_OK}" | wc -l )
  151.     echo
  152.     echo "History (${ENTR} entries):"
  153.     echo
  154.     echo "${DATA_OK}"
  155.     echo
  156.   fi
  157. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement