The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.
Aug 31, 2020 09:44 AM
I’m accessing the Airtable API using a Python script below:
import requests
from pprint import pprint
from airtable import Airtable
base_key = 'BASE_KEY'
table_name = 'Events List'
api_key = 'API_KEY'
airtable = Airtable(base_key, table_name, api_key)
pages = airtable.get_iter(maxRecords=3, sort=["Date"])
for page in pages:
for record in page:
pprint(record)
The above successfully retrieves the first 3 records in the table as follows:
{'createdTime': '2020-08-15T10:04:49.000Z',
'fields': {'Date': '2020-08-15T19:00:00.000Z',
'Description': 'Lorem ipsum dolor sit amet,',
'ID': 20,
'Title': 'Proin laoreet facilisis cursus',
'URL': 'https://www.example1.com/'},
'id': 'rec4oT3PiXXXXX9ph'}
{'createdTime': '2020-08-15T10:05:50.000Z',
'fields': {'Date': '2020-08-15T20:00:00.000Z',
'Description': 'Pellentesque viverra gravida lectus',
'ID': 21,
'Title': 'Curabitur porttitor vehicula urna',
'URL': 'https://www.example2.com/'},
'id': 'recO21GCXXXXX2pv1'}
{'createdTime': '2020-08-15T19:40:26.000Z',
'fields': {'Date': '2020-08-17T17:00:00.000Z',
'Description': 'Aliquam condimentum iaculis odio',
'ID': 23,
'Title': 'Suspendisse aliquet porta risus',
'URL': 'https://www.example2.com/'},
'id': 'recvduwKBFXXXXX8j'}
I want to do a number of things with the data:
a) I want to retrieve all records where (‘Date’ >= NOW) to pull up events that are in the future.
b) I would like to order the records retrieved by ‘Date’
c) I would like to format the data retrieved - something like this:
Date: '2020-08-15T19:00:00.000Z'
Description: 'Lorem ipsum dolor sit amet'
Title: 'Proin laoreet facilisis cursus'
URL: 'https://www.example1.com/'
Date: '2020-08-15T20:00:00.000Z'
Description: 'Pellentesque viverra gravida lectus'
Title: 'Curabitur porttitor vehicula urna'
URL: 'https://www.example2.com/'
Date': '2020-08-17T17:00:00.000Z'
Description': 'Aliquam condimentum iaculis odio'
Title': 'Suspendisse aliquet porta risus'
URL': 'https://www.example2.com/'
I can filter out unwanted fields using the following:
at.get(table_name, table_name, record_id=None, limit=0, offset=None,
filter_by_formula=None, view=None, max_records=0, fields=[])
I just need a hint about how to filter on the date field and how to format the outputted data.
Hope you can help and thanks in advance.
Aug 31, 2020 02:52 PM
The following retrieves the records where (‘Date’ >= NOW) and orders the records retrieved by ‘Date’:
pages = airtable.get_iter(maxRecords=3, formula="Date >= NOW()", sort=["Date"], fields=('Date', 'Title', 'Description'))
The above gives me the following:
{'createdTime': '2020-08-27T08:23:09.000Z',
'fields': {'Date': '2020-09-02T19:00:00.000Z',
'Description': 'Lorem ipsum dolor sit amet",
'Title': 'Proin laoreet facilisis cursus'},
'id': 'rec4oT3PiXXXXX9ph'}
{'createdTime': '2020-08-27T08:28:20.000Z',
'fields': {'Date': '2020-09-03T08:00:00.000Z',
'Description': 'Pellentesque viverra gravida lectus',
'Title': 'Curabitur porttitor vehicula urna'},
'id': 'reck4DyBmip3GJ9V0'}
{'createdTime': '2020-08-27T08:31:07.000Z',
'fields': {'Date': '2020-09-03T08:30:00.000Z',
'Description': 'Aliquam condimentum iaculis odio',
'Title': 'Suspendisse aliquet porta risus'},
'id': 'recgyHXNlcl8Y14Gx'}
The next stage is to format the results to be more readable and to strip out the ‘createdTime’ and ‘id’ fields.