import requests
from bs4 import BeautifulSoup
import json
import re


def downloadTranslationFilemap(lang="de"):
    """
    Create a filename -> info map for a given Crowdin.
    The info contains all crowdin info plus the "id" property,
    containing the numeric file ID on Crowdin and the "path" property
    containing the path inside the language directory.
    """
    # Extract filemap
    response = requests.get("http://crowdin.khanacademy.org/project/khanacademy/%s" % lang)
    soup = BeautifulSoup(response.text, "lxml")
    scripttext = soup.find_all("script")[3].text
    jsonStr = scripttext.partition("PROJECT_FILES = ")[2]
    jsonStr = jsonStr.rpartition(", DOWNLOAD_PERMISSIONS")[0].replace("\\/", "/")
    projectFiles = json.loads(jsonStr)
    # Build map for the directory structure
    directoryMap = {
        v["id"]: v["name"] + "/"
        for k, v in projectFiles.items()
        if v["node_type"] == "0"} # 0 -> directory
    # Filter only POT. Create filename -> object map with "id" property set
    idRegex = re.compile("/khanacademy/(\d+)/enus-de")
    return {
        v["name"]: dict(v.items() | [("id", int(v["id"])), ("path", directoryMap[v["parent_id"]] + v["name"])])
        for k, v in projectFiles.items()
        if v["name"].endswith(".pot")}