1010try :
1111 resp = requests .get (url , timeout = 10 )
1212 print (f"DEBUG: Status code: { resp .status_code } " )
13- print (f"DEBUG: Content type: { resp .headers .get ('content-type' , 'unknown' )} " )
1413 print (f"DEBUG: Content length: { len (resp .content )} bytes" )
15- print (f"DEBUG: Response headers: { dict (resp .headers )} " )
1614
1715 if resp .status_code != 200 :
18- print (f"DEBUG: HTTP Error - Status: { resp .status_code } " )
19- print (f"DEBUG: Response text: { resp .text } " )
20- raise Exception (f"HTTP { resp .status_code } : { resp .text } " )
16+ print (f"DEBUG: HTTP Error - Response: { resp .text } " )
17+ raise Exception (f"HTTP { resp .status_code } " )
2118
22- # Check if response is empty
2319 if not resp .text .strip ():
2420 print ("DEBUG: ERROR - Response is empty!" )
25- raise Exception ("Empty response from server " )
21+ raise Exception ("Empty response" )
2622
27- # Show first part of response
28- preview = resp .text [:500 ] if len (resp .text ) > 500 else resp .text
29- print (f"DEBUG: Response preview (first 500 chars):" )
30- print (f"'{ preview } '" )
23+ print (f"DEBUG: Response preview: { resp .text [:200 ]} ..." )
3124
32- # Try to parse JSON
33- try :
34- data = json .loads (resp .text )
35- print (f"DEBUG: ✓ JSON parsed successfully, { len (data )} items" )
36-
37- # Show structure of first item
38- if data :
39- print (f"DEBUG: First item structure: { data [0 ]} " )
40- print (f"DEBUG: First item keys: { list (data [0 ].keys ())} " )
41-
42- except json .JSONDecodeError as je :
43- print (f"DEBUG: ✗ JSON decode failed: { je } " )
44- print (f"DEBUG: Error position: line { je .lineno } , column { je .colno } " )
45- print (f"DEBUG: Problematic text around error:" )
46- start = max (0 , je .pos - 50 )
47- end = min (len (resp .text ), je .pos + 50 )
48- print (f"'{ resp .text [start :end ]} '" )
49- raise
50-
51- except requests .exceptions .RequestException as e :
52- print (f"DEBUG: ✗ Request failed: { e } " )
25+ data = json .loads (resp .text )
26+ print (f"DEBUG: ✓ JSON parsed successfully, { len (data )} items" )
5327
54- # Fallback to test data
28+ if data and len (data ) > 0 :
29+ print (f"DEBUG: First item: { data [0 ]} " )
30+
31+ except Exception as e :
32+ print (f"DEBUG: ✗ Download failed: { e } " )
5533 print ("DEBUG: Using fallback test data" )
5634 data = [
5735 {"Answer" : "DNA" , "Question" : "What carries genetic information?" , "Category" : "BIOLOGY" },
5836 {"Answer" : "Photosynthesis" , "Question" : "Process plants use to make food?" , "Category" : "BIOLOGY" },
59- {"Answer" : "Mitochondria" , "Question" : "Powerhouse of the cell?" , "Category" : "BIOLOGY" },
60- {"Answer" : "Evolution" , "Question" : "Theory explaining species change?" , "Category" : "BIOLOGY" },
61- {"Answer" : "Ecosystem" , "Question" : "Community of living and non-living things?" , "Category" : "BIOLOGY" }
37+ {"Answer" : "Mitochondria" , "Question" : "Powerhouse of the cell?" , "Category" : "BIOLOGY" }
6238 ]
63- print (f"DEBUG: Using { len (data )} fallback items" )
64-
65- except Exception as e :
66- print (f"DEBUG: ✗ Unexpected error: { e } " )
67- raise
6839
6940print ("=== DEBUG: Starting batch import ===" )
70-
71- # highlight-start
7241questions = client .collections .get ("Question" )
7342
43+ if not data :
44+ print ("DEBUG: ✗ No data to import" )
45+ client .close ()
46+ exit (1 )
47+
7448print (f"DEBUG: About to import { len (data )} objects" )
7549
50+ # highlight-start
51+ successful_imports = 0
52+ failed_imports = 0
53+
7654with questions .batch .fixed_size (batch_size = 200 ) as batch :
77- for i , d in enumerate (data ):
78- # Validate data structure
79- required_keys = [ "Answer" , "Question" , "Category" ]
80- missing_keys = [ key for key in required_keys if key not in d ]
81- if missing_keys :
82- print ( f"DEBUG: ⚠ Item { i } missing keys: { missing_keys } " )
83- print ( f"DEBUG: Item { i } has keys: { list ( d . keys ()) } " )
84- continue
55+ for i , item in enumerate (data ):
56+ try :
57+ batch . add_object ({
58+ "answer" : item [ "Answer" ],
59+ "question" : item [ "Question" ],
60+ "category" : item [ "Category" ],
61+ } )
62+ successful_imports += 1
8563
86- batch .add_object (
87- {
88- "answer" : d ["Answer" ],
89- "question" : d ["Question" ],
90- "category" : d ["Category" ],
91- }
92- )
93-
94- # Log progress every 50 items
95- if (i + 1 ) % 50 == 0 :
96- print (f"DEBUG: Processed { i + 1 } /{ len (data )} items" )
64+ if (i + 1 ) % 10 == 0 :
65+ print (f"DEBUG: Processed { i + 1 } /{ len (data )} items" )
66+
67+ except KeyError as ke :
68+ failed_imports += 1
69+ print (f"DEBUG: ✗ Item { i } missing key { ke } : { item } " )
70+ except Exception as e :
71+ failed_imports += 1
72+ print (f"DEBUG: ✗ Error with item { i } : { e } " )
9773
9874 # highlight-end
9975 if batch .number_errors > 10 :
100- print (f"DEBUG: ✗ Batch import stopped due to excessive errors: { batch .number_errors } " )
76+ print (f"DEBUG: ✗ Too many batch errors: { batch .number_errors } " )
10177 break
10278
103- print ("DEBUG: Batch import completed" )
79+ print (f "DEBUG: Import attempt completed - Success: { successful_imports } , Failed: { failed_imports } " )
10480
105- # Check for failed imports
10681failed_objects = questions .batch .failed_objects
10782if failed_objects :
108- print (f"DEBUG: ✗ Number of failed imports: { len (failed_objects )} " )
109- print (f"DEBUG: First failed object: { failed_objects [0 ]} " )
110-
111- # Show details of first few failures
112- for i , failed in enumerate (failed_objects [:3 ]):
113- print (f"DEBUG: Failed object { i + 1 } : { failed } " )
83+ print (f"DEBUG: ✗ Batch failed objects: { len (failed_objects )} " )
84+ if len (failed_objects ) > 0 :
85+ print (f"DEBUG: First failed: { failed_objects [0 ]} " )
11486else :
115- print (f "DEBUG: ✓ All imports successful " )
87+ print ("DEBUG: ✓ No batch failures " )
11688
117- # Verify import worked
89+ # Verify import
11890print ("=== DEBUG: Verifying import ===" )
11991try :
12092 aggregate_result = questions .aggregate .over_all (total_count = True )
12193 total_count = aggregate_result .total_count
12294 print (f"DEBUG: ✓ Total objects in collection: { total_count } " )
12395
12496 if total_count > 0 :
125- # Show a few sample objects
126- sample_objects = questions .query .fetch_objects (limit = 3 )
127- print (f"DEBUG: Sample imported objects:" )
128- for i , obj in enumerate (sample_objects .objects ):
129- print (f" { i + 1 } : { obj .properties } " )
130- else :
131- print ("DEBUG: ⚠ No objects found in collection after import" )
132-
97+ sample_objects = questions .query .fetch_objects (limit = 2 )
98+ print (f"DEBUG: Sample objects:" )
99+ for obj in sample_objects .objects :
100+ print (f" - { obj .properties } " )
101+
133102except Exception as e :
134- print (f"DEBUG: ✗ Error verifying import : { e } " )
103+ print (f"DEBUG: ✗ Verification error : { e } " )
135104
136- client .close () # Free up resources
137- print ("DEBUG: Import process completed" )
105+ client .close ()
106+ print ("DEBUG: Import completed" )
138107# END Import
0 commit comments