@@ -174,36 +174,68 @@ def convert_member_to_toml(self, member: Dict) -> Dict:
174174
175175 # Add memberships if available
176176 if member .get ('memberships' ):
177- memberships = member ['memberships' ]
178- toml_data ['membershipInfo' ] = {
179- 'congress' : memberships .get ('congress' , 0 ),
180- 'congressDesc' : memberships .get ('congress_desc' , '' ),
181- 'type' : memberships .get ('type' , 0 ),
182- 'typeDesc' : memberships .get ('type_desc' , '' ),
183- 'district' : memberships .get ('district' , 0 ),
184- 'partyListId' : memberships .get ('party_list' , 0 ),
185- 'partyListName' : memberships .get ('party_list_name' , '' )
186- }
187-
188- # Add photo info
177+ memberships = member .get ('memberships' )
178+ # Check if memberships is a dict
179+ if isinstance (memberships , dict ):
180+ toml_data ['membershipInfo' ] = {
181+ 'congress' : memberships .get ('congress' , 0 ),
182+ 'congressDesc' : memberships .get ('congress_desc' , '' ),
183+ 'type' : memberships .get ('type' , 0 ),
184+ 'typeDesc' : memberships .get ('type_desc' , '' ),
185+ 'district' : memberships .get ('district' , 0 ),
186+ 'partyListId' : memberships .get ('party_list' , 0 ),
187+ 'partyListName' : memberships .get ('party_list_name' , '' )
188+ }
189+ elif isinstance (memberships , list ) and memberships :
190+ # If memberships is a list, process all of them
191+ membership_list = []
192+ for mem in memberships :
193+ if isinstance (mem , dict ):
194+ membership_list .append ({
195+ 'congress' : mem .get ('congress' , 0 ),
196+ 'congressDesc' : mem .get ('congress_desc' , '' ),
197+ 'type' : mem .get ('type' , 0 ),
198+ 'typeDesc' : mem .get ('type_desc' , '' ),
199+ 'district' : mem .get ('district' , 0 ),
200+ 'partyListId' : mem .get ('party_list' , 0 ),
201+ 'partyListName' : mem .get ('party_list_name' , '' )
202+ })
203+ if membership_list :
204+ toml_data ['memberships' ] = membership_list
205+
206+ # Add photo information
189207 if member .get ('photo' ):
190- photo = member ['photo' ]
191- toml_data ['photo' ] = {
192- 'url' : photo .get ('url' , '' ),
193- 'size' : photo .get ('size' , 0 ),
194- 'type' : photo .get ('type' , '' )
195- }
208+ photo = member .get ('photo' )
209+ # Check if photo is a dict (not a list or other type)
210+ if isinstance (photo , dict ):
211+ toml_data ['photo' ] = {
212+ 'url' : photo .get ('url' , '' ),
213+ 'size' : photo .get ('size' , 0 ),
214+ 'type' : photo .get ('type' , '' )
215+ }
216+ elif isinstance (photo , list ) and photo :
217+ # If photo is a list, take the first element if it exists
218+ first_photo = photo [0 ] if photo else {}
219+ if isinstance (first_photo , dict ):
220+ toml_data ['photo' ] = {
221+ 'url' : first_photo .get ('url' , '' ),
222+ 'size' : first_photo .get ('size' , 0 ),
223+ 'type' : first_photo .get ('type' , '' )
224+ }
196225
197226 # Add committee memberships
198227 if member .get ('committee_membership' ):
199228 committees = []
200- for comm in member ['committee_membership' ]:
201- if comm :
202- committees .append ({
203- 'code' : comm .get ('committee_code' , '' ),
204- 'name' : comm .get ('name' , '' ),
205- 'title' : comm .get ('title' , '' )
206- })
229+ committee_data = member .get ('committee_membership' , [])
230+ # Handle if committee_membership is a list
231+ if isinstance (committee_data , list ):
232+ for comm in committee_data :
233+ if comm and isinstance (comm , dict ):
234+ committees .append ({
235+ 'code' : comm .get ('committee_code' , '' ),
236+ 'name' : comm .get ('name' , '' ),
237+ 'title' : comm .get ('title' , '' )
238+ })
207239 if committees :
208240 toml_data ['committees' ] = committees
209241
@@ -212,18 +244,21 @@ def convert_member_to_toml(self, member: Dict) -> Dict:
212244 bills = []
213245 # Group bills by congress
214246 bills_by_congress = {}
215- for bill in member ['principal_authored_bills' ]:
216- if bill :
217- congress = bill .get ('congress' , 0 )
218- if congress not in bills_by_congress :
219- bills_by_congress [congress ] = []
220- bills_by_congress [congress ].append ({
221- 'billNo' : bill .get ('bill_no' , '' ),
222- 'date' : bill .get ('date' , '' ),
223- 'name' : bill .get ('name' , '' ),
224- 'nameCode' : bill .get ('name_code' , '' ),
225- 'sequenceNo' : bill .get ('sequence_no' , 0 )
226- })
247+ bills_data = member .get ('principal_authored_bills' , [])
248+ # Handle if principal_authored_bills is a list
249+ if isinstance (bills_data , list ):
250+ for bill in bills_data :
251+ if bill and isinstance (bill , dict ):
252+ congress = bill .get ('congress' , 0 )
253+ if congress not in bills_by_congress :
254+ bills_by_congress [congress ] = []
255+ bills_by_congress [congress ].append ({
256+ 'billNo' : bill .get ('bill_no' , '' ),
257+ 'date' : bill .get ('date' , '' ),
258+ 'name' : bill .get ('name' , '' ),
259+ 'nameCode' : bill .get ('name_code' , '' ),
260+ 'sequenceNo' : bill .get ('sequence_no' , 0 )
261+ })
227262
228263 # Convert to list format
229264 for congress , congress_bills in sorted (bills_by_congress .items ()):
@@ -241,17 +276,20 @@ def convert_member_to_toml(self, member: Dict) -> Dict:
241276 coauthored = []
242277 # Group bills by congress
243278 bills_by_congress = {}
244- for bill in member ['coauthored_bills' ]:
245- if bill :
246- congress = bill .get ('congress' , 0 )
247- if congress not in bills_by_congress :
248- bills_by_congress [congress ] = []
249- bills_by_congress [congress ].append ({
250- 'billNo' : bill .get ('bill_no' , '' ),
251- 'date' : bill .get ('date' , '' ),
252- 'journalNo' : bill .get ('journal_no' , '' ),
253- 'sessionNo' : bill .get ('session_no' , '' )
254- })
279+ bills_data = member .get ('coauthored_bills' , [])
280+ # Handle if coauthored_bills is a list
281+ if isinstance (bills_data , list ):
282+ for bill in bills_data :
283+ if bill and isinstance (bill , dict ):
284+ congress = bill .get ('congress' , 0 )
285+ if congress not in bills_by_congress :
286+ bills_by_congress [congress ] = []
287+ bills_by_congress [congress ].append ({
288+ 'billNo' : bill .get ('bill_no' , '' ),
289+ 'date' : bill .get ('date' , '' ),
290+ 'journalNo' : bill .get ('journal_no' , '' ),
291+ 'sessionNo' : bill .get ('session_no' , '' )
292+ })
255293
256294 # Convert to list format
257295 for congress , congress_bills in sorted (bills_by_congress .items ()):
0 commit comments