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

4
FABLE.cmd Normal file
View File

@@ -0,0 +1,4 @@
powershell ./FABLE.ps1
pause
echo HEHE

7
FABLE.ps1 Normal file
View File

@@ -0,0 +1,7 @@
d:/09._AI_projects/FABLE/youtuber/Scripts/Activate.ps1
python main.py
pause
echo This line will be executed after you press any key.

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,63 +63,52 @@ 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 "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")):
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")):
#skip this slide
continue
#continue
# TODO: Do not skip for now, add support for also checking for assets
pass
if "image" in script[i]:
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
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("+", ",")
#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:
with open(slide_asset_path, 'wb') as f:
f.write(requests.get(real_url, allow_redirects=True).content)
f.close()
content = marp.replace("[imagesrc]", "assets/slide" + str(i) + ".jpg")
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:
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'], 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)"
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
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
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
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)):
@@ -100,7 +117,7 @@ async def prepare(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

View File

@@ -6,28 +6,3 @@ class:
- invert
backgroundImage: url(https://images.unsplash.com/photo-1651604454911-fdfb0edde727)
---
<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>

View File

@@ -13,8 +13,8 @@ def download_image(query, download_path):
driver = uc.Chrome(options=options)
try:
driver.get(f"https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=99&q=site:wikipedia.org+{query.replace(' ', '+')}")
time.sleep(2)
driver.get(f"https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=99&q=site:wikipedia.org+{query.replace(' ', '+')}")
tos = driver.find_elements(By.CLASS_NAME, "VfPpkd-vQzf8d")
for to in tos:
@@ -22,14 +22,20 @@ def download_image(query, download_path):
to.click()
break
time.sleep(1)
while True:
f = 0
while f < 10:
try:
driver.get(f"https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=99&q=site:wikipedia.org+{query.replace(' ', '+')}")
image = driver.find_element(By.CLASS_NAME, "rg_i").click()
break
except:
pass
f += 1
finally:
time.sleep(1)
if f == 10:
raise Exception("No image found")
time.sleep(5)
while True:
try:
@@ -46,7 +52,10 @@ def download_image(query, download_path):
if image.startswith("data:"):
image_content = base64.b64decode(image.split(",")[1])
else:
response = requests.get(image, stream=True)
#define a common user agent for all requests
headers = {'User-Agent': 'FABLE/1.2 (Website coming soon; me@paillat.dev)'}
response = requests.get(image, stream=True, headers=headers)
response.raise_for_status()
image_content = response.content