-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.py
305 lines (256 loc) · 9.98 KB
/
music.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import itertools
import json
import logging
import multiprocessing
import os
import pprint
import re
import sys
from pelican.generators import ArticlesGenerator
from pelican.generators import PagesGenerator
from pelican.settings import DEFAULT_CONFIG
from pelican import signals
from pelican.utils import pelican_open
import taglib
from shutil import copyfile
logger = logging.getLogger(__name__)
MUSIC_ROOT_OUTPUT = 'music'
def initialized(pelican):
p = os.path.expanduser('~/Music')
DEFAULT_CONFIG.setdefault('MUSIC_LIBRARY', p)
DEFAULT_CONFIG.setdefault('MUSIC_COVER', 'cover.jpg')
DEFAULT_CONFIG['plugin_dir'] = os.path.dirname(os.path.realpath(__file__))
if pelican:
pelican.settings.setdefault('MUSIC_LIBRARY', p)
pelican.settings.setdefault('MUSIC_COVER', 'cover.jpg')
#
# check if copy is needed :DONE:
#
def check_copy(src, dest):
if not os.path.exists(src):
logger.warn('music: Source {} does not exist!!!'.format(src))
return False
if not os.path.exists(dest):
logger.info('music: Destination {} does not exist yet.'.format(dest))
return True
if os.path.getmtime(src) > os.path.getmtime(dest):
logger.info('music: Destination {} is older than source {}.'.format(dest, src))
return True
logger.info('music: Source {} is older than destination {}.'.format(src, dest))
return False
#
# read json tags from previsouly processed tracks :DONE:
#
def read_tags(filename):
tag = {}
with pelican_open(filename) as text:
tag = json.loads(text)
return tag
#
# guess if file is a image (e.g: a cover.jpg) :DONE:
#
def is_image(path):
return path.endswith('.jpeg') or path.endswith('.JPEG') or path.endswith('.jpg') or path.endswith('.JPG') or path.endswith('.png') or path.endswith('.PNG') or path.endswith('.gif') or path.endswith('.GIF')
#
# scan '{music}' contents in HTML and proccess accordingly. :DONE:
#
def replace_music_entities(content):
library = os.path.expanduser(content.settings['MUSIC_LIBRARY'])
site = content.settings['SITEURL']
if site == None:
site = ''
relative = not site.startswith('http')
siteslash = site.endswith('/')
if relative:
outputmusic = os.path.join(site, MUSIC_ROOT_OUTPUT)
elif siteslash:
outputmusic = site + MUSIC_ROOT_OUTPUT
else:
outputmusic = site + '/' + MUSIC_ROOT_OUTPUT
if content._content:
content._content = content._content.replace('{music}', outputmusic)
#
# generates a music_album context object from album header :DONE:
#
def process_album_header(generator, content, header):
library = os.path.expanduser(generator.settings['MUSIC_LIBRARY'])
albumtitle = header
if re.match('^{music}.*{[^}]+}$', albumtitle):
albumtitle = re.sub('^{music}.*{([^}]+)}$', '\\1', albumtitle)
else:
albumtitle = re.sub('^{music}.*/(.+)/?$', '\\1', albumtitle)
logger.info('music: albumtitle {}'.format(albumtitle))
albumpath = header
if re.match('^{music}.*{[^}]+}$', albumpath):
albumpath = re.sub('^({music}.*){[^}]*}$', '\\1', albumpath)
logger.info('music: albumpath {}'.format(albumpath))
albumpath = albumpath.replace('{music}', library)
coverpath = os.path.join(albumpath, generator.settings['MUSIC_COVER'])
albumout = header
if re.match('^{music}.*{[^}]+}$', albumout):
albumout = re.sub('^({music}.*){[^}]*}$', '\\1', albumout)
albumout = albumout.replace('{music}', MUSIC_ROOT_OUTPUT)
absalbumout = os.path.join(generator.output_path, albumout)
logger.info('music: albumout {}'.format(albumout))
coverout = os.path.join(albumout, generator.settings['MUSIC_COVER'])
abscoverout = os.path.join(absalbumout, generator.settings['MUSIC_COVER'])
if not os.path.exists(absalbumout):
try:
os.makedirs(absalbumout)
except Exception:
logger.exception('music: Could not create {}'.format(absalbumout))
# copy cover file to the output directory
if check_copy(coverpath, abscoverout):
copyfile(coverpath, abscoverout)
tagsfiles = []
# copy all track files to the output directory
sortedfiles = os.listdir(albumpath);
sortedfiles.sort()
for f in sortedfiles:
path = os.path.join(albumpath, f)
if os.path.isfile(path) and not f.endswith('.tags') and not is_image(f):
trackout = os.path.join(albumout, f)
abstrackout = os.path.join(absalbumout, f)
# copy audio file if needed
if check_copy(path, abstrackout):
copyfile(path, abstrackout)
# create tags json file if needed
tagspath = os.path.splitext(path)[0] + '.tags'
if check_copy(path, tagspath):
make_tags(path, tagspath)
tracks = []
# rescan now for json tag files
sortedfiles = os.listdir(albumpath);
sortedfiles.sort()
for f in sortedfiles:
path = os.path.join(albumpath, f)
if os.path.isfile(path) and f.endswith('.tags'):
tag = read_tags(path)
track = {
"artist": tag.get('artist', ''),
"album": tag.get('album', ''),
"cover": coverout,
"path": os.path.join(albumout, tag.get('file', '')),
"title": tag.get('title', ''),
"track": tag.get('track', '0'),
"year": tag.get('year', '1970') }
tracks.append(track)
content.music_album = {
"path": albumout,
"title": albumtitle,
"cover": coverout,
"tracks": tracks }
def check_tag(tag):
return {
'artist': tag.get('ARTIST', ['unknown'])[0],
'album': tag.get('ALBUM', ['unknown'])[0],
'title': tag.get('TITLE', ['unknown'])[0],
'track': tag.get('TRACK', ['0'])[0],
'year': tag.get('YEAR', ['1970'])[0] }
#
# make .tags json file from audio file :DONE:
#
def make_tags(infile, outfile):
try:
insong = taglib.File(infile)
except Exception as e:
logger.exception('music: Could not open for reading {}'.format(infile))
return
tag = insong.tags
if tag is None:
tag = {
"artist": '',
"album": '',
"title": '',
"track": '0',
"year": '1970' }
else:
tag = check_tag(tag)
tag['file'] = os.path.basename(infile)
try:
outtags = open(outfile, 'w')
except Exception as e:
logger.exception('music: Could not open for writing {}'.format(outname))
return
jss = json.dumps(tag)
outtags.write(jss)
outtags.close()
#
# generates a music_track context object from track header :DONE:
#
def process_track_header(generator, content, header):
library = os.path.expanduser(generator.settings['MUSIC_LIBRARY'])
trackpath = header
trackpath = trackpath.replace('{music}', library)
tagspath = os.path.splitext(trackpath)[0] + '.tags'
trackout = header
trackout = trackout.replace('{music}', MUSIC_ROOT_OUTPUT)
abstrackout = os.path.join(generator.output_path, trackout)
cover = generator.settings['MUSIC_COVER']
coverpath = os.path.join(os.path.dirname(trackpath), cover)
coverout = os.path.join(os.path.dirname(trackout), cover)
abscoverout = os.path.join(generator.output_path, coverout)
if check_copy(trackpath, tagspath):
make_tags(trackpath, tagspath)
# copy the audio file in output directory
directory = os.path.dirname(abstrackout)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except Exception:
logger.exception('music: Could not create {}'.format(directory))
if check_copy(trackpath, abstrackout):
copyfile(trackpath, abstrackout)
if check_copy(coverpath, abscoverout):
copyfile(coverpath, abscoverout)
tag = read_tags(tagspath)
content.music_track = {
"artist": tag.get('artist', ''),
"album": tag.get('album', ''),
"cover": coverout,
"path": trackout,
"title": tag.get('title', ''),
"track": tag.get('track', '0'),
"year": tag.get('year', '1970') }
if isinstance(generator, ArticlesGenerator):
#print(generator)
return
if isinstance(generator, PagesGenerator):
#print(generator)
return
#
# detect header Track: or Album: and process them :DONE:
#
def on_all_generators_finalized(generators):
for generator in generators:
if isinstance(generator, ArticlesGenerator):
for article in itertools.chain(generator.articles, generator.translations, generator.drafts):
track = article.metadata.get('track', None)
if track and track.startswith('{music}'):
process_track_header(generator, article, track)
album = article.metadata.get('album', None)
if album and album.startswith('{music}'):
process_album_header(generator, article, album)
elif isinstance(generator, PagesGenerator):
for page in itertools.chain(generator.pages, generator.translations, generator.hidden_pages):
track = page.metadata.get('track', None)
if track and track.startswith('{music}'):
process_track_header(generator, page, track)
album = article.metadata.get('album', None)
if album and album.startswith('{music}'):
process_album_header(generator, page, album)
#
# entry point.
#
def register():
"""Uses the new style of registration based on GitHub Pelican issue #314."""
signals.initialized.connect(initialized)
try:
signals.content_object_init.connect(replace_music_entities)
signals.all_generators_finalized.connect(on_all_generators_finalized)
#signals.article_writer_finalized.connect(on_writer_finalized)
except Exception as e:
logger.exception('music: Plugin failed to execute: {}'.format(pprint.pformat(e)))