summaryrefslogtreecommitdiff
path: root/scripts/rankings-editions.py
blob: bdbe46fe19312022c079e00aef7b4aaa27fe5dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import copy, json, sys
from collections import OrderedDict

from bs4 import BeautifulSoup as bs4

dates_config = json.load(open('config/dates.json'))
output_file = bs4(open(sys.argv[1]), 'lxml')

editions = OrderedDict()
for date_config in dates_config:
    year = date_config['name'].split(' ')[1]
    if year not in editions:
        editions[year] = []
    editions[year].append((
        '%s (%s)' % (date_config['name'].split(' ')[0], date_config['index']),
        date_config['url'],
        date_config['date']
    ))

template = bs4(open('templates/ranking.html'), 'lxml')

date_group = template.select('#editions')[0].extract()
year_group = date_group.select('div[role="group"]')[0].extract()
ranking_link = year_group.select('.btn-default')[0].extract()
for year, dates in editions.items():
    group = copy.copy(year_group)
    group.select('.year')[0].string = str(year)
    for date in dates[::-1]:
        link = copy.copy(ranking_link)
        link.string = date[0]
        link['href'] = date[1]
        link['datetime'] = date[2]
        group.append(link)
    date_group.insert(0, group)

output_file.select('#editions')[0].replace_with(date_group)
open(sys.argv[1], 'w').write(output_file.prettify())