thorpedosg

nJptcPaq

Aug 6th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/bin/bash
  2. # Download MTGO Vintage results from magic.wizards.com
  3.  
  4. # Get script's directory
  5. ROOT=$(dirname $(readlink -f ${0}))
  6.  
  7. # Store the date of the most recent results in the script's root
  8. DATE_FILE="${ROOT}/vintage-last-checked"
  9.  
  10. # If ./last-checked exists, get the date of the last results. Otherwise,
  11. # don't restrict the search window. By default, this appears to return the
  12. # most recent 6 results.
  13. FROM_DATE=""
  14. [ -f ${DATE_FILE} ] && FROM_DATE=$(egrep "^[0-9/]{10}$" ${DATE_FILE})
  15.  
  16. # URL definitions. Use magic.wizards.com for both searches and event links.
  17. URL_BASE="https://magic.wizards.com/en/"
  18. SEARCH_PATH="section-articles-see-more-ajax?l=en&f=9041&search-result-theme=&fromDate=${FROM_DATE}&word=Vintage"
  19. RESULT_PATH="articles/archive/mtgo-standings"
  20.  
  21. # Query the recent tournament results. Clean up the output enough to extract
  22. # links. Sort them chronologically, ascending.
  23. #
  24. # Note: We must reverse the results manually intead of using the website's
  25. # search function because reversing the sort in the curl request returns results
  26. # from ~2014 if no fromDate is defined.
  27. SEARCH=$(curl -s "${URL_BASE}${SEARCH_PATH}" \
  28. | sed -r "s/\\\n/\n/g" \
  29. | egrep -o "href.*$" \
  30. | sed -r "s/^.*mtgo-standings(.+)\\.*$/\1/g" \
  31. | awk -F\\ '{print $2}' \
  32. | tr '\n' ' ' \
  33. | tac -rs' ')
  34.  
  35. # Loop over the results. Extract the date from the end for later use, then
  36. # format the result as a link and print it.
  37. for S in ${SEARCH}; do
  38. D=$(echo "${S}" | egrep -o "[0-9-]{10}$")
  39.  
  40. # Clean up the result: strip the leading '/', convert to proper case,
  41. # and replace dashes with spaces.
  42. T=$(echo "${S:0:-10}" | tr -d '/' | tr '-' ' ' | sed 's/[^ ]\+/\L\u&/g')
  43. echo "${T}${D}"
  44. done
  45.  
  46. # Reformat the date of the last entry and save it to ${DATE_FILE}
  47. [ ! -z ${D} ] && date -d "${D}+1 day" "+%m/%d/%Y" > ${DATE_FILE} 2>/dev/null
Add Comment
Please, Sign In to add comment