-
Notifications
You must be signed in to change notification settings - Fork 7
/
darwin_create_webpage.py
executable file
·244 lines (204 loc) · 6.92 KB
/
darwin_create_webpage.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
import json
import codecs
import argparse
from slugify import slugify
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
#####################################################################
def str2filename(string):
filename = re.sub(r'[<>:"/\|?*]', '-', string)
filename = filename.strip('. ')
filename = slugify(filename)
return filename
def create_webpage(data, template_path, filename):
html_code = ''
current_month = ''
new_season = True
new_month = True
months = [
u'Janvier',
u'Février',
u'Mars',
u'Avril',
u'Mai',
u'Juin',
u'Juillet',
u'Août',
u'Septembre',
u'Octobre',
u'Novembre',
u'Décembre'
]
html_code += '<div data-role="collapsible-set" data-inset="false">'
for emission_data in data:
# emission_data = data[hash_dummy]
emission_data = emission_data['infos']
# print emission_data
titre = emission_data['titre']
jj, mm, aa = emission_data['date']['jour'], emission_data['date']['mois'], emission_data['date']['annee']
pagelink = emission_data['lien_emission']
# rediff = emission_data['rediffusion']
if 'lien_ecouter' in emission_data:
player_link = emission_data['lien_ecouter']
mp3link = emission_data['lien_mp3']
else:
player_link = ''
mp3link = ''
new_month = False
print titre
# jj,mm,aa = date
# month = months[int(mm) - 1]
if mm == '09' and new_season:
if current_month != '':
html_code += '''
</ul>
</div> <!-- 3 -->
</div> <!-- saison last month -->
</div> <!-- saison -->
</div> <!-- saison -->
'''
new_month = False
html_code += '''
<div data-role="collapsible" data-collapsed-icon="carat-r" data-expanded-icon="carat-d">
<h2>Saison {a1}-{a2}</h2>
<div class="saison" data-role="collapsible-set" data-inset="false">
'''.format(
a1 = str(aa),
a2 = str(int(aa) + 1),
)
new_season = False
else:
new_month = True
if mm != '09':
new_season = True
if aa + mm != current_month:
if current_month != '' and new_month:
html_code += '''
</ul>
</div> <!-- mois -->
</div> <!-- collapsible mois -->
'''
html_code += u'''
<div data-role="collapsible" data-collapsed-icon="carat-r" data-expanded-icon="carat-d">
<h3>{mm} {aa}</h3>
<div class="mois">
<ul>
'''.format(
mm = months[int(mm) - 1],
aa = str(aa),
)
current_month = aa + mm
line = ' <li>'
line += '<table><tr>'
link_name = aa + '-' + mm + '-' + jj + ' - ' + titre
if player_link and mp3link:
# TEST
title = str2filename(titre)
mp3link = u'./files/{aa}-{mm}-{jj} - {title}.mp3'.format(
aa = aa,
mm = mm,
jj = jj,
title = title,
)
# ENDOF TEST
line += u'''
<td>
<a class="play-link" href="{plink}" >Ecouter</a>
</td>
<td>
<a class="download-link" href="{mlink}" download="{lname}" >Télécharger</a>
</td>
'''.format(
plink = unicode(player_link),
mlink = unicode(mp3link),
lname = unicode(link_name),
)
elif int(aa) < 2011 or (int(aa) == 2011 and int(mm) == 1): # janvier 2011
pascal_link = '''
http://prevost.pascal.free.fr/public/podcast/sur_les_epaules_de_darwin/Jean-Claude%20Ameisen%20-%20SUR%20LES%20EPAULES%20DE%20DARWIN%20{jj}.{mm}.{aa}.mp3
'''.format(
jj = jj,
mm = mm,
aa = aa,
)
line += u'''
<td>
<a class="download-link" href="{plink}" download="{lname}" >Télécharger (via prevost.pascal.free.fr)</a>
</td>
'''.format(
plink = unicode(pascal_link),
lname = unicode(link_name),
)
line += u'''
<td>
<span style="vertical-align:middle;float:left;"><a class="link" href="{plink}" >{ptitre}</a>, diffusée le {date}</span>
</td>
'''.format(
plink = unicode(pagelink),
ptitre = unicode(titre),
date = str(int(jj)) + u' ' + months[int(mm) - 1] + u' ' + str(aa),
)
line += '</tr></table>'
line += u'</li>\n'
html_code += line
html_code += '''
</ul>
</div>
</div>
</div>
</div>
</div>
'''
template_file = codecs.open(template_path, 'r', 'utf-8')
template = template_file.read()
template_file.close()
template = template.replace('%content', html_code)
result_file = codecs.open(filename, 'w', 'utf-8')
result_file.write(template)
result_file.close()
#####################################################################
parser = argparse.ArgumentParser(
description='Création d\'une page web à partir d\'une base de données JSON pour une émission de France Inter.'
)
parser.add_argument(
'-base',
metavar = 'fichier JSON',
help = 'Le fichier JSON qui contient la base de données.',
default = './output/darwin_base.json'
)
parser.add_argument(
'-web',
metavar = 'page web',
help = 'Le fichier contenant la page web créee.',
default = './output/index.html'
)
parser.add_argument(
'-template',
metavar = 'template',
help = 'Le fichier de template de la page web.',
default = './output/temp_public.2017.html'
)
args = parser.parse_args()
template_path = args.template
# template_path = './output/temp3.html'
json_file = args.base
# json_file = './output/darwin_base.json'
result_file = args.web
# result_file = './output/index.php'
#####################################################################
input_json = open(json_file, 'r')
data = json.load(input_json)
input_json.close()
data = data['emissions']
# print data
# keys = [e['hash'] for e in data]
# keys.sort()
# print keys
# sorted_data = [data['infos'] for key in keys]
# print sorted_data
print u'Création de la page web\n'
create_webpage(data, template_path, result_file)
print u'\nPage web créée : ' + result_file