summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2017-04-06 01:36:51 +0200
committeremkael <emkael@tlen.pl>2017-04-06 01:36:51 +0200
commite106531c0d74dccb20794ab9226bbde52fc4129e (patch)
tree60ca747b86b6a19e74c6a3e2a26b734c870a9cd3
parentcce7440974bbf7999f160d07d99cf22265ceebd5 (diff)
Player history generation
-rw-r--r--http/res/ranking.css17
-rw-r--r--players.py76
-rw-r--r--templates/player.html74
3 files changed, 167 insertions, 0 deletions
diff --git a/http/res/ranking.css b/http/res/ranking.css
index da8b586..316aa49 100644
--- a/http/res/ranking.css
+++ b/http/res/ranking.css
@@ -8,6 +8,9 @@
border-top: solid 1px #ddd; border-bottom: solid 1px #ddd;
opacity: 0.1;
}
+.table.history {
+ opacity: 1;
+}
.table .label { display: inline-block; width: 40px }
.table, .table .club,
.table .region, .table .age,
@@ -21,3 +24,17 @@
border-right: solid 1px #ddd
}
.table > tbody > tr { cursor: pointer }
+.table-bordered > tbody .place,
+.table-bordered > tbody .age-place,
+.table-bordered > tbody .gender-place,
+.table-bordered > tbody .region-place {
+ border-right: none;
+}
+.table-bordered > tbody .place-change,
+.table-bordered > tbody .age-place-change,
+.table-bordered > tbody .gender-place-change,
+.table-bordered > tbody .region-place-change {
+ border-left: none;
+}
+.table > thead th { text-align: center }
+.table.history > tbody > tr { cursor: auto }
diff --git a/players.py b/players.py
new file mode 100644
index 0000000..a9ff20a
--- /dev/null
+++ b/players.py
@@ -0,0 +1,76 @@
+import copy, json, os, sys
+from bs4 import BeautifulSoup as bs4
+from pyranking.fetch import fetch_ranking
+
+output_directory = sys.argv[1]
+config_file = sys.argv[2] if len(sys.argv) > 2 else 'dates.json'
+
+dates = json.load(file(config_file))
+
+players = {}
+
+for date in sorted(dates.keys()):
+ for player, ranking in fetch_ranking(date, True).iteritems():
+ if player not in players:
+ players[player] = {'rankings':{}}
+ players[player]['name'] = ranking['player']
+ players[player]['club'] = ranking['club']
+ players[player]['rankings'][date] = {'change': 'N','change-class':'primary'}
+ for field in ['place', 'score']:
+ players[player]['rankings'][date][field] = ranking[field]
+ for field in ['gender', 'age', 'region']:
+ players[player]['rankings'][date][field] = ranking[field]
+ players[player]['rankings'][date][field + '-place'] = ranking[field + '-place']
+ players[player]['rankings'][date][field + '-change'] = 'N'
+ players[player]['rankings'][date][field + '-change-class'] = 'primary'
+
+for pid, player in players.iteritems():
+
+ template = bs4(file('templates/player.html'), 'lxml')
+ template.select('h2.name')[0].insert(0, player['name'])
+ template.select('h3.club')[0].string = player['club']
+ template.select('a.pid-link')[0]['href'] = 'https://msc.com.pl/cezar/?p=21&pid=%d' % (pid)
+
+ missing_row = template.select('tr.missing')[0].extract()
+ normal_row = template.select('tr.normal')[0].extract()
+
+ for date in dates:
+ if date not in player['rankings']:
+ player['rankings'][date] = None
+ prev = None
+ for date, ranking in sorted(player['rankings'].iteritems(), lambda x,y: cmp(x[0], y[0])):
+ if prev is not None and ranking is not None:
+ ranking['change'] = prev['place'] - ranking['place']
+ for field in ['gender', 'age', 'region']:
+ if prev[field] == ranking[field]:
+ ranking[field + '-change'] = prev[field + '-place'] - ranking[field + '-place']
+ for field in ['', 'gender-', 'age-', 'region-']:
+ if ranking[field+'change'] == 0:
+ ranking[field+'change'] = '='
+ ranking[field+'change-class'] = 'default'
+ elif ranking[field+'change'] == 'N':
+ ranking[field+'change-class'] = 'primary'
+ else:
+ ranking[field+'change-class'] = 'success' if ranking[field+'change'] > 0 else 'danger'
+ ranking[field+'change'] = '%+d' % (ranking[field+'change'])
+ prev = ranking
+
+ row = copy.copy(missing_row) if ranking is None else copy.copy(normal_row)
+ rank_link = row.find('td').a
+ rank_link.string = '.'.join(date.split('-')[::-1])
+ rank_link['href'] = '../%s' % (dates[date])
+ if ranking is not None:
+ row.select('.score')[0].string = str(ranking['score'])
+ for field in ['region', 'age', 'gender']:
+ link = row.select('td.'+field+' a')[0]
+ link['href'] = rank_link['href'] + '#%s:%s' % (
+ field, ranking[field]
+ )
+ link.string = ranking[field] if len(ranking[field]) else '-'
+ for field in ['', 'region-', 'age-', 'gender-']:
+ row.select('td.'+field+'place')[0].string = '%d.' % (ranking[field+'place'])
+ change_label = row.select('td.'+field+'place-change span.label')[0]
+ change_label.string = ranking[field+'change']
+ change_label['class'] = change_label['class'] + ['label-'+ranking[field+'change-class']]
+ template.select('table.table tbody')[0].insert(0, row)
+ file(os.path.join(output_directory, '%d.html' % pid), 'w').write(template.prettify().encode('utf-8'))
diff --git a/templates/player.html b/templates/player.html
new file mode 100644
index 0000000..0e7979d
--- /dev/null
+++ b/templates/player.html
@@ -0,0 +1,74 @@
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>Oficjalny Ranking Brydżowy PZBS</title>
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
+ <link rel="stylesheet" href="../res/ranking.css" />
+ </head>
+ <body>
+ <div class="container">
+ <div class="page-header">
+ <h1>
+ Oficjalny Ranking Brydżowy PZBS
+ <small></small>
+ </h1>
+ </div>
+ <div class="panel panel-default filters">
+ <div class="panel-body">
+ <h2 class="name">
+ <a href="" target="_blank" class="pid-link">
+ <span class="glyphicon glyphicon-new-window"></span>
+ </a>
+ </h2>
+ <h3 class="club"></h3>
+ </div>
+ </div>
+ <table class="history table table-striped table-hover table-bordered">
+ <thead>
+ <tr class="text-center">
+ <th>Data</th>
+ <th colspan="2">Miejsce</th>
+ <th class="region" colspan="3">Okręg</th>
+ <th class="gender" colspan="3">Płeć</th>
+ <th class="age" colspan="3">Kat. wiekowa</th>
+ <th>Wynik</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr class="missing">
+ <td class="date text-right">
+ <a href=""></a>
+ </td>
+ <td colspan="12" class="text-center">Brak notowania.</td>
+ </tr>
+ <tr class="normal">
+ <td class="date text-right">
+ <a href=""></a>
+ </td>
+ <td class="place text-right"></td>
+ <td class="place-change"><span class="label"></span></td>
+ <td class="region text-center">
+ <a href=""></a>
+ </td>
+ <td class="region-place text-right"></td>
+ <td class="region-place-change"><span class="label"></span></td>
+ <td class="gender text-center">
+ <a href=""></a>
+ </td>
+ <td class="gender-place text-right"></td>
+ <td class="gender-place-change"><span class="label"></span></td>
+ <td class="age text-center">
+ <a href=""></a>
+ </td>
+ <td class="age-place text-right"></td>
+ <td class="age-place-change"><span class="label"></span></td>
+ <td class="score text-right"></td>
+ </tr>
+ </tbody>
+ </table>
+ <div class="well well-sm">
+ &copy; 2017 - dla <a href="http://pzbs.pl">PZBS</a> zrobił <a href="https://emkael.info">mkl.</a> Też sobie <a href="https://github.com/emkael/pzbs-ranking">zrób.</a>
+ </div>
+ </div>
+ </body>
+</html>