diff options
author | emkael <emkael@tlen.pl> | 2017-04-06 01:33:22 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-04-06 01:33:22 +0200 |
commit | cce7440974bbf7999f160d07d99cf22265ceebd5 (patch) | |
tree | c67d875ee3f6bd5b8823cde6b421ea23ebb7bb96 | |
parent | 925e385cbb04977c3e8f5bfb0a3c6c8edfad48ac (diff) |
Refactoring re-usable code
-rw-r--r-- | pyranking/__init__.py | 0 | ||||
-rw-r--r-- | pyranking/__init__.pyc | bin | 0 -> 155 bytes | |||
-rw-r--r-- | pyranking/db.py | 13 | ||||
-rw-r--r-- | pyranking/db.pyc | bin | 0 -> 550 bytes | |||
-rw-r--r-- | pyranking/fetch.py | 42 | ||||
-rw-r--r-- | pyranking/fetch.pyc | bin | 0 -> 1401 bytes | |||
-rw-r--r-- | ranking.py | 55 |
7 files changed, 57 insertions, 53 deletions
diff --git a/pyranking/__init__.py b/pyranking/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pyranking/__init__.py diff --git a/pyranking/__init__.pyc b/pyranking/__init__.pyc Binary files differnew file mode 100644 index 0000000..549f38c --- /dev/null +++ b/pyranking/__init__.pyc diff --git a/pyranking/db.py b/pyranking/db.py new file mode 100644 index 0000000..d28147d --- /dev/null +++ b/pyranking/db.py @@ -0,0 +1,13 @@ +import json +import mysql.connector + +settings = json.load(file('config.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/pyranking/db.pyc b/pyranking/db.pyc Binary files differnew file mode 100644 index 0000000..e956b24 --- /dev/null +++ b/pyranking/db.pyc diff --git a/pyranking/fetch.py b/pyranking/fetch.py new file mode 100644 index 0000000..0d301d4 --- /dev/null +++ b/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 diff --git a/pyranking/fetch.pyc b/pyranking/fetch.pyc Binary files differnew file mode 100644 index 0000000..825b456 --- /dev/null +++ b/pyranking/fetch.pyc @@ -1,58 +1,7 @@ -import copy, json, sys -import mysql.connector +import copy, sys from bs4 import BeautifulSoup as bs4 -settings = json.load(file('config.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) - -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 +from pyranking.fetch import fetch_ranking ranking = fetch_ranking(sys.argv[1]) old_ranking = fetch_ranking(sys.argv[2], True) if len(sys.argv) > 2 else {} |