All pastes #347758 Raw Edit

Spliffster

public shellscript v1 · immutable
·published 2007-02-09 17:41 UTC
rendered paste body
#!/bin/bash# gamespy-history logger.## query stats from gamespy and store it in a sqlite db on a daily # basis per user.## usage: # $ gamespy-history.sh <username|player id># # NOTE:#  - if you pass a username in, then a search for the <player id> (pid) is done.#    if the search result contains 1 pid it is used for further queries as #    unique id of this player. if the result contains more than 1 player id, a#    list of pids and usernames is printed and the script exits #    (with exit code 2).#  - you need to have an anonymous proxy running or available. gamespy will #    shut you down after a couple of requests (dns lookup will fail)## PROGRAMMS# the following list of programms is needed by this script, you may have to # change paths in config.sh#  - a bash #! shebang#  - netcat#  - awk#  - sqlite#  - sed#  - grep#  - egrep#  - date (gnu version)## Exit codes:#   0 success#   1 connection error#   2 multiple player names matched## Files:#   - config.sh; is created if it does not exist#   - data.db; the sqlite database, path can be configured in config.sh## (c) 2005 Simon Wunderlin under the gpl, http://www.gnu.org/copyleftsource functions.sh## we need to know where we aredir=`dirname $0`;if [[ ! -z $dir ]]; then	dir="$dir/";fi## read configif [[ ! -f "${dir}"config.sh ]]; then	createConfig;fi. "${dir}"config.shdbfile="${dir}${dbfile}"## check input, we need one parameterif [[ -z $1 ]]; then	echo "usage: $0 <username|player id>";	echo "";	echo "ERROR: Please enter a nick name or a player id!";	exit 1;finick="$1";pid=`echo "$nick" | $egrep '^[0-9]+$'`;## make sure we have a dbif [[ ! -f $dbfile ]]; then	createDatabase;fi## check if the user input is nummeric, if not, we need to get the ## player id firstif [[ "x`echo $pid | $sed 's/[0-9]*//'`" != "x" || -z $pid ]]; then	#echo -en $nick": ";		pid=`getPid $nick`;	#echo "pid: $pid";	error=`echo "$pid" | $grep "\n"`;		if [[ -n $error ]]; then		echo "Error:";		echo "$pid";		exit 1;	fififunction main {	echo -en "Fetching for ($pid):";		# $info: requests all (known to me) options of a player which can be 	# retrived, no way to figure the active players ip as it turns out!	# maybe this must be done via the gamespy 1|2|3 udp protocol, have to	# find info on that however.	request="${CONN_PREFIX}getplayerinfo.aspx?pid=${pid}&info=${info}&debug=tx";	request="${request} ${CONN_SUFFIX}";	if [[ -z $proxy_host ]]; then		result=`echo -e -n "$request" | $netcat $queryhost $queryport`;	else		request=`echo -en "$request"`;		request=`proxy "$request"`;		#echo "$request";		result=`echo -e -n "$request\n\n" | $netcat $proxy_host $proxy_port`;	fi		# todo, check if we got a valid response, else exit.	# extract relevant data into comma seperated string	fields=`echo "$result" | $sed 's/\t/,/g' | $grep "^H,pid" | $sed 's/-/_/g' | $sed 's/^H,//'`;	data=`echo "$result" | $sed 's/\t/,/g' | $grep "^D,$pid" | $sed 's/^D,//'`;	## if updating fails, all records of today are purged and isnert must be run	mustinsert=1;	## check if we have a record for today	last_sql="SELECT count(hid) FROM history WHERE pid=${pid} AND date=$date";	numRecs=`$sqlite $dbfile "$last_sql"`;	## if we have no record fro today insert else update the record. if more than 	## one record for today are available, delete all and insert.	if [[ $numRecs -gt 0 ]]; then		echo -en " updating record";				# if there is 1 record update, else delete em all and fall back to insert		if [[ $numRecs -eq 1 ]]; then			updRecord "$fields" "$data";			mustinsert=0;		else			echo -en ", multiple records->purging";			delRecords;		fi	fi	if [[ $mustinsert -eq 1 ]]; then 		echo -en " inserting new record";		insRecord "$fields" "$data";	fi		echo ", done";}main;exit 0;