Yago Bolivar
commited on
Commit
·
0831af7
1
Parent(s):
736bdeb
feat: implement file download functionality in get_all_questions
Browse files
utilities/fetch_all_questions.py
CHANGED
@@ -7,10 +7,31 @@ load_dotenv()
|
|
7 |
|
8 |
BASE_URL = os.getenv("BASE_URL")
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def get_all_questions():
|
11 |
response = requests.get(f"{BASE_URL}/questions")
|
|
|
12 |
if response.status_code == 200:
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
else:
|
15 |
raise Exception(f"API request failed with status code {response.status_code}")
|
16 |
|
|
|
7 |
|
8 |
BASE_URL = os.getenv("BASE_URL")
|
9 |
|
10 |
+
def download_file(task_id, file_name):
|
11 |
+
file_endpoint = f"{BASE_URL}/files/{task_id}"
|
12 |
+
file_response = requests.get(file_endpoint)
|
13 |
+
if file_response.status_code == 200:
|
14 |
+
os.makedirs("downloaded_files", exist_ok=True)
|
15 |
+
file_path = os.path.join("downloaded_files", file_name)
|
16 |
+
with open(file_path, "wb") as f:
|
17 |
+
f.write(file_response.content)
|
18 |
+
print(f"Downloaded file for task_id {task_id} to {file_path}")
|
19 |
+
return True
|
20 |
+
else:
|
21 |
+
print(f"Failed to download file for task_id {task_id}. Status code: {file_response.status_code}")
|
22 |
+
return False
|
23 |
+
|
24 |
def get_all_questions():
|
25 |
response = requests.get(f"{BASE_URL}/questions")
|
26 |
+
downloaded_file_counter = 0
|
27 |
if response.status_code == 200:
|
28 |
+
questions = response.json()
|
29 |
+
for question in questions:
|
30 |
+
if "task_id" in question and "file_name" in question and question["file_name"]:
|
31 |
+
if download_file(question["task_id"], question["file_name"]):
|
32 |
+
downloaded_file_counter += 1
|
33 |
+
print(f"Total downloaded files: {downloaded_file_counter}")
|
34 |
+
return questions
|
35 |
else:
|
36 |
raise Exception(f"API request failed with status code {response.status_code}")
|
37 |
|