#!/usr/bin/env python3 """Upload 'The Sufi Who Fooled the Algorithm' to BookStack.""" import os, sys, json, subprocess, time, urllib.request BS_ID = "is2FJhIWpKEIY13qxHsmboYqukpgRn0U" BS_SECRET = "yQdxQaAKtTatZEeOLAS92umUfRNBqQFq" BS_BASE = "https://docs.falahos.my/api" CHAPTERS_DIR = os.path.expanduser("~/odysseus_book/shah_kiyosaki/chapters") PDF_PATH = os.path.expanduser("~/odysseus_book/shah_kiyosaki/pdf/The_Sufi_Who_Fooled_the_Algorithm.pdf") CHAPTERS = [ "The Man Who Watched the Shadow", "Nasrudin's Lost Key", "The Man Who Ate the Menu", "The Commanding Self", "Learning How to Unlearn", "The Assumption of Knowledge", "The Teacher Who Refused to Teach", "The Parrot and the Scholar", "The Dream That Woke the Dreamer", "The Story That Has No Ending", ] def api_call(method, path, data=None): url = f"{BS_BASE}/{path}" headers = { "Authorization": f"Token {BS_ID}:{BS_SECRET}", "User-Agent": "curl/7.88.1", "Content-Type": "application/json", } body = json.dumps(data).encode() if data else None req = urllib.request.Request(url, data=body, headers=headers, method=method) with urllib.request.urlopen(req, timeout=30) as r: return json.loads(r.read()) def markdown_to_html(md): text = md.replace('&', '&').replace('<', '<').replace('>', '>') parts = text.split('**') for i in range(1, len(parts), 2): parts[i] = f'{parts[i]}' text = ''.join(parts) parts = text.split('*') for i in range(1, len(parts), 2): parts[i] = f'{parts[i]}' text = ''.join(parts) text = text.replace('\n', '
\n') return text def main(): print("šŸ“š Creating BookStack book...") book_data = { "name": "The Sufi Who Fooled the Algorithm: Idries Shah for the Attention Age", "description": "Kiyosaki-Style Ɨ Shah's Teaching Stories Ɨ Achebe Oral Tradition — 10 Teaching Stories Reimagined for AI, Anxiety, and the Art of Not Being Fooled. Dual mentors: Shah (Idries Shah) + CFO (Modern CFO). By Jauhari Che Wan.", "tags": [ {"name": "Series", "value": "Shah Ɨ Kiyosaki Ɨ Achebe"}, {"name": "Author", "value": "Jauhari Che Wan"}, {"name": "Shelf", "value": "721"} ] } result = api_call("POST", "books", book_data) book_id = result.get("id") print(f" Book ID: {book_id}") print("šŸ“– Uploading chapters...") for i, title in enumerate(CHAPTERS): fname = f"Chapter_0{i+1}.md" if i < 9 else f"Chapter_{i+1}.md" fpath = os.path.join(CHAPTERS_DIR, fname) if not os.path.exists(fpath): print(f" WARNING: {fname} not found") continue with open(fpath) as f: content = f.read() lines = content.split('\n') body_start = 0 for j, line in enumerate(lines): if line.startswith('# ') or line.startswith('## ') or line.strip() == '': continue else: body_start = j break body = '\n'.join(lines[body_start:]) html_body = markdown_to_html(body) page_name = f"Chapter {i+1}: {title}" print(f" Uploading {page_name}...") data = { "book_id": book_id, "name": page_name, "html": html_body, "priority": i + 1, "tags": [{"name": "Part", "value": f"Chapter {i+1}"}] } try: page_result = api_call("POST", "pages", data) print(f" Page ID: {page_result.get('id')}") except Exception as e: print(f" Error: {str(e)[:100]}") time.sleep(0.3) print(f"\nāœ… BookStack upload complete! Book ID: {book_id}") if __name__ == "__main__": main()