MCPcopy
hub / github.com/DrewThomasson/ebook2audiobook / save_db_blocks

Function save_db_blocks

lib/core.py:711–769  ·  view source on GitHub ↗
(session_id:str)

Source from the content-addressed store, hash-verified

709 print(error)
710
711def save_db_blocks(session_id:str)->None:
712 try:
713 session = context.get_session(session_id)
714 if not (session and session.get('id', False)):
715 return
716 data = session['blocks_current']
717 if not data:
718 return
719 db_path = session['blocks_current_db']
720 create_db_blocks(db_path)
721 with sqlite3.connect(db_path) as conn:
722 conn.execute('PRAGMA foreign_keys=ON')
723 conn.execute(
724 'UPDATE stamp SET page=?, block_resume=?, sentence_resume=?, voice=?, tts_engine=?, fine_tuned=? WHERE id=1',
725 (
726 data.get('page'),
727 data.get('block_resume'),
728 data.get('sentence_resume'),
729 data.get('voice'),
730 data.get('tts_engine'),
731 data.get('fine_tuned'),
732 )
733 )
734 new_blocks = data.get('blocks', [])
735 new_ids = {b['id'] for b in new_blocks}
736 existing_ids = {row[0] for row in conn.execute('SELECT id FROM blocks')}
737 removed = existing_ids - new_ids
738 if removed:
739 conn.executemany('DELETE FROM blocks WHERE id=?', [(rid,) for rid in removed])
740 for idx, block in enumerate(new_blocks):
741 block_id = block['id']
742 conn.execute(
743 'INSERT INTO blocks (id, idx, expand, keep, text, voice, tts_engine, fine_tuned) '
744 'VALUES (?, ?, ?, ?, ?, ?, ?, ?) '
745 'ON CONFLICT(id) DO UPDATE SET '
746 'idx=excluded.idx, expand=excluded.expand, keep=excluded.keep, text=excluded.text, '
747 'voice=excluded.voice, tts_engine=excluded.tts_engine, fine_tuned=excluded.fine_tuned',
748 (
749 block_id,
750 idx,
751 1 if block.get('expand') else 0,
752 1 if block.get('keep') else 0,
753 block.get('text', ''),
754 block.get('voice'),
755 block.get('tts_engine'),
756 block.get('fine_tuned'),
757 )
758 )
759 conn.execute('DELETE FROM sentences WHERE block_id=?', (block_id,))
760 sentences = block.get('sentences', [])
761 if sentences:
762 conn.executemany(
763 'INSERT INTO sentences (block_id, idx, text) VALUES (?, ?, ?)',
764 [(block_id, i, s) for i, s in enumerate(sentences)]
765 )
766 conn.commit()
767 except Exception as e:
768 error = f'save_db_blocks() error: {e}'

Callers 5

convert_chapters2audioFunction · 0.85
convert_ebookFunction · 0.85
_click_gr_deletionFunction · 0.85
_change_current_blocksFunction · 0.85
_update_gr_save_sessionFunction · 0.85

Calls 2

create_db_blocksFunction · 0.85
get_sessionMethod · 0.80

Tested by

no test coverage detected