summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2017-09-07 00:50:15 +0200
committeremkael <emkael@tlen.pl>2017-09-07 00:50:15 +0200
commit9123253d8785a12ceb30df5f0bd67b27deda1e10 (patch)
treeb564229ab58473c302dbfba59630e0cbcba2d615
parentde19dcc91dd149b542ed4946fae093ca30fafb5e (diff)
Generating ranking table data in separate JSON files, rather than inline HTML
-rw-r--r--Makefile5
-rwxr-xr-xbin/build-datafiles.sh11
-rw-r--r--datafile.py30
-rw-r--r--ranking.py51
4 files changed, 48 insertions, 49 deletions
diff --git a/Makefile b/Makefile
index bf4e956..8c952e6 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,10 @@ targetfiles := $(shell bin/target-ranking-files.sh config/dates.json)
tmpfiles := $(patsubst %.html,http/%.html.tmp,$(targetfiles))
rankfiles := $(patsubst %.html,http/%.html.ed,$(targetfiles))
-rankings: tables editions json
+rankings: datafiles tables editions json
+
+datafiles:
+ bin/build-datafiles.sh config/dates.json http/data
tables:
bin/build-rankings.sh config/dates.json http
diff --git a/bin/build-datafiles.sh b/bin/build-datafiles.sh
new file mode 100755
index 0000000..bde7bc1
--- /dev/null
+++ b/bin/build-datafiles.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+CONFIG=$1
+DIR=$2
+PREV_DATE=""
+cat $CONFIG |
+ jq -r '.[] | .date' |
+ while read DATE
+ do
+ python datafile.py $DATE $PREV_DATE > "$DIR/$DATE.json"
+ PREV_DATE=$DATE
+ done
diff --git a/datafile.py b/datafile.py
new file mode 100644
index 0000000..5cee451
--- /dev/null
+++ b/datafile.py
@@ -0,0 +1,30 @@
+import json, sys
+from decimal import Decimal
+
+from pyranking.fetch import fetch_ranking
+
+ranking_date = sys.argv[1]
+ranking = fetch_ranking(ranking_date)
+old_ranking = fetch_ranking(sys.argv[2], True) if len(sys.argv) > 2 else {}
+
+for row in ranking:
+ if row['pid'] in old_ranking:
+ row['place-change'] = old_ranking[row['pid']]['place'] - row['place']
+ row['place-change-class'] = 'success' if row['place-change'] > 0 else 'danger'
+ row['place-change'] = '%+d' % (row['place-change'])
+ if row['place-change'] == '+0':
+ row['place-change'] = '='
+ row['place-change-class'] = 'default'
+ for category in ['gender', 'age', 'region']:
+ if row[category] == old_ranking[row['pid']][category]:
+ row[category + '-change'] = old_ranking[row['pid']][category + '-place'] - row[category + '-place']
+ row[category + '-change-class'] = 'success' if row[category + '-change'] > 0 else 'danger'
+ row[category + '-change'] = '%+d' % (row[category + '-change'])
+ if row[category + '-change'] == '+0':
+ row[category + '-change'] = '='
+ row[category + '-change-class'] = 'default'
+ for field in row:
+ if isinstance(row[field], Decimal):
+ row[field] = float(row[field])
+
+print json.dumps(ranking)
diff --git a/ranking.py b/ranking.py
index 96bc3f8..4a16867 100644
--- a/ranking.py
+++ b/ranking.py
@@ -1,61 +1,16 @@
-import copy, os, sys
+import os, sys
from bs4 import BeautifulSoup as bs4
-from pyranking.fetch import fetch_ranking
-
ranking_date = sys.argv[3]
subtitle = 'notowanie %s (%s), stan na %s' % (
sys.argv[1], sys.argv[2], '.'.join(ranking_date.split('-')[::-1])
)
-ranking = fetch_ranking(ranking_date)
-old_ranking = fetch_ranking(sys.argv[4], True) if len(sys.argv) > 4 else {}
-
-for row in ranking:
- if row['pid'] in old_ranking:
- row['place-change'] = old_ranking[row['pid']]['place'] - row['place']
- row['place-change-class'] = 'success' if row['place-change'] > 0 else 'danger'
- row['place-change'] = '%+d' % (row['place-change'])
- if row['place-change'] == '+0':
- row['place-change'] = '='
- row['place-change-class'] = 'default'
- for category in ['gender', 'age', 'region']:
- if row[category] == old_ranking[row['pid']][category]:
- row[category + '-change'] = old_ranking[row['pid']][category + '-place'] - row[category + '-place']
- row[category + '-change-class'] = 'success' if row[category + '-change'] > 0 else 'danger'
- row[category + '-change'] = '%+d' % (row[category + '-change'])
- if row[category + '-change'] == '+0':
- row[category + '-change'] = '='
- row[category + '-change-class'] = 'default'
table = bs4(file('templates/ranking.html'), 'lxml')
-table_body = table.select('table.data-table tbody')[0]
-table_row = table_body.select('tr')[0].extract()
-
table.select('.page-header h2 small')[0].string = subtitle
-for row in ranking:
- new_row = copy.copy(table_row)
- new_row.select('td.pid')[0].string = str(row['pid'])
- new_row.select('td.pidlink a')[0]['href'] = 'https://msc.com.pl/cezar/?p=21&pid=%d' % (row['pid'])
- new_row.select('td.name')[0].string = row['player']
- new_row.select('td.club')[0].string = row['club']
- for category in ['gender', 'age', 'region']:
- new_row.select('td.' + category)[0].string = row[category]
- new_row.select('td.' + category + '-place .rank')[0].string = str(row[category + '-place'])
- badge = new_row.select('td.' + category + '-place .change')[0]
- badge.string = row[category + '-change']
- badge['class'] = badge['class'] + ['label-' + row[category + '-change-class']]
- score_cell = new_row.select('td.ranking span')[0]
- score_cell['title'] = str(row['score'])
- score_cell.string = '%.2f' % (row['score'])
- new_row.select('td.place .rank')[0].string = str(row['place'])
- badge = new_row.select('td.place .change')[0]
- badge.string = row['place-change']
- badge['class'] = badge['class'] + ['label-' + row['place-change-class']]
- for category in ['gender', 'age', 'region']:
- if row[category + '-place'] == 1:
- new_row['class'] = new_row.get('class', []) + ['info']
- table_body.append(new_row)
+table.select('table.data-table')[0]['data-ranking'] = 'data/%s.json' % (
+ ranking_date)
script_src = table.select('script[src="res/ranking.js"]')[0]
script_src['src'] = '%s?%d' % ('res/ranking.js', os.path.getmtime('http/res/ranking.js'))