first commit
This commit is contained in:
59
get_records.py
Normal file
59
get_records.py
Normal file
@ -0,0 +1,59 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import api
|
||||
import config
|
||||
import logging
|
||||
import json
|
||||
|
||||
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
|
||||
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
|
||||
|
||||
def get_all_records(client: api.Client, access_token: str, app_token: str, table_id: str):
|
||||
"""Retrieve all records from a given table, handling pagination if necessary."""
|
||||
records = []
|
||||
page_token = None
|
||||
while True:
|
||||
resp = client.get_records_list(access_token, app_token, table_id, page_token=page_token)
|
||||
items = resp.get('items', [])
|
||||
if items:
|
||||
records.extend(items)
|
||||
if resp.get('has_more'):
|
||||
page_token = resp.get('page_token')
|
||||
else:
|
||||
break
|
||||
return records
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
client = api.Client(config.LARK_HOST)
|
||||
|
||||
# Get tenant access token
|
||||
try:
|
||||
access_token = client.get_tenant_access_token(config.APP_ID, config.APP_SECRET)
|
||||
except Exception as e:
|
||||
logging.error(f"Could not get access token: {e}")
|
||||
exit(1)
|
||||
|
||||
logging.info(f"Using App ID: {config.APP_ID}")
|
||||
|
||||
# Please replace these with your actual app_token and table_id
|
||||
APP_TOKEN = config.DEFAULT_APP_TOKEN
|
||||
TABLE_ID = config.DEFAULT_TABLE_ID
|
||||
|
||||
if APP_TOKEN == "your_app_token_here" or TABLE_ID == "your_table_id_here":
|
||||
logging.warning("Please specify APP_TOKEN and TABLE_ID in the script to get records.")
|
||||
logging.info("For example, you can edit this script to set APP_TOKEN and TABLE_ID, then run it again.")
|
||||
else:
|
||||
logging.info(f"Fetching records for App Token: {APP_TOKEN}, Table ID: {TABLE_ID}")
|
||||
records = get_all_records(client, access_token, APP_TOKEN, TABLE_ID)
|
||||
logging.info(f"Successfully retrieved {len(records)} records.")
|
||||
|
||||
# Print the first record as an example of the structure
|
||||
if records:
|
||||
print("\nExample Record (First item):")
|
||||
print(json.dumps(records[0], ensure_ascii=False, indent=2))
|
||||
|
||||
# Example to use batch_get_records if you have specific record IDs:
|
||||
# record_ids = [r['record_id'] for r in records[:5]] # get first 5 ids
|
||||
# if record_ids:
|
||||
# batch_records = client.batch_get_records(access_token, APP_TOKEN, TABLE_ID, record_ids)
|
||||
# logging.info(f"Batch GET returned {len(batch_records)} records.")
|
||||
Reference in New Issue
Block a user