
    !je                        d Z ddlZddlZddlZddlZddlmZmZ ddlmZ  ed      Z	 ed      j                         Zd Zd Zd	 Zd
 Zd Zd Zedk(  rddlZ ej(                         Zej-                  ddd       ej-                  ddd       ej/                         Zej2                  sej"                  r e       Z ede d       y e       Z ede d       yy)a   YouTube -> Obsidian enqueuer for the reconciliation queue.

Scans the YouTube-Research vault for markdown files and enqueues them
into the reconciliation queue (SQLITE) for processing by the Single Pipeline Writer.
No direct Graphiti writes are performed.
    N)datetimetimezone)Pathz+/opt/struktur/youtube-research/knowledge.dbz$/opt/obsidian-vault/YouTube-Researchc                  d    t        j                  t        j                        j	                         S N)r   nowr   utc	isoformat     0/opt/struktur/graphiti/youtube_graphiti_queue.pyr   r      s    <<%//11r   c                      t        j                  t        d      } t         j                  | _        | j                  d       | S )N   )timeoutzPRAGMA busy_timeout=30000)sqlite3connectDBRowrow_factoryexecute)cs    r   dbr      s/    oob$A++AMII)*Hr   c                 *   | j                  d       | j                  d      D ch c]  }|d   	 }}dddddddj                         D ]!  \  }}||vs| j                  d| d	|        # | j                  d
       | j                          y c c}w )Na  CREATE TABLE IF NOT EXISTS graphiti_import_queue(
        id INTEGER PRIMARY KEY,
        source_type TEXT NOT NULL,
        video_id TEXT,
        obsidian_path TEXT NOT NULL,
        content_hash TEXT NOT NULL,
        file_modified_at TEXT,
        graphiti_status TEXT NOT NULL DEFAULT 'queued',
        graphiti_attempts INTEGER NOT NULL DEFAULT 0,
        graphiti_last_attempt_at TEXT,
        graphiti_processed_at TEXT,
        graphiti_episode_id TEXT,
        graphiti_error TEXT,
        next_attempt_at TEXT,
        locked_at TEXT,
        created_at TEXT NOT NULL,
        updated_at TEXT NOT NULL,
        UNIQUE(obsidian_path,content_hash)
    )z(PRAGMA table_info(graphiti_import_queue)   zINTEGER NOT NULL DEFAULT 0TEXTINTEGER)attempt_countlast_attempt_atlast_error_classlast_error_messagelast_http_statuscompleted_atz-ALTER TABLE graphiti_import_queue ADD COLUMN  zkCREATE INDEX IF NOT EXISTS idx_graphiti_queue_due ON graphiti_import_queue(graphiti_status,next_attempt_at))r   itemscommit)r   rexistingnts        r   ensurer*      s     II 	 
& II&PQRqadRHR <vio  FL  `i  y  @  F  F  H a!Haii*WXYWZZ[\][^(_`aII{|HHJ	 Ss   Bc                 .   i }| j                  d      r| j                  dd      d   j                         D ][  }t        j                  d|      }|s|j                  d      j                         j                  d      ||j                  d      <   ] |S )Nz---   r   z^([A-Za-z0-9_-]+):\s*(.*)$")
startswithsplit
splitlinesrematchgroupstrip)soutlinems       r   frontr9   9   s    
C||EGGE!$Q'224 	@Dhh4T:A!''!*"2"2"4":":3"?#aggaj/	@ Jr   c                    t        |       j                         } t        | j                  vs-| j                  j                         dk7  s| j                         sy| j                  d      }t        j                  |j                               j                         }t        |      }t               }t               }t        |       t        j                  d|j!                  d       d| j                               j                         }d|j!                  d       d| }|j#                  d|j!                  d      t%        |       |t'        j(                  | j+                         j,                  t.        j0                        j3                         ||||d	|j!                  d       f	       |j4                  }|j7                          |j9                          t;        t=        |            S )
Nz.mdr   zutf-8)encodingzyoutube:
youtube_id:a@  
        INSERT OR IGNORE INTO graphiti_import_queue(
            source_type,video_id,obsidian_path,content_hash,file_modified_at,
            graphiti_status,created_at,updated_at,reconciliation_record_id,
            import_identity,knowledge_unit_identity
        ) VALUES('youtube',?,?,?,?, 'queued',?,?,?,?,?)
    zsource:)r   resolveROOTparentssuffixloweris_file	read_texthashlibsha256encode	hexdigestr9   r   r   r*   getr   strr   fromtimestampstatst_mtimer   r	   r
   total_changesr%   closeintbool)	pr5   hfr)   r   identityrecordr(   s	            r   enqueuerW   A   s   
1gooA199 0% 7qyy{kk7k#AnnQXXZ **,AAhA	eAdA
1I^^hquu\':&;1QC@GGIJTTVHaeeL)*!A3/FII  
|	SVAh&<&<QVVX=N=Nx||&\&f&f&hijklmst|  @G  HI  HM  HM  NZ  H[  G\  ~]  	^_ ooAHHJGGItAw<r   c                  L    t        d t        j                  d      D              S )Nc              3   2   K   | ]  }t        |        y wr   )rW   ).0rR   s     r   	<genexpr>zreconcile.<locals>.<genexpr>Z   s     6awqz6s   z*.md)sumr?   rglobr   r   r   	reconciler^   Y   s    64::f#5666r   __main__z--once
store_truezRun enqueue once and exit)actionhelpz--reconcilezAlias for --oncez	Enqueued z new/changed filesz new/changed files (default))__doc__rE   osr1   r   r   r   pathlibr   r   r>   r?   r   r   r*   r9   rW   r^   __name__argparseArgumentParserparseradd_argument
parse_argsargsonceenqueuedprintr   r   r   <module>rp      s     	 	  ' 56TBh=i=q=q=s2>07 Z"8""$F
:UV
\?QR				DyyDNN	(#567 	(#?@A r   