Created
January 30, 2017 14:39
-
-
Save rajiteh/f0afb379658183ac8cba4f76d93ba4a1 to your computer and use it in GitHub Desktop.
bitbar citizenship canada timers and status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PATH=$PATH:/usr/local/bin/ | |
WORK_DIR=~/.bitbar/pr_status | |
CHANGE_FILE="${WORK_DIR}/changed" | |
LOGGING=0 | |
mkdir -p ${WORK_DIR} | |
function errcho { | |
if [ "${LOGGING}" != "0" ]; then | |
>&2 echo $@; | |
fi | |
} | |
function uid { | |
md5 -s "${1}" | awk '{print $4}' | |
} | |
function sessionid_path { | |
echo ${WORK_DIR}/session_$(uid ${1}) | |
} | |
function authenticate_and_retrive_session { | |
local auth_fragment=$1 | |
local result=$(curl 'https://services3.cic.gc.ca/ecas/authenticate.do' -X 'POST' -vv --data "lang=&_page=_target0&app=&_submit=Continue&${auth_fragment}" 2>&1 | grep 'Set-Cookie' | \ | |
awk -F'JSESSIONID=|;' '{print $2}') | |
errcho "AUTHANDRETRIEVE: Output: ${result}" | |
echo "${result}" | |
} | |
function create_session { | |
local session_file=$(sessionid_path ${1}) | |
rm -f ${session_file} && touch "${session_file}" | |
if [ $1 == 'supu' ]; then | |
errcho 'CREATESESSION: Creating session for supu' | |
authenticate_and_retrive_session "identifierType=2&identifier=xxxx&surname=xxxx&dateOfBirth=xxxx-xx-xx&countryOfBirth=201" > "${session_file}" | |
else | |
errcho 'CREATESESSION: Creating session for not supu' | |
authenticate_and_retrive_session "identifierType=1&identifier=xxxx&surname=xx&dateOfBirth=xxxx-xx-xx&countryOfBirth=201" > "${session_file}" | |
fi | |
} | |
function get_sessionid { | |
local session_type=$1 | |
local do_renew=$2 | |
local le_token=$(cat $(sessionid_path "${session_type}") 2>/dev/null) | |
errcho "GETSESSIONID: Retrieving token for $session_type Renew? $do_renew" | |
if [ -n "${do_renew}" ] || [ ! -n "${le_token}" ]; then | |
create_session "${session_type}" | |
le_token=$(cat $(sessionid_path "${session_type}")) | |
fi | |
errcho "GETSESSIONID: Found session token : ${le_token}" | |
echo "${le_token}" | |
} | |
function get_status { | |
local url=$1 | |
local session_type=$2 | |
local renew_session=$3 | |
local result_file=$(mktemp) | |
local stored_file=${WORK_DIR}/$(uid "${url}") | |
touch ${stored_file} | |
rm -f ${result_file} | |
errcho "GETSTATUS: Start URL: ${url} session: ${session_type} renew: ${renew_session}" | |
local session_id=$(get_sessionid "${session_type}" "${renew_session}") | |
curl_out=$(curl -fSs "${url}" -H "Cookie: JSESSIONID=${session_id}" | pup -p 'form[action=viewcasehistory.do] ol li text{}' | sed -Ee "s/^[ \t]*|[ \t]*$//g") | |
# save a tempfile only if we have results | |
if [ -n "${curl_out}" ]; then | |
echo "${curl_out}" > "${result_file}" | |
else | |
errcho "GETSTATUS: No output from pr_page" | |
if [ ! -n "${renew_session}" ]; then | |
errcho "GETSTATUS: Tryin again with session renew" | |
if get_status "${url}" "${session_type}" "1"; then | |
return 0 | |
else | |
return 1 | |
fi | |
else | |
errcho "GETSTATUS: No matches m8 after session renew" | |
return 1 | |
fi | |
fi | |
if ! (diff -q ${result_file} ${stored_file} 2>&1 >/dev/null); then | |
errcho "GETSTATUS: Status was changed!" | |
touch "${CHANGE_FILE}" | |
fi | |
echo "${curl_out}" > "${stored_file}" | |
echo "${curl_out}" | |
return 0 | |
} | |
supu=$(get_status 'https://services3.cic.gc.ca/ecas/viewcasehistory.do?id=xxxxx&type=prCases&source=db&app=&lang=en' 'supu') | |
rsupu=$(get_status 'https://services3.cic.gc.ca/ecas/viewcasehistory.do?id=xxxxx&type=prSponsoredCases&source=db&app=&lang=en' 'raji') | |
raji=$(get_status 'https://services3.cic.gc.ca/ecas/viewcasehistory.do?id=xxxxx&type=sponsorshipCases&source=db&app=&lang=en' 'raji') | |
echo -n ":poop:" | |
if [ -e "${CHANGE_FILE}" ]; then | |
echo ":exclamation::exclamation:" | |
else | |
echo "" | |
fi | |
echo "---" | |
echo "Supul:" | |
echo "${supu}" | |
echo "---" | |
echo "R-Supul:" | |
echo "${rsupu}" | |
echo "---" | |
echo "Rajitha:" | |
echo "${raji}" | |
rm -f "${CHANGE_FILE}" | |
0Looking |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
import urllib2 | |
import re | |
PROCESSING_TIMES_URL = "http://www.cic.gc.ca/js/tools/processing-times/pt-messages.js" | |
APPLIED_DATE = datetime.datetime(2015, 9, 9) | |
response = urllib2.urlopen(PROCESSING_TIMES_URL) | |
content = response.read() | |
matches = re.search(r'.*spouse_out.*LK:"([0-9]*)".*lastupdated:"([0-9\/]*)".*', content) | |
months_left = int(matches.group(1)) | |
updated_date = datetime.datetime.strptime(matches.group(2), "%Y/%m/%d") | |
target_date = datetime.datetime(APPLIED_DATE.year + (APPLIED_DATE.month + months_left - 1) / 12, 1 + (APPLIED_DATE.month + months_left-1) % 12, APPLIED_DATE.day) | |
date_diff = target_date - datetime.datetime.now() | |
from_diff = datetime.datetime.now() - APPLIED_DATE | |
print("{} months.".format(months_left)) | |
print("{} days.".format(date_diff.days)) | |
print("{} days ago.".format(from_diff.days)) | |
print("{:.2f}%".format(100 * float(from_diff.total_seconds())/float(date_diff.total_seconds() + from_diff.total_seconds()))) | |
print("---") | |
print("Target date: {}".format(target_date.strftime("%d/%m/%Y"))) | |
print("Updated: {}".format(updated_date.strftime("%d/%m/%Y"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment