-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_readme.py
131 lines (103 loc) · 4.59 KB
/
generate_readme.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
import os
import re
from datetime import datetime
class Music():
def __init__(self, pdf: str):
'''
Music data structure to hold music data
Args:
pdf (str): relative path to pdf, with where this file is at being the root reference
'''
self.root = "https://potatowagon.github.io/scores/"
self.title = os.path.basename(pdf).split('.')[0]
self.audio_src = None
self.pdf = (self.root + pdf).replace('\\', '/')
def set_audio_src(self, src: str):
'''
Set the audio source to the source path when it is hosted online
Args:
src (str): relative path to pdf, with where this file is at being the root reference
'''
# .wav files take priority
if src.endswith(".wav"):
self.audio_src = (self.root + src).replace('\\', '/')
if self.audio_src is None and src.endswith(".mp3"):
self.audio_src = (self.root + src).replace('\\', '/')
class MusicContentCreator():
def __init__(self, dir_path, spotlight=[]):
self.spotlight = spotlight
self.music_list = self._collect_music(dir_path)
self.music_content_list = []
def _music_content(self, music: Music):
header = '### ' + music.title.replace('_', ' ')
pdf_link = '[%s.pdf](%s)' % (music.title, music.pdf)
return '\n\n'.join([header, MarkdownUtils.embedded_audio(music.audio_src), pdf_link, MarkdownUtils.embedded_pdf(music.pdf)])
def _collect_music(self, dir_path):
music_list = []
dir_items = os.listdir(dir_path)
for item in dir_items:
if item.endswith('.pdf'):
music_list.append(Music(os.path.join(os.path.basename(dir_path), item)))
for item in dir_items:
if item.endswith('.mp3') or item.endswith('.wav'):
title = item.split('.')[0]
for music in music_list:
if title == music.title:
music.set_audio_src(os.path.join(os.path.basename(dir_path), item))
return music_list
def _get_music(self, title):
for music in self.music_list:
if title == music.title:
return music
raise FileNotFoundError("canot find %s in music_list" % (title))
def make_music_content_section(self):
for title in self.spotlight:
music = None
try:
music = self._get_music(title)
except FileNotFoundError as e:
print("Cannot find spotlight title %s" % title)
if music:
self.music_content_list.append(self._music_content(music))
self.music_list.remove(music)
for music in self.music_list:
self.music_content_list.append(self._music_content(music))
return '\n\n'.join(self.music_content_list)
class MarkdownUtils():
@classmethod
def embedded_pdf(cls, pdf):
return '<object data=\"%s\" type=\"application/pdf\" width=\"1000px\" height=\"500px\"><embed src="%s"></embed></object>' % (pdf, pdf)
@classmethod
def embedded_audio(cls, audio_src):
return '<figure><audio controls src=\"%s\">Your browser does not support the<code>audio</code> element.</audio></figure>' % (audio_src)
@classmethod
def last_update_timestamp(cls):
return "Last updated on %s" % str(datetime.now())
@classmethod
def write_readme(cls, txt):
with open("README.md", "w+") as md:
md.write(txt)
def main():
items = []
done_spotlight = ["the_eggs"]
wip_spotlight = ["Secret_Diary"]
header1 = "# Scores"
musescore_link = "[musecore, no pro `:(`](https://musescore.com/user/28262500)"
soundcloud_link = "[Soundcloud](https://soundcloud.com/sherry-wong-59815924)"
docs_link = "[This page is auto-generated with a python script!](doc.md)"
items.append(header1)
items.append(musescore_link)
items.append(soundcloud_link)
items.append(docs_link)
done_music_content_creator = MusicContentCreator(os.path.join(os.getcwd(), "done"), done_spotlight)
done_music_section = done_music_content_creator.make_music_content_section()
items.append(done_music_section)
wip_header = "## WIP"
items.append(wip_header)
wip_music_content_creator = MusicContentCreator(os.path.join(os.getcwd(), "wip"), wip_spotlight)
wip_music_section = wip_music_content_creator.make_music_content_section()
items.append(wip_music_section)
items.append(MarkdownUtils.last_update_timestamp())
md = '\n\n'.join(items)
MarkdownUtils.write_readme(md)
main()