feat(FABLE.cmd): add FABLE.cmd script to run FABLE.ps1 and pause execution

feat(FABLE.ps1): add script to activate environment and run main.py
fix(montage.py): fix issue with downloading images and creating slide assets
fix(montage.py): fix issue with generating markdown slides
fix(montage.py): fix issue with generating huge slides
fix(montage.py): fix issue with generating blank slides
fix(thumbnail.py): remove TODO comment
fix(marp.md): remove unused CSS styles
fix(wiki_downloader.py): add user agent header to requests
This commit is contained in:
Paillat
2023-07-08 20:58:45 +02:00
parent d81b2ccebf
commit a82c6446ba
6 changed files with 92 additions and 81 deletions

View File

@@ -17,6 +17,34 @@ if not unsplash_access:
raise Exception("UNSPLASH_ACCESS_KEY is not set in .env file")
unsplash_url = "https://source.unsplash.com/random/?"
marp_image = """
<style>
section {
display: flex;
justify-content: center;
align-items: center;
}
.image-container {
width: 90%;
max-height: 90%;
display: flex;
justify-content: center;
align-items: center;
}
.image-container img {
object-fit: contain;
width: 100%;
height: 100%;
}
</style>
<div class="image-container">
<img src="[imagesrc]"/>
</div>
"""
async def prepare(path):
with open(os.path.join(path, "script.json"), 'r', encoding='utf-8') as f:
script = json.load(f)
@@ -35,72 +63,61 @@ async def prepare(path):
generator = VoiceGenerator(speaker=choosen_voice)
print("Generating audio for slide " + str(i))
generator.generate_voice(audio_path, script[i]['spoken'])
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")):
#skip this slide
#continue
# TODO: Do not skip for now, add support for also checking for assets
pass
if "image" in script[i]:
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")) and os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".png")):
#skip this slide
continue
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
url= unsplash_url + script[i]['image'].replace("+", ",")
#r = requests.get(url)
#real_url = r.json()['urls']['raw']
real_url = url
with open(path + "/slides/assets/slide" + str(i) + ".jpg", 'wb') as f:
f.write(requests.get(real_url, allow_redirects=True).content)
f.close()
content = marp.replace("[imagesrc]", "assets/slide" + str(i) + ".jpg")
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
slide_asset_path = os.path.abspath(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg"))
w = 0
while (not os.path.exists(slide_asset_path) or w < 5) and not os.path.exists(path + "/slides/slide" + str(i) + ".md"):
url= unsplash_url + script[i]['image'].replace("+", ",")
real_url = url
with open(slide_asset_path, 'wb') as f:
f.write(requests.get(real_url, allow_redirects=True).content)
f.close()
content = marp
content += "\n\n" + marp_image
content = content.replace("[imagesrc]", "assets/slide" + str(i) + ".jpg")
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
w += 1
elif "wikimage" in script[i]:
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")) and os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".png")):
#skip this slide
continue
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
r = 0
while True:
try:
print("Trying to download image for slide " + str(i))
wiki_download_image(script[i]['wikimage'], os.path.abspath(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg")))
print("Downloaded image for slide with wikiimage " + str(i))
if not os.path.exists(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg")):
raise FileNotFoundError
else:
break
except:
r += 1
if r > 5:
break
continue
content = marp + f"\n\n![bg 70%](assets/slide{i}.jpg)"
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
w = 0
slide_asset_path = os.path.abspath(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg"))
while not (os.path.exists(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg")) and os.path.exists(os.path.abspath(os.path.join(path, "slides", "slide" + str(i) + ".md")))):
print("Trying to download image for slide " + str(i))
wiki_download_image(script[i]['wikimage'], slide_asset_path)
content = marp
content += "\n\n" + marp_image
content = content.replace("[imagesrc]", "assets/slide" + str(i) + ".jpg")
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
w += 1
elif "markdown" in script[i]:
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n" + script[i]['markdown'])
while not os.path.exists(path + "/slides/slide" + str(i) + ".md"):
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n" + script[i]['markdown'])
elif "huge" in script[i]:
#use fit
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n# <!-- fit --> " + script[i]['huge'])
while not os.path.exists(path + "/slides/slide" + str(i) + ".md"):
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n# <!-- fit --> " + script[i]['huge'])
else:
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n") # blank slide
while not os.path.exists(path + "/slides/slide" + str(i) + ".md"):
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n") # blank slide
for i in range(len(script)):
markdown_path = os.path.join(path, f"slides/slide{i}.md")
markdown_path = os.path.abspath(markdown_path)
image_path = os.path.join(path, f"slides/slide{i}.png")
image_path = os.path.abspath(image_path)
if not os.path.exists(image_path):
command = f'marp.exe "{markdown_path}" -o "{image_path}" --allow-local-files'
command = f'marp.exe --html "{markdown_path}" -o "{image_path}" --allow-local-files'
os.system(command)
return script

View File

@@ -29,7 +29,6 @@ Here is the title of the video: [TITLE]
Here is the description of the video: [DESCRIPTION]'''
# TODO: make jpg qith 90% quality default when generating the image to avoid having to convert it later