@ -18,6 +18,8 @@ from datetime import datetime, timedelta
from typing import Optional , Tuple
from urllib . parse import urlparse
import duckdb
sys . path . insert ( 0 , " . " ) # run from project root
from api_client import TweedeKamerAPI
@ -71,8 +73,6 @@ def update_existing_motions(
Returns :
( updated_count , skipped_count ) tuple .
"""
import duckdb
# Read motions with missing body_text
conn_read = duckdb . connect ( db_path , read_only = True )
rows = conn_read . execute (
@ -89,16 +89,22 @@ def update_existing_motions(
updated = 0
skipped = 0
conn_write = duckdb . connect ( db_path , read_only = False )
try :
for row in rows :
motion_id , url , title , description = row
besluit_id = extract_besluit_id ( url or " " )
if not besluit_id :
print ( f " Skipping motion { motion_id } : cannot extract besluit_id from URL " )
print (
f " Skipping motion { motion_id } : cannot extract besluit_id from URL "
)
skipped + = 1
continue
print ( f " Fetching details for motion { motion_id } (besluit_id= { besluit_id } )... " )
print (
f " Fetching details for motion { motion_id } (besluit_id= { besluit_id } )... "
)
details = api . _get_motion_details ( besluit_id )
if not details or not details . get ( " body_text " ) :
@ -123,7 +129,6 @@ def update_existing_motions(
) :
new_desc = details . get ( " description " ) or description
conn_write = duckdb . connect ( db_path , read_only = False )
conn_write . execute (
"""
UPDATE motions
@ -132,13 +137,14 @@ def update_existing_motions(
""" ,
( new_body , new_title , new_desc , motion_id ) ,
)
conn_write . close ( )
updated + = 1
print ( f " Updated motion { motion_id } " )
if delay > 0 and updated + skipped < len ( rows ) :
time . sleep ( delay )
finally :
conn_write . close ( )
return updated , skipped