File indexing completed on 2024-04-14 14:08:48

0001 #!/usr/bin/python3
0002 #
0003 # GCompris - download-assets.py
0004 #
0005 # SPDX-FileCopyrightText: 2016-2018 Johnny Jazeix <jazeix@gmail.com>
0006 #
0007 #   SPDX-License-Identifier: GPL-3.0-or-later
0008 import urllib.request
0009 import shutil
0010 import sys
0011 import os
0012 
0013 # see https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3/7244263#7244263
0014 def download_file(from_url, output_dir):
0015     file_name = output_dir + "/" + from_url.split('/')[-1]
0016     print(f"Start to download {file_name}")
0017     with urllib.request.urlopen(from_url) as response, open(file_name, 'wb') as out_file:
0018         shutil.copyfileobj(response, out_file)
0019     print(f"Downloaded {file_name}")
0020 
0021 def download_contents_and_file(subfolder, key, downloadContents=True):
0022     out = OUTPUT_FOLDER + subfolder
0023     os.makedirs(out, exist_ok=True)
0024     CONTENTS_FILE = out+"Contents"
0025     if downloadContents:
0026         download_file(DOWNLOAD_PATH+subfolder+"Contents", out)
0027     rcc_line = ""
0028     with open(CONTENTS_FILE) as f:
0029         lines = f.readlines()
0030         for line in lines:
0031             if key in line:
0032                 rcc_line = line.split()[1]
0033                 break
0034     download_file(DOWNLOAD_PATH+subfolder+rcc_line, out)
0035 
0036 
0037 # argv[0]: program name
0038 # argv[1]: assets to download (words, full for full rccs, music, locale to get corresponding locale voices)
0039 # argv[2]: audio format (ogg, mp3, aac)
0040 # argv[3]: output directory (rcc directory)
0041 if len(sys.argv) != 4:
0042     print("Usage: download-assets.py \"words,full,music,en,fr,pt_BR\" ogg/mp3/aac outputFolder")
0043     sys.exit(0)
0044 
0045 """Download the voices and words assets depending on the wanted audio format
0046 """
0047 
0048 DOWNLOAD_PATH = "https://cdn.kde.org/gcompris/data3/"
0049 AUDIO_FORMAT = sys.argv[2]
0050 OUTPUT_FOLDER = sys.argv[3]+"/data3/"
0051 
0052 ALL_ASSETS = [x.strip() for x in sys.argv[1].split(",") if len(x)]
0053 DOWNLOAD_WORDS = "words" in ALL_ASSETS
0054 DOWNLOAD_FULL = "full" in ALL_ASSETS
0055 DOWNLOAD_MUSIC = "music" in ALL_ASSETS
0056 
0057 os.makedirs(OUTPUT_FOLDER, exist_ok=True)
0058 
0059 if DOWNLOAD_WORDS:
0060     ALL_ASSETS.remove("words")
0061     download_contents_and_file("words/", "words-webp")
0062 
0063 if DOWNLOAD_FULL:
0064     ALL_ASSETS.remove("full")
0065     download_contents_and_file("", AUDIO_FORMAT)
0066 
0067 if DOWNLOAD_MUSIC:
0068     ALL_ASSETS.remove("music")
0069     download_contents_and_file("backgroundMusic/", AUDIO_FORMAT)
0070 
0071 if ALL_ASSETS:
0072     downloadContents = True
0073     for lang in ALL_ASSETS: # We open, read n times the Contents file, could be optimisable but should not take too much time
0074         download_contents_and_file("voices-"+AUDIO_FORMAT+"/", "-"+lang+"-", downloadContents)
0075         downloadContents = False
0076