import requests
from pyairtable import Table
import json
import time
class WixToAirtableSync:
def __init__(self, wix_api_key, airtable_api_key, airtable_base_id, airtable_table_name):
"""
Initialize the sync tool with API credentials and Airtable setup details.
:param wix_api_key: Wix API key for accessing bookings.
:param airtable_api_key: Airtable API key for writing records.
:param airtable_base_id: The ID of the Airtable base where reservations will be synced.
:param airtable_table_name: The name of the table in Airtable where reservations will be inserted.
"""
self.wix_api_key = wix_api_key
self.airtable_api_key = airtable_api_key
self.airtable_base_id = airtable_base_id
self.airtable_table_name = airtable_table_name
self.airtable_table = Table(airtable_api_key, airtable_base_id, airtable_table_name)
def get_wix_reservations(self, limit=100, offset=0):
"""
Fetch reservations from the Wix API.
:param limit: The maximum number of records to retrieve per request.
:param offset: Offset for pagination, to fetch the next set of records.
:return: JSON response from the Wix API containing the reservations.
"""
url = "https://www.wixapis.com/bookings/v2/appointments"
headers = {
"Authorization": f"Bearer {self.wix_api_key}",
"Content-Type": "application/json"
}
params = {
"limit": limit,
"offset": offset
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status() # Raise an error for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request to Wix API failed: {e}")
return None
def sync_reservations_to_airtable(self, wix_reservations):
"""
Sync the fetched Wix reservations to the specified Airtable.
:param wix_reservations: A list of reservation objects from the Wix API.
"""
for reservation in wix_reservations.get('appointments', []):
# Map Wix reservation fields to Airtable fields
# NOTE: Adjust field names according to your Airtable setup
airtable_record = {
"Name": reservation.get('title', 'Untitled Reservation'),
"Start Date": reservation.get('startDate'),
"End Date": reservation.get('endDate'),
"Status": reservation.get('status', 'Unknown'),
# Add more fields as necessary
}
try:
self.airtable_table.insert(airtable_record)
print(f"Successfully synced: {reservation.get('title', 'Untitled')}")
except Exception as e:
print(f"Failed to sync {reservation.get('title', 'Untitled')}: {e}")
def handle_pagination(self, wix_reservations):
"""
Check if there are more pages of reservations and fetch them recursively.
:param wix_reservations: The response from the Wix API, which may contain a nextPageToken.
"""
if 'nextPageToken' in wix_reservations:
next_offset = wix_reservations['nextPageToken']
print("Fetching next page of reservations...")
next_page_reservations = self.get_wix_reservations(offset=next_offset)
if next_page_reservations:
self.sync_reservations_to_airtable(next_page_reservations)
self.handle_pagination(next_page_reservations) # Recursive call for the next page
def run_sync(self):
"""
Main method to initiate the sync process.
"""
wix_reservations = self.get_wix_reservations()
if wix_reservations:
self.sync_reservations_to_airtable(wix_reservations)
self.handle_pagination(wix_reservations) # Handle pagination if there are more records
else:
print("No reservations found or failed to fetch.")
# Example Usage
if __name__ == "__main__":
wix_api_key = "YOUR_WIX_API_KEY"
airtable_api_key = "YOUR_AIRTABLE_API_KEY"
airtable_base_id = "YOUR_AIRTABLE_BASE_ID"
airtable_table_name = "YOUR_AIRTABLE_TABLE_NAME"
sync_tool = WixToAirtableSync(wix_api_key, airtable_api_key, airtable_base_id, airtable_table_name)
sync_tool.run_sync()