From 4060fa99c23bca17895029ae1ffad24d0d024269 Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 7 Sep 2017 02:10:36 +0200 Subject: Moving *.py scripts to separate directory, cleaning things up --- scripts/pyranking/__init__.py | 0 scripts/pyranking/db.py | 13 +++++++++++++ scripts/pyranking/fetch.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 scripts/pyranking/__init__.py create mode 100644 scripts/pyranking/db.py create mode 100644 scripts/pyranking/fetch.py (limited to 'scripts/pyranking') diff --git a/scripts/pyranking/__init__.py b/scripts/pyranking/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/pyranking/db.py b/scripts/pyranking/db.py new file mode 100644 index 0000000..16595d3 --- /dev/null +++ b/scripts/pyranking/db.py @@ -0,0 +1,13 @@ +import json +import mysql.connector + +settings = json.load(file('config/db.json')) + +connection = mysql.connector.connect( + user=settings['user'], + password=settings['pass'], + host=settings['host'], + port=settings['port'], + database=settings['db'] +) +cursor = connection.cursor(dictionary=True, buffered=True) diff --git a/scripts/pyranking/fetch.py b/scripts/pyranking/fetch.py new file mode 100644 index 0000000..0d301d4 --- /dev/null +++ b/scripts/pyranking/fetch.py @@ -0,0 +1,42 @@ +from pyranking.db import cursor + +def fetch_ranking(date, assoc=False): + sql = '''SELECT + rankings.place, + rankings.pid, + CONCAT(players.name, " ", players.surname) player, + players.rank, + rankings.region, players.club, + REPLACE(rankings.flags, "K", "") age, + IF(rankings.flags LIKE 'K%', "K", "") gender, + rankings.score +FROM rankings +JOIN players + ON players.id = rankings.pid +WHERE rankings.date = %(date)s +ORDER BY rankings.place + ''' + cursor.execute(sql, {'date': date}) + ranks = { + 'gender': {}, + 'age': {}, + 'region': {} + } + ranking = cursor.fetchall() + for row in ranking: + if row['gender'] == '': + row['gender'] = 'M' + for category in ['gender', 'age', 'region']: + if row[category] not in ranks[category]: + ranks[category][row[category]] = 0 + ranks[category][row[category]] += 1 + row[category + '-place'] = ranks[category][row[category]] + for category in ['place', 'gender', 'age', 'region']: + row[category + '-change'] = 'N' + row[category + '-change-class'] = 'info' + if assoc: + result = {} + for row in ranking: + result[row['pid']] = row + return result + return ranking -- cgit v1.2.3