#!/bin/bash
DOC_ID="K1FedXsfNopQASxmEkycZQ3Vndf"
TOKEN="t-g1044dbDQFZ3RLKODM67LLT3HBO4OMC3ECGBIFIJ"
PAGE_TOKEN=""
OUTPUT_FILE="all_blocks.json"
> $OUTPUT_FILE
while true; do
  if [ -z "$PAGE_TOKEN" ]; then
    RESPONSE=$(curl -s -X GET "https://open.feishu.cn/open-apis/docx/v1/documents/$DOC_ID/blocks?page_size=500" -H "Authorization: Bearer $TOKEN")
  else
    RESPONSE=$(curl -s -X GET "https://open.feishu.cn/open-apis/docx/v1/documents/$DOC_ID/blocks?page_size=500&page_token=$PAGE_TOKEN" -H "Authorization: Bearer $TOKEN")
  fi
  echo "$RESPONSE" >> $OUTPUT_FILE
  HAS_MORE=$(echo "$RESPONSE" | jq -r '.data.has_more')
  if [ "$HAS_MORE" = "false" ]; then
    break
  fi
  PAGE_TOKEN=$(echo "$RESPONSE" | jq -r '.data.page_token')
  sleep 1
done
echo "Done"