Advertisement
Spielkalb

tournget

Jun 13th, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.82 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # tournget.sh
  4. # script for downloading and merging multiple tournament *.pgn files from
  5. # chess.com
  6. # Author: ralf_h
  7. # Contact: ralf_h-linux<at>gmx.de
  8. #
  9. # http://pastebin.com/R32G8SaE
  10. #
  11. # IMPORTANT NOTE:
  12. # run "sed -i 's/\r//' tournget.sh" to remove CRLF line terminators
  13. #
  14.  
  15. echo
  16. echo "This script is for downloading and merging multiple tournament *.pgn files from chess.com."
  17. until [[ "$TOURN" =~ [0-9]+ ]]
  18.     do echo
  19.         echo "First we need the identifier of the tournament. Visit the games page of your tournament and copy the plain number you see in the address bar."
  20.         read -p "Insert tournament identifier (e.g. 87416): " TOURN
  21.     done
  22. until [[ "$PAGES" =~ [0-9]+ ]]
  23.     do echo
  24.         echo "How many pages are shown on the tournament games page?"
  25.         read -p "Insert number of pages (e.g. 7): " PAGES
  26.     done
  27. until [ -n "$DIR" ]
  28.     do echo
  29.         echo "In which directory do you want to save your output file?"
  30.         read -ep "Insert directory: $HOME/" -i Downloads/ DIR
  31.     done
  32. until [ -n "$NAME" ]
  33.     do echo
  34.         echo "Choose the name of your output file."
  35.         read -ep "Insert NAME[.pgn] (without '.pgn'): " -i mytournament NAME
  36.     done
  37. echo
  38. echo "Please confirm your input data."
  39. echo "Press any key to continue or 'q' to quit."
  40. read -sn1 char
  41. if echo $char | grep -iq 'q'
  42.     then echo
  43.         echo "Aborted by user."
  44.         exit 1
  45. fi
  46.  
  47. if [ ! -d "$HOME"/"$DIR" ]
  48.     then mkdir -vp $HOME/$DIR
  49. fi
  50. pushd $HOME/$DIR 1>/dev/null
  51.  
  52. if [ -f "$NAME".pgn ]
  53.     then echo
  54.         echo "File \"$NAME.pgn\" already exists!"
  55.         echo "Press any key to abort or 'o' to override file."
  56.         read -sn1 char
  57.         if echo $char | grep -iq 'o'
  58.             then echo
  59.                 echo "$NAME.pgn will be overwritten."
  60.             else echo
  61.                 echo "Aborted. Please choose another output file name."
  62.                 exit 1
  63.         fi
  64. fi
  65.  
  66. if [ -f /tmp/urigames ]
  67.     then
  68.         rm /tmp/urigames
  69. fi
  70.  
  71. echo
  72. echo "Looking for *pgn files, please wait..."
  73. for (( i = 1; i <= $PAGES; i++ ))
  74.     do "echo reading page $i"
  75.         wget -O - -o /dev/null "http://www.chess.com/tournaments/games?id=$TOURN&page=$i" | grep echess/game >>/tmp/urigames
  76. done
  77.  
  78. if grep -sqm1 'echess/game' /tmp/urigames
  79.     then sed 's/^.*game?id/http:\/\/www.chess.com\/echess\/download_pgn?id/;s/"\sid.*$//' /tmp/urigames >/tmp/pgnuris
  80.     else echo
  81.         echo "Can't find any games. Please check your input parameters."
  82.         echo
  83.         rm -f /tmp/urigames /tmp/pgnuris
  84.         exit 1
  85. fi
  86.  
  87. if [ -f /tmp/pgnuris ]
  88.     then echo
  89.         echo "Downloading and merging `wc -l /tmp/pgnuris | sed 's/\s.*$//'` *.pgn files. This might take a while, please wait..."
  90.         wget -i /tmp/pgnuris -O - -o /dev/null >$NAME.pgn
  91.     else echo
  92.         echo "Error while compiling PGN file!"
  93.         exit 1
  94. fi
  95.  
  96. echo
  97. echo "Deleting temporary files..."
  98. rm -v /tmp/urigames /tmp/pgnuris
  99. popd 1>/dev/null
  100. echo
  101. echo "Done. Your tournament archive \"$NAME.pgn\" in \"$HOME/$DIR\" is ready to use! :)"
  102. echo
  103. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement