forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_dynamic.py
More file actions
29 lines (24 loc) · 925 Bytes
/
data_dynamic.py
File metadata and controls
29 lines (24 loc) · 925 Bytes
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
'''
This file is responsible for fetching quiz questions from the Open Trivia Database API.
'''
import requests
parameters = {
"amount": 10,
"type": "multiple",
"category": 18
}
error_message = ""
try:
response = requests.get(url="https://opentdb.com/api.php", params=parameters, timeout=10)
response.raise_for_status() # Raise an exception for HTTP errors
question_data = response.json()["results"]
print("Questions loaded successfully from the API.")
except requests.exceptions.ConnectionError:
error_message = "Network connection is poor. Please check your internet connection."
question_data = []
except requests.exceptions.Timeout:
error_message = "Request timed out. Internet speed might be too slow."
question_data = []
except requests.exceptions.RequestException as e:
error_message = f"An error occurred: {e}"
question_data = []