Help

Select by datetime using Python requests and format output

Topic Labels: API
1950 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Bill_Best
4 - Data Explorer
4 - Data Explorer

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.

1 Reply 1
Bill_Best
4 - Data Explorer
4 - Data Explorer

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.