import socket
# Force IPv4
old_getaddrinfo = socket.getaddrinfo
def new_getaddrinfo(*args, **kwargs):
    responses = old_getaddrinfo(*args, **kwargs)
    return [response for response in responses if response[0] == socket.AF_INET]
socket.getaddrinfo = new_getaddrinfo

import json
from google.oauth2 import service_account
from googleapiclient.discovery import build

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

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

document_id = '1xiB5pmqTOSch_VJh735mYHLWzCdBKWNNBFo0VCynBV8'

with open('extracted_11_20.txt', 'r', encoding='utf-8') as f:
    text_content = f.read()

# Add newlines at the start to separate from existing content
text_to_insert = '\n\n' + text_content + '\n'

requests = [
    {
        'insertText': {
            'endOfSegmentLocation': {},
            'text': text_to_insert
        }
    }
]

try:
    result = docs_service.documents().batchUpdate(documentId=document_id, body={'requests': requests}).execute()
    print("Success")
except Exception as e:
    print(f"Error: {e}")
