import eyed3 import os import sys import shutil from html_writer import Html #TracksDir class definition class TracksDir: def __init__(self, trackFiles): self.trackFiles = trackFiles #HTML Output def toHTML(self): head = Html() head.self_close_tag('meta', attributes=dict(charset='utf-8')) head.self_close_tag('link', attributes=dict(href='assets/labelize.css',rel='stylesheet')) body = Html() trackNumber = 0 with body.tag('div',classes=['track-list']): #body.tag_with_content('Track List', name='h2') with body.tag('div',attributes=dict(id='player-toggler')): body.self_close_tag('img', attributes=dict(src='assets/labelize.png')) with body.tag('ul') as list: for t in self.trackFiles: trackNumber = trackNumber + 1 trackId = 'track-' + str(trackNumber) audiofile = eyed3.load("build/" + t) with body.tag('li') as li: print(audiofile.tag.title) li += str(audiofile.tag.title or 'untitled')+ ' - ' li += str(audiofile.tag.album or 'untitled')+ ' - ' li += audiofile.tag.artist with body.tag('button',attributes=dict(onclick="togglePlay(this)")): body.self_close_tag('img', attributes=dict(src='assets/play.png')) with body.tag('audio', attributes=dict(src=t, id=trackId, ontimeupdate="updateProgress(this)")) as audio: audio+="Your browser does not support the audio element" # with body.tag('canvas',attributes=dict(id='player-progress')) as canvas: # canvas += "player's progress bar" with body.tag('progress',attributes=dict(id='player-progress',value='0')) as canvas: canvas += "player's progress bar" with body.tag('div',attributes=dict(id='time-info')) as timeInfo: timeInfo += '00:00' with body.tag('script', attributes=dict(src='assets/labelize.js')) as script: script+=""#script tag is not added without that trick return Html.html_template(head, body).to_raw_html(indent_size=2) # script usage function def usage(): print('USAGE : labelize.py [inputDirectory] [outputDirectory]') # script beginning arguments = len(sys.argv) - 1 if(arguments != 2): usage() sys.exit(2) inputDirectory = sys.argv[1] outputDirectory = sys.argv[2] def copyDirectory(src, dest): try: shutil.copytree(src, dest) except shutil.Error as e: print('Directory not copied. Error: %s' % e) except OSError as e: print('Directory not copied. Error: %s' % e) #removes existing build directory if exists if os.path.isdir(outputDirectory): shutil.rmtree(outputDirectory) #copies source files in the build directory copyDirectory(inputDirectory, 'build') #copies assets in the build directory copyDirectory('assets', outputDirectory + '/assets') for root, dirs, files in os.walk(inputDirectory): trackFiles = [] for f in files: if f.lower().endswith(('.mp3', '.wav', '.ogg')): trackFiles.append(f) td = TracksDir(trackFiles) f = open(outputDirectory + "/index.html","w+") f.write(td.toHTML()) f.close()