File size: 2,106 Bytes
28a72c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import requests
import json

API_URL = os.getenv("DEFAULT_API_URL")


def fetch_questions(output_file_path: str | None = None):
    QUESTIONS_URL = f"{API_URL}/questions"

    print(f"Fetching questions from: {QUESTIONS_URL}")
    try:
        response = requests.get(QUESTIONS_URL, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        if not questions_data:
            print("Fetched questions list is empty.")
            return "Fetched questions list is empty or invalid format.", None
        print(f"Fetched {len(questions_data)} questions.")
    except Exception as e:
        raise e

    if output_file_path:
        output_dir_path = os.path.dirname(output_file_path)

        if not os.path.exists(output_dir_path):
            os.makedirs(output_dir_path, exist_ok=True)

        with open(output_file_path, "w") as output_file:
            json.dump(
                questions_data,
                output_file,
                indent=4,
                ensure_ascii=False,
            )

    return questions_data


def download_questions_files(
    questions: list[dict[str, str]], output_folder: str = "./data/assets"
):
    """Download files associated with each question and save them locally."""
    # Implement your logic to check if the question files exist here.
    FILES_URL = f"{API_URL}/files"

    if not os.path.exists(output_folder):
        os.makedirs(output_folder, exist_ok=True)

    for question in questions:
        if file_name := question.get("file_name"):
            task_id = question.get("task_id")
            file_url = f"{FILES_URL}/{task_id}"
            print(f"Downloading file associated with task_id {task_id}...")

            try:
                response = requests.get(file_url, timeout=15)
                response.raise_for_status()

                with open(f"{output_folder}/{file_name}", "wb") as file:
                    file.write(response.content)

                print(f"File downloaded successfully for task_id: {task_id}")
            except Exception as e:
                raise e