#!/usr/bin/env python3
import os
import json
import requests
from google.oauth2 import service_account
from googleapiclient.discovery import build

SERVICE_ACCOUNT_FILE = 'google_service_account.json'
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/documents']

url = 'https://docs.google.com/document/d/18Z7qIqBq8lV7vcSa7F943CwId3IhP8vrWgXpzUVa7UI/edit'
doc_id = url.split('/d/')[1].split('/')[0]

creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
docs_service = build('docs', 'v1', credentials=creds)

doc = docs_service.documents().get(documentId=doc_id).execute()

# Extract paragraphs (let's get first ~15,000 chars, roughly 10 pages)
paragraphs = []
char_count = 0
content = doc.get('body', {}).get('content', [])

for elem in content:
    if 'paragraph' in elem:
        para_text = ""
        for run in elem.get('paragraph', {}).get('elements', []):
            if 'textRun' in run:
                para_text += run['textRun']['content']
        para_text = para_text.strip()
        if para_text:
            paragraphs.append(para_text)
            char_count += len(para_text)
        if char_count > 15000:
            break

print(f"Extracted {len(paragraphs)} paragraphs, total {char_count} chars.")

# Save to local file
with open('original_10_pages.txt', 'w', encoding='utf-8') as f:
    for p in paragraphs:
        f.write(p + '\n\n')

print("Saved to original_10_pages.txt")