#!/bin/sh
# Parameter: a telepules neve
# A script visszaadja az orszag Wikidata-kodjat es a telepules koordinatait.


if [ -z "$1" ]
then	echo Hívas: `basename $0` telepulesnev
	exit 1
fi

WGETLOG=$0.wget
TEMP=$0.$$

lekerd() {
rm -f $TEMP
wget -t1 -T10 "$1" -a $WGETLOG -O $TEMP
}

rm -f $WGETLOG $TEMP

#lekerd "https://www.wikidata.org/w/api.php?action=wbgetentities&sites=huwiki&props=sitelinks&sitefilter=huwiki&format=json&titles=$1"
#if grep '^{"entities":{"-1":' $TEMP >/dev/null ; then exit 2 ; fi	# nem talalt
#WDATA=`logcut -d '"' -f 4 $TEMP`

WDATA=Q81581	# Szeged. Teszt.
#lekerd "https://www.wikidata.org/w/api.php?action=wbgetclaims&entity=$WDATA&format=json&property=P17"
#ORSZAG=`sed 's/^{"claims":{"P17":\[{"mainsnak":{"snaktype":"value","property":"P17","datavalue":{"value":{"entity-type":"item","numeric-id":[0-9]*,"id":"\([^"]*\)"}.*$/\1/' $TEMP`

ORSZAG=Q28	# Magyarorszag. Teszt.


lekerd "https://www.wikidata.org/w/api.php?action=wbgetclaims&entity=$WDATA&format=json&property=P625"
sed 's/^{"claims":{"P625":\[{"mainsnak":{"snaktype":"value","property":"P625","datavalue":{"value":{\([^}]*\)}.*$/\1/' $TEMP
