Skip to content

Instantly share code, notes, and snippets.

@matyapiro31
Last active July 31, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matyapiro31/7b1c7a29b9c1e92de2f8c0fb45ce5966 to your computer and use it in GitHub Desktop.
Save matyapiro31/7b1c7a29b9c1e92de2f8c0fb45ce5966 to your computer and use it in GitHub Desktop.
Wikipediaから国旗を取得する ref: http://qiita.com/matyapiro31/items/ba062c7f7d545929fa28
<img alt="シエラレオネの国旗"
src="//upload.wikimedia.org/wikipedia/commons/thumb/
1/17/Flag_of_Sierra_Leone.svg/
125px-Flag_of_Sierra_Leone.svg.png"
...
>
echo "cat /html/body/div[3]/div[3]/div[4]/dl[1]/dd/table/tr[1]/th[1]/a/img/@src" \
|xmllint --shell $URL
#!/bin/bash
function fix_name() {
if [ ${1} = "アルメニア共和国" ];then
echo アルメニア
elif [ ${1} = "オランダ王国" ];then
echo オランダ
elif [ ${1} = "ギリシャ共和国" ];then
echo ギリシャ
elif [ ${1} = "ジョージア" ];then
echo グルジア
elif [ ${1} = "スロバキア共和国" ];then
echo スロバキア
elif [ ${1} = "デンマーク王国" ];then
echo デンマーク
elif [ ${1} = "シーランド" ];then
echo シーランド公国
fi
}
function get_flag() {
country="${1}"
if [ ! -z $(fix_name "${country}") ];then
country=$(fix_name "${country}")
fi
if [ ${LANG:0:2} = "en" -o $LANG = 'C' ];then
LNG="en"
dir="/html/body/div[3]/div[3]/div[4]/table[1]/tr[2]/td/table/tr[1]/td[1]/a/img/@src"
elif [ ${LANG:0:2} = "ja" ];then
LNG="ja"
dir="/html/body/div[3]/div[3]/div[4]/dl[1]/dd/table/tr[1]/th[1]/a/img/@src"
elif [ ${LANG:0:2} = "zh" ];then
LNG="zh"
dir="/html/body/div[3]/div[3]/div[4]/table[1]/tr[3]/td/table/tr[1]/td[1]/a/img/@src"
else
echo "Your language is not supported." >&2
fi
wget -qO- https://${LNG}.wikipedia.org/wiki/"${country}" > /tmp/country.tmp
array=(`echo "cat ${dir}"|xmllint --shell /tmp/country.tmp|tail -2|head -1|sed -e 's/\// /g'`)
# rm /tmp/country.tmp
log=$(LANG=C wget https://upload.wikimedia.org/wikipedia/commons/${array[5]}/${array[6]}/${array[7]} 2>&1)
fname=( $(echo "$log"|tail -1) )
if [ "${fname[5]}" != "Found." ];then
if [ ${2} != "true" ];then
flag=$(echo "${fname[5]:1:-1}"|sed -e "s/\\\'/\'/g")
gimp "${flag}" & >/dev/null
else
echo Done.
fi
else
echo Faild to get flag of ${country}.
fi
}
#option detection.
if [ ${#@} = 0 ];then
echo "getcountryflag [COUNTRY] [OPTION]
-i : interpret mode.
-d: download only.
-h: show this help."
exit 0
fi
while getopts "idh" opts
do
if [ ${opts} = "i" ];then
std_in="true"
elif [ ${opts} = "d" ];then
download_only="true"
elif [ ${opts} = "h" ];then
echo "getcountryflag [COUNTRY] [OPTION]
-i : interpret mode.
-d: download only.
-h: show this help."
exit 0
fi
done
while [ ${std_in:-false} != "true" -a -n "${1}" ]
do
if [ ${1:0:1} != "-" ];then
get_flag "${1}" ${download_only:-false}
fi
shift
done
while [ ${std_in:-false} = "true" ]
do
printf '> '
read -r country
if [ "${country}" = "exit" -o "${country}" = "quit" ];then
exit 0
fi
if [ -n "${country}" ];then
get_flag "${country}" ${download_only:-false}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment