diff options
author | emkael <emkael@tlen.pl> | 2019-11-09 15:59:53 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-11-09 16:00:17 +0100 |
commit | 95a8f54093e88264ebdfbbfc57d09ca6b888788b (patch) | |
tree | 16e4530e9363e7952025d464ad4402c45b9c5a0d /scripts | |
parent | ee6a00f4af64ad679d67233e730a5fa0d308ce4b (diff) |
Dynamic player pages from JSON datafiles
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/players-compile.py | 52 |
1 files changed, 18 insertions, 34 deletions
diff --git a/scripts/players-compile.py b/scripts/players-compile.py index f4c012a..66750d1 100644 --- a/scripts/players-compile.py +++ b/scripts/players-compile.py @@ -1,4 +1,5 @@ -import copy, json, os, sys +import copy, os, sys +import simplejson as json from bs4 import BeautifulSoup as bs4 from math import ceil from pyranking.fetch import fetch_ranking @@ -6,8 +7,13 @@ from pyranking.fetch import fetch_ranking output_directory = sys.argv[1] pagesize = 100.0 +template = bs4(file('templates/player.html'), 'lxml') menu_file = sys.argv[2] menu_content = bs4(file(menu_file), 'html.parser') +menu = template.select('div.static-menu')[0] +menu.append(copy.copy(menu_content)) + +file(os.path.join(output_directory, 'index.html'), 'w').write(template.prettify().encode('utf-8')) dates = {} for date_config in json.load(file('config/dates.json')): @@ -33,20 +39,14 @@ for date in sorted(dates.keys()): pcount = 0 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() - + player['url'] = 'https://msc.com.pl/cezar/?p=21&pid=%d' % (pid) for date in dates: if date not in player['rankings']: - player['rankings'][date] = None + player['rankings'][date] = {'place': 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: + if prev is not None and prev['place'] is not None and ranking['place'] is not None: ranking['change'] = prev['place'] - ranking['place'] for field in ['gender', 'age', 'region']: if prev[field] == ranking[field]: @@ -62,42 +62,26 @@ for pid, player in players.iteritems(): 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]) base_rank_link = '../%s' % (dates[date]) - if ranking is not None and ranking['place'] > int(pagesize): - rank_link['href'] = '../%s#page:%d' % ( + if ranking['place'] is not None and ranking['place'] > int(pagesize): + ranking['href'] = '../%s#page:%d' % ( dates[date], ceil(ranking['place'] / pagesize) ) else: - rank_link['href'] = base_rank_link - if ranking is not None: - score_cell = row.select('.score span')[0] - score_cell.string = '%.2f' % (ranking['score']) - score_cell['title'] = str(ranking['score']) + ranking['href'] = base_rank_link + if ranking['place'] is not None: for field in ['region', 'age', 'gender']: - link = row.select('td.'+field+' a')[0] if ranking[field+'-place'] > int(pagesize): - link['href'] = base_rank_link + '#%s:%s;page:%d' % ( + ranking[field+'-href'] = base_rank_link + '#%s:%s;page:%d' % ( field, ranking[field], ceil(ranking[field+'-place'] / pagesize) ) else: - link['href'] = base_rank_link + '#%s:%s' % ( + ranking[field+'-href'] = base_rank_link + '#%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) - menu = template.select('div.static-menu')[0] - menu.append(copy.copy(menu_content)) + json.dump(player, file(os.path.join(output_directory, '%d.json' % pid), 'w')) - file(os.path.join(output_directory, '%d.html' % pid), 'w').write(template.prettify().encode('utf-8')) pcount += 1 sys.stdout.write("[%d/%d]\r" % (pcount, len(players))) sys.stdout.flush() |