1  �
   2  
   3  
   4  o�j2�
   5  �v�SrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	Sr
   6  \RRS5(aSO4\RR\RR\5S5r\RR\S5rSSS	S
   7  /SQSS
   8  /SSSS./SS/S.rSrSrSrSrSr"SS\R0R25r\S:XaKS\R8l\R8"S\
   9  4\5r\"S\
  10  35 \RA5 SSS5 gg!,(df   g=f)z�
  11  NeuralOS Private Stremio Addon Server
  12  Exposes the Stremio Addon Protocol v3 on Port 7000.
  13  Allows direct sequential streaming of Magnet Vault releases on Mac, iPhone, and Smart TV.
  14  �NiXz/var/neural_memory/data�dataz
  15  magnets.dbzorg.neuralos.magnetvaultz1.0.0zNeuralOS Magnet VaultzJAutonomous Media Catalog & Magnet Vault powered by PicoClaw & Dell Node-01)�catalog�meta�stream�movie�series�neuralos_vault_moviesu ⚡ NeuralOS Vault (Agent Picks))�type�id�name�tt�nv_)r�versionr�description�	resources�types�catalogs�
  16  idPrefixesc��[RR[5(dg[R
  17  "[5n[RUlU$�N)�os�path�exists�DB_PATH�sqlite3�connect�Row�row_factory)�conns �-/Users/shubhamsharma/project/stremio_addon.py�get_db_connectionr!%s7��
  18  �7�7�>�>�'�"�"���?�?�7�#�D��{�{�D���K�c��[5nU(d/$UR5nURS5 UR5n/nUHqnUS(aUSOSUS3nUS(aUSOSnUR	USUSUUS	(a[US	5OS
  19  SUSS
  20  USSUSS3S.5 Ms UR
  21  5 U$)Nz�
  22      SELECT MIN(id) as id, title, year, poster_url, imdb_id, quality, size_str, COUNT(*) as release_cnt
  23      FROM magnets
  24      GROUP BY title, year
  25      ORDER BY id DESC
  26      LIMIT 100;
  27      �imdb_idrr�
  28  poster_url�Bhttps://images.unsplash.com/photo-1489599849927-2ee91cede3ba?w=500r�title�year�zIndexed in NeuralOS Vault. �release_cntz releases available (�qualityz / �size_strz).�rr
  29  r�poster�releaseInfor)r!�cursor�execute�fetchall�append�str�close)r�c�rows�metas�r�item_idr.s       r �get_catalog_metasr;,s�����D���	����
  30  �A��I�I��	�
  31  �:�:�<�D��E�
  32  ��"#�I�,�!�I�,�c�!�D�'��O��$%�l�O��<��9}��
  33  ������g�J��-.�v�Y�3�q��y�>�B�8��=�9I�8J�J_�`a�bk�`l�_m�mp�qr�s}�q~�p�@B�C�
  34  
  35  �	��	�J�J�L��Lr"c��[5nU(dgUR5nURS5(aURSU45 OVURS5(a/[	URSS55nURSU45 OUR
  36  5 gUR5nUR
  37  5 U(dgUS(aUSOSnUSUS	UUS
  38  (a[US
  39  5SS.$SSS.$)
  40  Nr
  41  z\SELECT id, title, year, poster_url, imdb_id, quality FROM magnets WHERE imdb_id = ? LIMIT 1;rr)zWSELECT id, title, year, poster_url, imdb_id, quality FROM magnets WHERE id = ? LIMIT 1;r%r&rr'r(z]Autonomous release cached in NeuralOS Magnet Vault. Direct sequential torrent stream enabled.r-)	r!r0�
  42  startswithr1�int�replacer5�fetchoner4)r:rr6�raw_idr9r.s      r �get_meta_detailrBIs�����D������
  43  �A����$���	�	�	�p�sz�r|�}�	�	�	�E�	"�	"��W�_�_�U�B�/�0��	�	�	�k�nt�mv�w��
  44  �
  45  ���	�
  46  �
  47  ��A��J�J�L��� !�,��Q�|�_�5y�F����'�
  48  ��)*�6��s�1�V�9�~�v�
  49  ��
  50  9;�v�
  51  �r"c���[5nU(d/$UR5nURS5(aURSU45 O�URS5(ap[	URSS55nURSU45 UR
  52  5nU(aURSUS45 O&URSU45 OUR5 /$UR5nUR5 /nUHfnUS	nUS
  53  =(d Sn	US=(d S
  54  n
  55  USnUS=(d SR5nURSU	SU
  56  SUSUS3	US.5 Mh U$)Nr
  57  zqSELECT info_hash, quality, size_str, seeders, source, title FROM magnets WHERE imdb_id = ? ORDER BY seeders DESC;rr)z'SELECT title FROM magnets WHERE id = ?;zoSELECT info_hash, quality, size_str, seeders, source, title FROM magnets WHERE title = ? ORDER BY seeders DESC;r'zVSELECT info_hash, quality, size_str, seeders, source, title FROM magnets WHERE id = ?;�	info_hashr+�1080pr,zN/A�seeders�source�vault�[z] u ⚡ z seeds (�))r'�infoHash)r!r0r=r1r>r?r@r5r2�upperr3)
  58  r:rr6rA�	title_rowr7�streamsr9rD�qual�sz�seeds�srcs
  59               r �get_streamsrSfs�����D���	����
  60  �A����$���	�	�	�F�IP�HR�	S�	�	�	�E�	"�	"��W�_�_�U�B�/�0��	�	�	�;�f�Y�G��J�J�L�	��
  61  �I�I�H�KT�U\�K]�J_�
  62  `�
  63  �I�I�n�qw�py�z��
  64  �
  65  ���	��:�:�<�D��J�J�L��G�
  66  ���k�N�	���|�&�w��
  67  �z�]�
  68  #�e���)�����{�%�g�,�,�.��������b���E�%�����Q�?�!�
  69  �	���Nr"c�x�[5nSn/nU(aeUR5nURS5 UR5SnURS5 UR	5nUR5 SUS3nSUS3n/nUH�nUSUSUS	US
  70  USUSUS
  71  US4up�p�p�nnUR
  72  S[R"U
  73  5SU(aSUS3OSSUSU
  74  SUSUR5SUSSS35 M� U(aSRU5OSnSUSUSUSUS 3	$)!NrzSELECT COUNT(*) FROM magnets;zmSELECT id, title, year, quality, size_str, seeders, info_hash, source FROM magnets ORDER BY id DESC LIMIT 15;zhttp://�/manifest.jsonz
  75  stremio://rr'r(r+r,rFrDrGzG
  76          <tr>
  77              <td style="color:#58a6ff; font-weight:bold;">� �(rJr)z*</td>
  78              <td><span class="badge">z</span></td>
  79              <td>uC</td>
  80              <td style="color:#38ef7d; font-weight:bold;">⚡ z?</td>
  81              <td style="font-size:0.85em; color:#8b949e;">zD</td>
  82              <td><code style="font-size:0.8em; color:#e3b341;">�z...</code></td>
  83          </tr>z�<tr><td colspan='6' style='text-align:center; padding:20px; color:#8b949e;'>Vault is empty. Search with magnet_vault.py or via PicoClaw.</td></tr>ug	<!DOCTYPE html>
  84  <html lang="en">
  85  <head>
  86      <meta charset="utf-8">
  87      <meta name="viewport" content="width=device-width, initial-scale=1.0">
  88      <title>NEURALOS // STREMIO STREAMING GATEWAY</title>
  89      <style>
  90          :root {
  91              --bg-body: #0d1117;
  92              --bg-card: #161b22;
  93              --border: #30363d;
  94              --text: #c9d1d9;
  95              --text-dim: #8b949e;
  96              --text-bright: #f0f6fc;
  97              --blue: #58a6ff;
  98              --green: #38ef7d;
  99              --purple: #d2a8ff;
 100          }
 101          * { box-sizing: border-box; }
 102          body {
 103              background: var(--bg-body);
 104              color: var(--text);
 105              font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
 106              padding: 24px;
 107              margin: 0;
 108              line-height: 1.5;
 109          }
 110          a { color: var(--blue); text-decoration: none; }
 111          a:hover { text-decoration: underline; }
 112          .box {
 113              background: var(--bg-card);
 114              border: 1px solid var(--border);
 115              border-radius: 6px;
 116              padding: 20px;
 117              margin-bottom: 24px;
 118          }
 119          .header { color: var(--blue); font-weight: bold; font-size: 1.2em; margin-bottom: 8px; }
 120          .btn {
 121              display: inline-block;
 122              background: #238636;
 123              color: #fff;
 124              padding: 10px 18px;
 125              border-radius: 6px;
 126              font-weight: bold;
 127              font-size: 0.95em;
 128              cursor: pointer;
 129              border: none;
 130              text-decoration: none;
 131          }
 132          .btn:hover { background: #2ea043; text-decoration: none; }
 133          .badge { background: rgba(88,166,255,0.2); color: var(--blue); padding: 2px 6px; border-radius: 4px; font-size: 0.8em; }
 134          table { width: 100%; border-collapse: collapse; margin-top: 14px; }
 135          th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #21262d; }
 136          th { color: var(--text-dim); font-size: 0.85em; text-transform: uppercase; }
 137          tr:hover td { background: rgba(56, 139, 253, 0.05); }
 138      </style>
 139  </head>
 140  <body>
 141  
 142  <div class="box">
 143      <div class="header">⚡ NEURALOS MAGNET VAULT & STREMIO ADDON GATEWAY</div>
 144      <div style="color:var(--text-dim); margin-bottom: 16px;">
 145          Host: <strong style="color:#f0f6fc;">dell-ss.lan:7000</strong> &nbsp;|&nbsp; 
 146          Catalog Size: <strong style="color:var(--green);">z� Releases</strong> &nbsp;|&nbsp; 
 147          Anonymity: <strong style="color:var(--purple);">TOR SOCKS5 ACTIVE</strong>
 148      </div>
 149  
 150      <div style="display: flex; gap: 12px; align-items: center; flex-wrap: wrap;">
 151          <a class="btn" href="u%">📺 Install in Stremio (1-Click)</a>
 152          <button class="btn" style="background:#21262d; border:1px solid var(--border);" onclick="copyLink()">📋 Copy Addon URL</button>
 153          <a class="btn" style="background:#21262d; border:1px solid var(--border);" href="http://dell-ss.lan:8080/">🏠 Return to Media Vault (:8080)</a>
 154      </div>
 155  
 156      <div style="margin-top: 16px; font-size: 0.85em; color:var(--text-dim);">
 157          Addon Manifest URL: <code id="addonUrl" style="background:#0d1117; padding:4px 8px; border-radius:4px; color:#58a6ff;">a�</code>
 158      </div>
 159  </div>
 160  
 161  <div class="box">
 162      <div style="font-weight: bold; color: #f0f6fc; margin-bottom: 8px;">Recent Cached Releases Available in Stremio:</div>
 163      <table>
 164          <thead>
 165              <tr>
 166                  <th>Title</th>
 167                  <th>Quality</th>
 168                  <th>Size</th>
 169                  <th>Health</th>
 170                  <th>Source</th>
 171                  <th>Hash</th>
 172              </tr>
 173          </thead>
 174          <tbody>
 175              a
 176          </tbody>
 177      </table>
 178  </div>
 179  
 180  <script>
 181  function copyLink() {
 182      const url = document.getElementById('addonUrl').innerText;
 183      navigator.clipboard.writeText(url).then(() => alert('Copied Stremio Addon URL to clipboard!'));
 184  }
 185  </script>
 186  </body>
 187  </html>)r!r0r1r@r2r5r3�html�escaperL�join)�host_headerr�	total_cnt�recent_rowsr6�lan_url�stremio_link�
 188  table_rowsr9�midr'�yr�qrPrQ�hrR�	rows_htmls                  r �get_dashboard_htmlrg�s�����D��I��K���K�K�M��	�	�	�1�2��J�J�L��O�	�	�	�	�B�	C��j�j�l���
 189  �
 190  ����}�N�3�G���}�N�;�L��J�
 191  ��/0��w��'�
 192  �A�f�I�q�QZ�|�]^�_i�]j�lm�nw�lx�z{�}H�{I�KL�MU�KV�0V�,��B�2�a�����:�:>�+�+�e�:L�9M�Q�\^�QR�SU�RV�VW�y�df�Ng�h%�%&�C�(���>�>C�W�E:�:=�)�)�+��G?�?@��"�v�h�G��	��(2����
 193  �#�8L�I�?;�~<E�+�F�
 194  +�^�,@�AH�@I�I
 195  �$�K��yh�hr"c�*�\rSrSrSSjrSrSrSrg)�StremioHandleric�x�[R"USS9RS5nURU5 UR	SS5 UR	SS5 UR	SS5 UR	S	[[
 196  U555 UR5 URRU5 g)
 197  N�)�indent�utf-8�Content-Typezapplication/json; charset=utf-8zAccess-Control-Allow-Origin�*zAccess-Control-Allow-Headers�Content-Length)
 198  �json�dumps�encode�
 199  send_response�send_headerr4�len�end_headers�wfile�write)�selfr�status�bodys    r �	send_json�StremioHandler.send_jsons����z�z�$�q�)�0�0��9�����6�"�����)J�K����6��<����7��=����)�3�s�4�y�>�:������
 200  �
 201  ����r"c��[RRUR5nURR	S5nUS;aUR[5 gUS:Xd4US:Xa�SURRSS5R5;a�URRSS[35n[U5RS	5nURS
 202  5 URSS5 URS
 203  [[!U555 UR#5 UR$R'U5 gUR)S5(aWUR+S5n[!U5S:�a7USR-S5(a[/5nURSU05 gUR)S5(azUR+S5n[!U5S:�aZUSR-S5(aAUSSSn[1U5nU(aURSU05 gURS00SS9 gUR)S5(a`UR+S5n[!U5S:�a@USR-S5(a'USSSn[3U5n	URSU	05 gURSS0SS9 g)N�/)r)rUz/statusr)�stremioz
 204  User-Agent�Hostzdell-ss.lan:rm��rnztext/html; charset=utf-8rpz	/catalog/��z.jsonr8z/meta/�����ri�)r{z/stream/rN�errorzResource Not Found)�urllib�parse�urlparser�rstripr}�MANIFEST�headers�get�lower�PORTrgrsrtrur4rvrwrxryr=�split�endswithr;rBrS)
 205  rz�parsedrr\r|�partsr8r:rrNs
 206            r �do_GET�StremioHandler.do_GETsh�����&�&�t�y�y�1���{�{�!�!�#�&���)�)��N�N�8�$���9�����	����AQ�AQ�R^�`b�Ac�Ai�Ai�Ak�0k��,�,�*�*�6�\�$��3H�I�K�%�k�2�9�9�'�B�D����s�#����^�-G�H����-�s�3�t�9�~�>������J�J���T�"���?�?�;�'�'��J�J�s�O�E��5�z�Q��5��8�#4�#4�W�#=�#=�)�+�������/�0���?�?�8�$�$��J�J�s�O�E��5�z�Q��5��8�#4�#4�W�#=�#=���(�3�B�-��&�w�/����N�N�F�D�>�2���N�N�F�B�<��N�<���?�?�:�&�&��J�J�s�O�E��5�z�Q��5��8�#4�#4�W�#=�#=���(�3�B�-��%�g�.�����	�7�3�4��	
 207  ����!5�6�s��Cr"c��gr�)rz�format�argss   r �log_message�StremioHandler.log_messageQs��r"r�N)r�)�__name__�
 208  __module__�__qualname__�__firstlineno__r}r�r��__static_attributes__r�r"r riris���2D�hr"ri�__main__Tz0.0.0.0z+[*] Stremio Addon Server active on 0.0.0.0:)!�__doc__�http.server�http�socketserverrqrr�urllib.parser�rYr�rrr[�dirname�__file__�DB_DIRrr�r!r;rBrSrg�server�BaseHTTPRequestHandlerrir��	TCPServer�allow_reuse_address�httpd�print�
 209  serve_foreverr�r"r �<module>r�sX�������	�����&(�g�g�n�n�5N�&O�&O�	"�UW�U\�U\�Ua�Ua�bd�bi�bi�bq�bq�rz�b{�~D�VE��
 210  �'�'�,�,�v�|�
 211  ,��%��#�_�.��x�
 212   ��)�6�	
 213  ����-���"��:�:#�JE�NA�T�[�[�7�7�A�F�z��15�L���.�	�	�	��D� 1�>�	B�e�
 214  �;�D�6�B�C�
 215  ����
 216  C�	B��	B�	B�s�D*�*
 217  D8