#!/bin/bash # See usage() for detailed description # # Depends on the `mktemp` utility from debianutils, available on most # distributions, as well as `grep`, `cut`, `tr` and >=bash-2.05 for C-style # math. # # Author: Donnie Berkholz # These should probably be configurable with command-line parameters, # but I don't care that much. # How many spaces are in a tab tab_size="8" # How many tabs worth of space the expansion name gets expansion_size="4" usage() { echo echo " gatherer CARD" echo echo "Get the rarity and expansion of a Magic: The Gathering card." echo echo "Example use to get multiple cards, from a list of cards in a file:" echo "while read card; do gatherer ${card}; done < card_list.txt > rarity.txt" echo echo "Generate card_list.txt from your *.dec files like this:" echo "grep -v -e '^/' -e '^$' *.dec | cut -d: -f2- \\" echo " | sed -e \"s~SB: ~~g\" -e \"s: : :g\" | cut -d' ' -f2- | sort | uniq -i \\" echo " > card_list.txt" echo echo "Example use to run on a single *.dec decklist:" echo "DECK=\"decklist.dec\"" echo "grep -v -e '^/' -e '^$' \${DECK} | sed -e \"s~SB: ~~g\" -e \"s: : :g\" \\" echo " | cut -d' ' -f2- | sort | uniq -i | while read card; \\" echo " do gatherer \"\${card}\" ; done" echo echo "Returns tab-formatted output of rarity, expansion and card name" echo "and prints invalid cards to stderr. Expansion returned is the first expansion" echo "in Gatherer HTML. I welcome contributions and logical formatting for showing" echo "multiple expansions." exit 1 } setup_card() { orig_card="${@}" # Replace all spaces w/ underscores card=${orig_card// /_} # Set up secure temp file tmpfile=$(mktemp /tmp/mtg.XXXXXX) # Get card info wget -c http://gatherer.wizards.com/gathererlookup.asp?name=${card} \ -O ${tmpfile} &> /dev/null } get_expansion() { local file=${1} # Get expansion name out of html expansion=$(grep -i 'td width="99%"' ${file} | cut -d'>' -f3 \ | cut -d'<' -f1) # Calculate number of tabs following expansion name tab_number=$(( ${expansion_size} - ${#expansion} / ${tab_size} )) echo -n "${expansion}" # Add tabs for (( ; tab_number > 0; tab_number-- )); do echo -ne "\t" done } get_rarity() { local file=${1} # Check for the rarities for rarity in rare uncommon common; do grep -q -e " ${rarity}" ${file} eval ${rarity}=\$? if [[ ${!rarity} -eq 0 ]]; then found=1 # Handle cards with changing rarities rarities=${rarities}${rarity:0:1} fi done if [[ ${found} -eq 1 ]]; then echo -ne "$(echo ${rarities} | tr [:lower:] [:upper:])\t" fi } if [[ $# -ne 1 ]]; then usage fi setup_card "${@}" get_rarity ${tmpfile} if [[ ${found} -eq 1 ]]; then get_expansion ${tmpfile} echo "${orig_card}" fi rm ${tmpfile} if [[ ${found} -eq 1 ]]; then exit 0 else echo "ERROR: ${orig_card} not found!" > /dev/stderr exit 1 fi