Advertisement
yo9gjx

savemeqsl

Nov 13th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.95 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2011 Federico Ch. Tomasczik (LW3ESH)
  4. # E-Mail: <ftomasczik at gmail dot com>
  5. #
  6. # savemeqsl (Save Me eQSL) is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # savemeqsl is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with savemeqsl.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ###########################################################################
  20. # Thanks to:
  21. #
  22. # Cezar, YO3FHM
  23. # Jan, ES6JAN
  24. # Albert, EA3CFV
  25. ###########################################################################
  26. VERSION=0.2.0
  27.  
  28. TMP="/var/tmp"
  29. URL="http://www.eqsl.cc/qslcard/"
  30.  
  31.  
  32. # Print the version number:
  33. function printVersion
  34. {
  35.     echo ""
  36.     echo $"Save Me eQSL: savemeqsl version $VERSION."
  37.     echo $"Simple bash script for download yours Electronic QSL cards from eQSL.cc site."
  38.     echo ""
  39.     echo $"Latest version is at :"
  40.     echo '      http://code.google.com/p/savemeqsl/'
  41.     echo ""
  42.     echo "              73 de LW3ESH, Federico Ch. Tomasczik"
  43.     echo ""
  44. }
  45.  
  46. # Funtion to print the help and exit:
  47. function printHelpAndExit
  48. {
  49.     exitCode=$1
  50.     echo $" Usage $0: [-hvoVq] -u callsign -p passwrd -d directory."
  51.     echo $"  -h, --help print this help."
  52.     echo $"  -v, --version show version."
  53.     echo $"  -o, overwrite ALL eQSL files."
  54.     echo $"  -q, --quiet don't show any information."
  55.     echo $"  -u your callsign or user in eQSL.cc."
  56.     echo $"  -p your password in eQSL.cc."
  57.     echo $"  -d directory where save yours eQSL."
  58.     exit $exitCode
  59. }
  60.  
  61. # Function to show fatal errors and exit:
  62. function showErrorAndExit
  63. {
  64.     exitCode=$2
  65.     printf "$1"
  66.     exit $exitCode
  67. }
  68.  
  69. # Function to read the users options:
  70. function parseOptions
  71. {
  72.     while getopts "hvou:p:d:-help-verbose-version-quiet" OPTION; do    
  73.         case "$OPTION" in
  74.             h|--help)   printHelpAndExit 0;;
  75.             v|--version)    printVersion;exit 0;;
  76.             q|--quiet)  QUIET="Y";;
  77.             o)  OVERWRITE="Y";;
  78.             u)  USER=$OPTARG;;
  79.             p)  PASS=$OPTARG;;
  80.             d)  DESTINATION=$OPTARG
  81.                 # Check if $DESTINATION exist.
  82.                 if [ ! -d $DESTINATION ];then
  83.                     showErrorAndExit $"The directory $DESTINATION does not exist!\nCheck this and try again.\n" 20
  84.                 else   
  85.                     # Check if $DESTINATION if writeable for the user.
  86.                     if [ ! -w $DESTINATION ]
  87.                     then
  88.                         showErrorAndExit $"You can not write into $DESTINATION!\nCheck this and try again.\n" 20
  89.                     fi
  90.                 fi;;
  91.             [?])    showErrorAndExit $"Option not recognised.\nTry '$0 -hยด for more information.\n" 21;;
  92.         esac
  93.     done
  94. }
  95.  
  96. # Function to connect to eQSL site and get cookies:
  97. function eQSLConnect
  98. {
  99. if [ ! "$QUIET" ];then
  100.     echo $" Connecting to eQSL site..."
  101. fi
  102.  
  103. wget -T 25 -q --cookies=on --keep-session-cookies --save-cookies="$TMP/cookie.txt" -O "$TMP/tmp.txt" $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go
  104. }
  105.  
  106. # Function to login to eQSL site:
  107. function eQSLLogin {
  108. if [ ! "$QUIET" ];then
  109.     echo $" Login to eQSL site..."
  110. fi
  111.  
  112. wget -T 25 -q --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies --save-cookies="$TMP/cookie.txt" -O "$TMP/tmp.txt" $URL"DownloadInBox.cfm"
  113.  
  114.  
  115. # If file tmp.txt contain 'You are not yet logged in' then...
  116. t=`grep "You are not yet logged in" $TMP/tmp.txt`
  117. if [ -n "$t" ];then
  118.     showErrorAndExit $" User or password are incorrect.\nPlease, try again.\n" 21
  119.     else
  120.     if [ ! "$QUIET" ];then
  121.         echo $" Login OK..."
  122.     fi
  123. fi
  124. }
  125.  
  126. # Function to get your loogbook:
  127. function getLogBook
  128. {
  129. t=`grep ".TXT file" $TMP/tmp.txt | cut -d '"' -f2`
  130.  
  131. if [ -z $t ]; then
  132.     showErrorAndExit $"The file $TMP/tmp.txt don't look like a loogbook.\n" 22
  133. fi
  134.  
  135. if [ ! "$QUIET" ];then
  136.     echo $" Downloading you logbook file..."
  137. fi
  138.  
  139. wget -T 25 -q --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies -O "$TMP/log.txt" $URL$t
  140.  
  141.  
  142. if [ ! "$QUIET" ];then
  143.     echo $" Reading you logbook file..."
  144. fi
  145. }
  146.  
  147. # Function to get the eQSL images:
  148. function getQSL
  149. {
  150. grep "<CALL:" $TMP/log.txt | while read LINE
  151. do
  152. export n=0
  153. CALLSIGNAL=$(echo $LINE | cut -d '<' -f2 | cut -d '>' -f2)
  154. DATE=$(echo $LINE | cut -d '<' -f3 | cut -d '>' -f2)
  155. TIME=$(echo $LINE | cut -d '<' -f4 | cut -d '>' -f2)
  156. BAND=$(echo $LINE | cut -d '<' -f5 | cut -d '>' -f2)
  157. MODE=$(echo $LINE | cut -d '<' -f6 | cut -d '>' -f2)
  158.  
  159. FILE_NAME=$CALLSIGNAL"_"$DATE"_"$TIME"_"$BAND"_"$MODE
  160. FILE_NAME=$(echo $FILE_NAME|sed 's/\//-/g')
  161.  
  162. YEAR=$(expr substr $DATE 1 4)
  163. MONTH=$(expr substr $DATE 5 2)
  164. DAY=$(expr substr $DATE 7 2)
  165. HOUR=$(expr substr $TIME 1 2)
  166. MINUTE=$(expr substr $TIME 3 2)
  167. TIME="$(expr substr $TIME 1 2)":"$(expr substr $TIME 3 2)"
  168. DATE="$(expr substr $DATE 1 4)"-"$(expr substr $DATE 5 2)"-"$(expr substr $DATE 7 2)"
  169.  
  170. wget -T 25 -q --referer $URL"LoginFinish.cfm?Callsign="$USER\&EnteredPassword=$PASS\&Login=Go --cookies=on --load-cookies="$TMP/cookie.txt" --keep-session-cookies -O "$TMP/tmp.txt" $URL"DisplayeQSL.cfm?Callsign="$CALLSIGNAL"&VisitorCallsign="$USER"&QSODate="$DATE"%20"$TIME"&Band="$BAND"&Mode="$MODE
  171.  
  172. # For default, the images are in .PNG
  173.     URL_IMG=`grep ".PNG" $TMP/tmp.txt | cut -d '"' -f2 | cut -d '/' -f4`
  174.  
  175. if [ -z "$URL_IMG" ]; then
  176. # If $URL_IMG is null, then check por .JPG:
  177.     URL_IMG=`grep ".JPG" $TMP/tmp.txt | cut -d '"' -f2 | cut -d '/' -f4`
  178.     FILE_NAME=$FILE_NAME".jpg"
  179. else
  180.     FILE_NAME=$FILE_NAME".png"
  181. fi
  182.  
  183. if [ -f $DESTINATION"/"$FILE_NAME ] && [ ! $OVERWRITE ];then
  184.         if [ ! "$QUIET" ];then
  185.             echo $" The $FILE_NAME exists. No overwrite it."
  186.         fi
  187. else
  188.  
  189.     if [ ! "$QUIET" ];then
  190.         echo $" Downloading eQSL for you QSO with $CALLSIGNAL..."
  191.     fi
  192.  
  193. wget -T 25 -q -O $DESTINATION"/"$FILE_NAME "http://www.eqsl.cc/CFFileServlet/_cf_image/$URL_IMG"
  194.  
  195. # Check the file size. If this is 0 byte, then it is removed.
  196. FILE_SIZE=`stat -c %s $DESTINATION"/"$FILE_NAME`
  197.  
  198.     if [ $FILE_SIZE -eq 0 ]; then
  199.         rm $DESTINATION"/"$FILE_NAME
  200.         echo " Some error ocurred with the eQSL http://www.eqsl.cc/CFFileServlet/_cf_image/$URL_IMG"
  201.     fi
  202.  
  203. fi
  204. done
  205. }
  206.  
  207. # Function to remove all temporary files
  208. function clearFiles
  209. {
  210.     if [ -f "$TMP/cookie.txt" ];then
  211.         rm "$TMP/cookie.txt"
  212.     fi
  213.     if [ -f "$TMP/tmp.txt" ];then
  214.         rm "$TMP/tmp.txt"
  215.     fi
  216.     if [ -f "$TMP/log.txt" ];then
  217.         rm "$TMP/log.txt"
  218.     fi
  219. }
  220.  
  221. ##########################################################################
  222. # The script start here!
  223. #
  224. # Read user options:
  225. parseOptions "$@"
  226.  
  227. # Connect to eQSL site and save cookies:
  228. eQSLConnect
  229.  
  230. # Login to eQSL site:
  231. eQSLLogin
  232.  
  233. # Get your loogbook:
  234. getLogBook
  235.  
  236. # Start eQSL download:
  237. getQSL
  238.  
  239. # Remove all temporary files
  240. clearFiles
  241. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement