From d81b2ccebfc68b8756e77038b5243a1dbf3f42f9 Mon Sep 17 00:00:00 2001 From: Paillat Date: Sun, 2 Jul 2023 12:43:43 +0200 Subject: [PATCH] fix(video.py): remove unnecessary prompt for generating thumbnail fix(video.py): assign url and id properties after uploading video fix(montage.py): fix unsplash_url to use source.unsplash.com instead of api.unsplash.com fix(montage.py): fix image download logic in prepare function fix(marp.md): add missing newline at end of file fix(wiki_downloader.py): fix image download logic in download_image function --- classes/video.py | 6 ++++-- generators/montage.py | 16 ++++++++++------ prompts/marp.md | 27 ++++++++++++++++++++++++++- utils/wiki_downloader.py | 22 ++++++++++++++++++---- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/classes/video.py b/classes/video.py index afb5518..ac8613d 100644 --- a/classes/video.py +++ b/classes/video.py @@ -60,8 +60,8 @@ class Video: "title": self.idea['title'], "description": self.idea['description'] + "\n\n" + credits, } - if input("Do you want to generate a thumbnail ? (y/N) : ").lower() == "y": - await generate_thumbnail( self.path, self.idea['title'], self.idea['description']) + #if input("Do you want to generate a thumbnail ? (y/N) : ").lower() == "y": + await generate_thumbnail( self.path, self.idea['title'], self.idea['description']) videoid = await upload_video( self.path, self.idea['title'], self.metadata['description'], 28, "", "private", self.parent.path) printm(f"Your video is ready! You can find it in { self.path}") video_meta_file = { @@ -71,6 +71,8 @@ class Video: "path": self.path, "url": f"https://www.youtube.com/watch?v={videoid}", } + self.url = video_meta_file['url'] + self.id = videoid with open(os.path.join( self.path, "video.yaml"), "w") as f: yaml.dump(video_meta_file, f) f.close() diff --git a/generators/montage.py b/generators/montage.py index 02ddd7d..3547e72 100644 --- a/generators/montage.py +++ b/generators/montage.py @@ -15,7 +15,7 @@ from utils.wiki_downloader import download_image as wiki_download_image unsplash_access = getenv("unsplash_access_key") if not unsplash_access: raise Exception("UNSPLASH_ACCESS_KEY is not set in .env file") -unsplash_url = "https://api.unsplash.com/photos/random/?client_id=" + unsplash_access + "&query=" +unsplash_url = "https://source.unsplash.com/random/?" async def prepare(path): with open(os.path.join(path, "script.json"), 'r', encoding='utf-8') as f: @@ -42,12 +42,13 @@ async def prepare(path): 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'] + #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).content) + f.write(requests.get(real_url, allow_redirects=True).content) f.close() - content = marp + f"\n\n![bg 70%](assets/slide{i}.jpg)" + 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) elif "wikimage" in script[i]: @@ -62,7 +63,10 @@ async def prepare(path): 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)) - break + 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: diff --git a/prompts/marp.md b/prompts/marp.md index 7df607d..4f3c028 100644 --- a/prompts/marp.md +++ b/prompts/marp.md @@ -5,4 +5,29 @@ class: - lead - invert backgroundImage: url(https://images.unsplash.com/photo-1651604454911-fdfb0edde727) ---- \ No newline at end of file +--- + + +
+ +
\ No newline at end of file diff --git a/utils/wiki_downloader.py b/utils/wiki_downloader.py index 86b08bc..412d2c5 100644 --- a/utils/wiki_downloader.py +++ b/utils/wiki_downloader.py @@ -22,11 +22,25 @@ def download_image(query, download_path): to.click() break time.sleep(1) - image = driver.find_element(By.CLASS_NAME, "rg_i") - image.click() + while True: + try: + image = driver.find_element(By.CLASS_NAME, "rg_i").click() + break + except: + pass + finally: + time.sleep(1) time.sleep(5) - image = driver.find_element(By.CLASS_NAME, "r48jcc").get_attribute("src") or "" - + while True: + try: + image = driver.find_element(By.CLASS_NAME, "iPVvYb").get_attribute("src") or driver.find_element(By.CLASS_NAME, "pT0Scc").get_attribute("src") or driver.find_element(By.CLASS_NAME, "r48jcc").get_attribute("src") or "" + if image != "": + break + except Exception as a: + print(a) + pass + finally: + time.sleep(1) image_content = None if image.startswith("data:"):