Skip to content

Instantly share code, notes, and snippets.

@spumer
Created April 22, 2019 09:54
Show Gist options
  • Save spumer/47242d8d01bc208e569cefb5b7024ade to your computer and use it in GitHub Desktop.
Save spumer/47242d8d01bc208e569cefb5b7024ade to your computer and use it in GitHub Desktop.
GeoLite2 for HLstatsX
#!/usr/bin/env bash
# Import CSV file into MySQL table
#
# Database requisites passed trough .env file
# Please ensure .env file exists or variables set
#
# To pass options to mysqlimport use '--': -- --lock-tables
#
# Credits
# Argument parsing: https://stackoverflow.com/a/29754866
# Load up .env
set -o allexport
[[ -f .env ]] && source .env
set +o allexport
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
display_usage () { echo "Usage: $0 -f|--file <input.csv> [-c|--container <container_name>] [-- <mysqlimport options>]" 1>&2; }
if [[ $# -le 1 ]] ;then
display_usage
exit 1
fi
DBHOST=${DBHOST:-localhost}
DBPORT=${DBPORT:-3306}
DBNAME=${DBNAME:-hlstats}
DBUSER=${DBUSER:?"Please set DBUSER env"}
DBPASS=${DBPASS:?"Please set DBPASS env"}
OPTIONS=f:h
LONGOPTS=file:,container:,help
# -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
EXEC_CONTAINER="docker exec -i"
EXEC_NATIVE=exec
EXEC=$EXEC_NATIVE
CONTAINER=
CSVPATH=
CSVFILE=
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-f|--file)
CSVFILE=$2
shift 2
;;
--container)
CONTAINER="$2"
EXEC="${EXEC_CONTAINER} $CONTAINER"
shift 2
;;
--)
shift
break
;;
-h|--help)
display_usage
exit 0
;;
*)
display_usage
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 0 ]]; then
echo "[DEBUG] Extra options '"$@"' will be passed to 'mysqlimport' command"
fi
if [[ -z $CONTAINER ]] ;then
CSVPATH=${CSVFILE}
else
# in container
CSVPATH=/opt/${CSVFILE}
fi
if [[ ! -z $CONTAINER ]] ;then
echo "Copy '$CSVFILE' to ${CONTAINER}:/opt ..."
docker cp ${CSVFILE} ${CONTAINER}:/opt
fi
echo "Import csv -> table ..."
$EXEC mysqlimport -C --delete --fields-terminated-by=, --fields-enclosed-by=\" --ignore-lines=1 --replace --default-character-set=utf8 -h $DBHOST -u $DBUSER --port=$DBPORT --password=$DBPASS $DBNAME -L ${CSVPATH} $@
if [[ ! -z $CONTAINER ]] ;then
echo "Cleanup: rm ${CONTAINER}:${CSVPATH} ..."
$EXEC rm ${CSVPATH}
fi
echo "Done!"
#!/bin/sh
# HLstatsX Community Edition - Real-time player and clan rankings and statistics
# Copyleft (L) 2008-20XX Nicholas Hastings (nshastings@gmail.com)
# http://www.hlxcommunity.com
#
# HLstatsX Community Edition is a continuation of
# ELstatsNEO - Real-time player and clan rankings and statistics
# Copyleft (L) 2008-20XX Malte Bayer (steam@neo-soft.org)
# http://ovrsized.neo-soft.org/
#
# ELstatsNEO is an very improved & enhanced - so called Ultra-Humongus Edition of HLstatsX
# HLstatsX - Real-time player and clan rankings and statistics for Half-Life 2
# http://www.hlstatsx.com/
# Copyright (C) 2005-2007 Tobias Oetzel (Tobi@hlstatsx.com)
#
# HLstatsX is an enhanced version of HLstats made by Simon Garner
# HLstats - Real-time player and clan rankings and statistics for Half-Life
# http://sourceforge.net/projects/hlstats/
# Copyright (C) 2001 Simon Garner
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# For support and installation notes visit http://www.hlxcommunity.com
set -o pipefail
# Configure the variables below
# Set this value to 1 if you are running Gentoo linux, or any other linux distro where the "cal" command outputs not Sunday as the first day in every row!
LINUX_OTHER="0"
#
# Nothing to change below here.
#
# database is updated every first tuesday of any month, so download it with that specific date and import it
TODAY_MONTH=$( date +%m )
TODAY_YEAR=$( date +%Y )
if [[ ${LINUX_OTHER} == "1" ]]
then CAL_COMMAND="cal -s"
else CAL_COMMAND="cal"
fi
FIRST_TUESDAY_MONTH=$( $CAL_COMMAND $TODAY_MONTH $TODAY_YEAR |
awk '
NR == 1 { next }
NR == 2 { next }
NF <= 4 { next }
NF == 5 { print $1 ; exit }
NF == 6 { print $2 ; exit }
NF == 7 { print $3 ; exit }
' )
DATE=""$TODAY_YEAR""$TODAY_MONTH"0"$FIRST_TUESDAY_MONTH""
DIR="GeoLite2-City-CSV_$DATE"
FILE="GeoLite2-City-CSV_$DATE.zip"
[[ -f ${FILE} ]] || wget http://geolite.maxmind.com/download/geoip/database/${FILE} || exit 1
if [[ ! -f ./geoip2-csv-converter ]]; then
echo "Can't find './geoip2-csv-converter' binary."
echo "Please install: https://github.com/maxmind/geoip2-csv-converter/releases"
exit 2
fi
unzip -o ${FILE} || exit 1
mv $DIR/GeoLite2-City-Locations-en.csv geoLite2_City_Locations.csv
./geoip2-csv-converter -block-file $DIR/GeoLite2-City-Blocks-IPv4.csv -output-file geoLite2_City_Blocks.csv -include-integer-range
# Cleanup
rm $FILE
rm -r $DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment