Mrowqa

[GCI 2013] Tool for checking whether given task has reviewed

Nov 30th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.49 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo
  4. echo \(C\) 2013 Artur \"Mrowqa\" Jamro  -----  http://mrowqa.pl/
  5. echo Tool for auto-checking whether GCI task has reviewed.
  6. echo Version 1.17.2
  7. echo
  8.  
  9. ########################################################
  10. function getState() { # params: $task_id
  11.     local task_id=$1
  12.     result=$(wget -qO- http://www.google-melange.com/gci/task/view/google/$CONTEST_YEAR/$task_id 2> /dev/null | grep Status: | sed -r s/^.*\"emph\"\>//g | sed -r s/\<.*$//g)
  13.     echo $result
  14. }
  15.  
  16. function onStateChange() { # params: $newState, $prevState
  17.     local newState=$1
  18.     local prevState=$2
  19.    
  20.     echo
  21.     echo Task changed state!
  22.     echo New state: $newState
  23.     # notifyViaToast # you probably don't have "toast" command, so beeping is still default; i'll maybe upload this binary :)
  24.     notifyBeeping # you can choose these functions you want, you can even write some yourself :)
  25. }
  26.  
  27.  
  28. ########################################################
  29. ####### notify functions - you can choose these you want or even write some yourself :)
  30. function notifyBeeping () {
  31.     echo Press Ctrl-C to stop beeping.
  32.  
  33.     break_on_ctrl_c=0
  34.     while [ "$ctrl_c_pressed" == "0" ]; do
  35.         echo -en "\007" # beep
  36.         sleep 1s
  37.     done
  38.     ctrl_c_pressed=0
  39.     break_on_ctrl_c=1
  40. }
  41. function notifyViaLibnotify () { # Dedicated for GNOME Shell, will work partly on Unity and Xfce, also probably KDE and other DEs with libnotify support
  42.     # thanks to: Mateusz "m4tx" Maćkowski --- http://m4tx.pl
  43.     notify-send --expire-time=3600000 --urgency=critical 'GCI2013' "Task $task_id: $taskState! \nhttp://www.google-melange.com/gci/task/view/google/$CONTEST_YEAR/$task_id$@"
  44. }
  45. function notifyViaToast () { #win8+
  46.     local toastState=Timeout
  47.     local imageFile=$(pwd)/gci2013-logo.png
  48.     while [ "$toastState" == "Timeout" ]; do
  49.         toastState=$(toast -t "Task ID: $task_id" -p $imageFile -m "New task state: $taskState!
  50. Click here to see your task." -w)
  51.     done
  52.     if [ "$toastState" == "Activated" ]; then
  53.         explorer http://www.google-melange.com/gci/task/view/google/$CONTEST_YEAR/$task_id
  54.     fi
  55. }
  56. # tried write one notify function for win8 and message box, but still had problems with bash...
  57.  
  58.  
  59. ########################################################
  60. break_on_ctrl_c=1
  61. ctrl_c_pressed=0
  62. control_c() { # run if user hits control-c
  63.     ctrl_c_pressed=1
  64.     if [ "$break_on_ctrl_c" == "1" ]; then
  65.         echo Ctr-C interruption! Exiting...
  66.         exit
  67.     fi
  68. }
  69. # trap keyboard interrupt (control-c)
  70. trap control_c SIGINT
  71. ########################################################
  72. ########################################################
  73.  
  74. if [ -z "$1" ]; then
  75.     echo Usage: wait_for_task \<task_id\>
  76.     echo Usage: wait_for_task -
  77.     echo
  78.     echo If you choose second version, \<task_id\> will be read from "task.id" file.
  79.     exit
  80. fi
  81.  
  82. task_id=$1
  83. TIMESTAMP=60s
  84. CONTEST_YEAR=gci2013 # will probably works for GSoC too
  85.  
  86.  
  87. if [ "$task_id" == "-" ]; then
  88.     task_id=$(cat task.id)
  89. fi
  90.  
  91. echo Waiting for task state changes, task: $task_id
  92. echo Checking every $TIMESTAMP
  93.  
  94. prevState=$(getState $task_id)
  95. if [ "$prevState" == "Closed" ]; then
  96.     echo Task $task_id is closed!
  97.     exit
  98. fi
  99.  
  100. echo Current state: $prevState
  101. echo
  102.  
  103. while [ "true" ]; do
  104.     taskState=$(getState $task_id)
  105.     if [ "$taskState" != "$prevState" ]; then
  106.         onStateChange $taskState $prevState # wait for user interaction, don't check firstly once more state
  107.         if [ "$taskState" == "Closed" ]; then
  108.             break
  109.         fi
  110.         echo
  111.         echo Still waiting for state changes...
  112.     fi
  113.     prevState=$taskState
  114.     sleep $TIMESTAMP
  115. done
  116.  
  117. echo
  118. echo Task $task_id closed!
Advertisement
Add Comment
Please, Sign In to add comment