-
Notifications
You must be signed in to change notification settings - Fork 0
/
cerulean-cloud-city.py
159 lines (141 loc) · 7.84 KB
/
cerulean-cloud-city.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
#!/usr/bin/env python
import os
import urllib
import re
import shutil
import natsort
from argparse import ArgumentParser
#
# global functions
#
def filter_album_names(path, name):
if '.git' in name:
return False
elif name == '.ignore':
return False
return os.path.isdir(os.path.join(path, name))
def filter_tracks(filename):
return True if '.mp3' in filename else False
def clean_track_name(filename):
name = filename.replace('.mp3', '')
while re.match(r'[0-9].*', name):
name = name[1:]
while re.match(r'.*[0-9]$', name):
name = name[:-1]
return name.replace('_', ' ').strip()
def album_download_link(band_metadata, album):
album_name = urllib.quote(album['name'])
return band_metadata['git_root'] + album['name'] + '/' + album_name + '.zip'
def track_download_link(band_metadata, album, track):
track_filename = urllib.quote(track['path'][track['path'].rfind('/'):])
return band_metadata['git_root'] + urllib.quote(album['name']) + '/' + track_filename
def get_metadata(path, band_name, git_root):
try:
with open(os.path.join(path, 'description'), 'r') as desc:
description = desc.read()
except Exception:
description = ''
metadata = {'name': band_name,
'description': description,
'albums': [],
'git_root': git_root}
album_paths = natsort.natsorted(os.listdir(path))
for album_name in filter(lambda a: filter_album_names(path, a), album_paths):
album_path = os.path.join(path, album_name)
try:
with open(os.path.join(album_path, 'description'), 'r') as desc:
album_description = desc.read()
except Exception:
album_description = 'Shorts are comfy and easy to wear!'
tracks = []
track_number = 1
track_paths = natsort.natsorted(os.listdir(album_path))
for track in filter(filter_tracks, track_paths):
track_name = clean_track_name(track)
tracks.append({'number': track_number,
'name': track_name,
'path': os.path.join(album_path, track)})
track_number += 1
metadata['albums'].append({'name': album_name,
'path': album_path,
'tracks': tracks,
'description': album_description})
return metadata
def copy_deps(templates_path, build_path):
deps = map(lambda x: "{}/{}".format(templates_path, x), ('jquery.min.js',))
map(lambda x: shutil.copyfile(x, build_path + '/' + shutil._basename(x)), deps)
#
# main program
#
def main(templates_path, args):
# get the band metadata
band_metadata = get_metadata(args.band_path, args.band_name, args.binaries_path)
# always clean html files
try:
cleanup_files = [html_file for html_file in os.listdir(args.build_path) if html_file.endswith('.html')]
for html_file in cleanup_files:
os.remove(args.build_path + '/' + str(html_file))
except OSError:
# if there's nothing there then make the directory
os.makedirs(args.build_path)
# build the site
with open("{}/index.html".format(args.build_path), 'w') as index:
with open("{}/band.header.template.html".format(templates_path), 'r') as header_template:
for line in [template_line.replace('~~BAND~~', band_metadata['name']) for template_line in header_template.readlines()]:
index.write(line)
with open("{}/band.template.html".format(templates_path), 'r') as band_template:
for line in [template_line.replace('~~BAND~~', band_metadata['name']) for template_line in band_template.readlines()]:
index.write(line)
with open("{}/description.template.html".format(templates_path), 'r') as description_template:
for line in [template_line.replace('~~DESCRIPTION~~', band_metadata['description']) for template_line in description_template.readlines()]:
index.write(line)
for album_index, album in enumerate(band_metadata['albums']):
album_page_name = "{}.html".format(urllib.quote(album['name'].replace(' ', '-')))
album_page_path = "{}/{}".format(args.build_path, album_page_name)
with open("{}/album.template.html".format(templates_path), 'r') as album_template:
for line in album_template.readlines():
line = line.replace('~~ALBUM~~', album['name'])
line = line.replace('~~ALBUM_PATH~~', album_page_name)
line = line.replace('~~ALBUM_DOWNLOAD_LINK~~', album_download_link(band_metadata, album))
index.write(line)
with open("{}/description.template.html".format(templates_path), 'r') as description_template:
for line in [template_line.replace('~~DESCRIPTION~~', album['description']) for template_line in description_template.readlines()]:
index.write(line)
with open(album_page_path, 'w') as album_page:
with open("{}/album.header.template.html".format(templates_path), 'r') as header_template:
for line in header_template.readlines():
line = line.replace('~~BAND~~', band_metadata['name'])
line = line.replace('~~ALBUM~~', album['name'])
line = line.replace('~~ALBUM_PATH~~', album_page_name)
line = line.replace('~~ALBUM_DOWNLOAD_LINK~~', album_download_link(band_metadata, album))
line = line.replace('~~DESCRIPTION~~', album['description'])
line = line.replace('~~FIRST_TRACK_DOWNLOAD_LINK~~', track_download_link(band_metadata, album, album['tracks'][0]))
line = line.replace('~~FIRST_TRACK_TITLE~~', album['tracks'][0]['name'])
album_page.write(line)
for track in album['tracks']:
with open("{}/track.template.html".format(templates_path), 'r') as track_template:
for line in track_template.readlines():
line = line.replace('~~ALBUM~~', album['name'])
line = line.replace('~~TRACK_TITLE~~', track['name'])
line = line.replace('~~TRACK_DOWNLOAD_LINK~~',
track_download_link(band_metadata, album, track))
album_page.write(line)
with open("{}/album.end.template.html".format(templates_path), 'r') as album_end_template:
album_page.write(album_end_template.read())
with open("{}/footer.template.html".format(templates_path), 'r') as footer_template:
album_page.write(footer_template.read())
with open("{}/footer.template.html".format(templates_path), 'r') as footer_template:
index.write(footer_template.read())
# copy the dependencies
copy_deps(templates_path, args.build_path)
if __name__ == '__main__':
project_root = os.path.dirname(os.path.realpath(__file__))
parser = ArgumentParser(
description='generates a super awesome website based on a folder full of music',
epilog='see http://github.com/tonygaetani/cerulean-cloud-city for more details')
parser.add_argument('--band-name', default='Cerulean', help='name of the band')
parser.add_argument('--band-path', default=project_root + '/../cerulean', help='folder containing the music')
parser.add_argument('--build-path', default='build', help='where to put the website')
parser.add_argument('--binaries-path', default='https://raw.githubusercontent.com/marshzor/cerulean/master/',
help='root path to binaries for download/streaming')
main(project_root + '/templates', parser.parse_args())