feat(main.py): add support for downloading Marp CLI tool from GitHub repository to ensure availability of necessary files for program execution

feat(marpdownloader.py): implement function to download Marp CLI tool from GitHub repository based on specified username, repository, and file extension
This commit is contained in:
Paillat
2023-07-22 11:46:06 +02:00
parent 5428251ee0
commit 08969b75fa
2 changed files with 55 additions and 1 deletions

View File

@@ -9,7 +9,8 @@ from utils.config import loadingmessage, bcolors
from utils.misc import clear_screen, printm, getenv from utils.misc import clear_screen, printm, getenv
from utils.openaicaller import openai from utils.openaicaller import openai
from utils.license import check_license_agreement from utils.license import check_license_agreement
from utils.marpdownloader import download_marp_cli
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
async def main(): async def main():
@@ -20,6 +21,10 @@ async def main():
printm("Welcome in FABLE, the Film and Artistic Bot for Lively Entertainment!") printm("Welcome in FABLE, the Film and Artistic Bot for Lively Entertainment!")
await asyncio.sleep(0.5) await asyncio.sleep(0.5)
printm(f"This program will generate for you complete {bcolors.FAIL}{bcolors.BOLD}YouTube{bcolors.ENDC} videos, as well as uploading them to YouTube.") printm(f"This program will generate for you complete {bcolors.FAIL}{bcolors.BOLD}YouTube{bcolors.ENDC} videos, as well as uploading them to YouTube.")
printm("Please wait until we have downloaded the necessary files...")
await asyncio.sleep(0.5)
download_marp_cli(force_download=bool(settings.force_download_marp_cli))
settings.set_setting('force_download_marp_cli', False)
if not os.path.exists('env.yaml'): if not os.path.exists('env.yaml'):
printm("It looks like you don't have an OpenAI API key yet. Please paste it here:") printm("It looks like you don't have an OpenAI API key yet. Please paste it here:")
openai_api_key = input("Paste the key here: ") openai_api_key = input("Paste the key here: ")

49
utils/marpdownloader.py Normal file
View File

@@ -0,0 +1,49 @@
import requests
import os
import zipfile
from settings import settings
def get_latest_release_download_url(username, repository, file_extension):
api_url = f"https://api.github.com/repos/{username}/{repository}/releases/latest"
response = requests.get(api_url)
if response.status_code == 200:
data = response.json()
for asset in data.get("assets", []):
download_url = asset.get("browser_download_url")
if download_url and download_url.endswith(file_extension):
return download_url
else:
print(f"Failed to fetch latest release information. Status code: {response.status_code}")
return None
def download_file(url, file_name):
response = requests.get(url, stream=True)
with open(file_name, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
# Replace these variables with the desired GitHub repository information
def download_marp_cli(force_download=False):
github_username = "marp-team"
repo_name = "marp-cli"
file_extension = "win.zip" # Replace this with your desired file extension (e.g., "win.zip")
download_url = get_latest_release_download_url(github_username, repo_name, file_extension)
if download_url:
file_name = download_url.split("/")[-1]
download_version = file_name.split("-")[2]
current_version = settings.marp_version
if download_version != current_version or force_download:
download_file(download_url, file_name)
with zipfile.ZipFile(file_name, 'r') as zip_ref:
zip_ref.extractall()
settings.set_setting("marp_version", download_version)
os.remove(file_name)
else:
raise Exception("Failed to get the download URL for the latest release.")
if __name__ == "__main__":
download_marp_cli(force_download=True)