airtable = Airtable(test_base, test_table, api_key=os.environ["AIRTABLE_API_KEY"]) # Insert rec = airtable.insert({"text": "A", "number": 1, "boolean": True}) # Get assert airtable.get(rec["id"]) # Update rv = airtable.update(rec["id"], {"text": "B"}) assert rv["fields"]["text"] == "B" # Replace rv = airtable.replace(rec["id"], {"text": "C"}) assert rv["fields"]["text"] == "C" # Get all assert airtable.get_all() # Delete assert airtable.delete(rec["id"]) # Batch Insert records = airtable.batch_insert([{ "text": "A", "number": 1, "boolean": True } for _ in range(100)])
from airtable import Airtable #intilize airtable1 = Airtable(api_key='key7BnLq2O4dACj81', table_name='leading_ads', base_key='appPzGUWNjmOLCJqG') #for loop on list for info in airtable1.get_all(): print(info['fields']) #update record airtable1.update(info['id'], {'Profile_name': 'Ready'}) #replace whole row fields = {'PassangerName': 'Mike', 'Passport': 'YASD232-23'} airtable1.replace(record['id'], fields) #or send the dic #saving screen shots dbx = dropbox.Dropbox( 'key') def save_screenshot(driver, profile_name, row_id): filename = profile_name + ".png" driver.save_screenshot(f'Screenshots/{filename}') with open(f'Screenshots/{filename}', 'rb') as file: ss = file.read()
def copy_to_airtable(self): """ copies the DB to airtables """ try: logger.debug('Starting Airtable copy...') # set the ready state to false to indicate that it's not ready state_table = Airtable(self.airtable_basekey, 'State', self.airtable_apikey) ready_id = state_table.match('key', 'ready')['id'] fields = {'key': 'ready', 'value': 'false'} state_table.replace(ready_id, fields) # delete previous table entries logger.debug("Deleting previous table entries...") pilot_table = Airtable(self.airtable_basekey, 'Pilots', self.airtable_apikey) character_table = Airtable(self.airtable_basekey, 'Characters', self.airtable_apikey) attribute_table = Airtable(self.airtable_basekey, 'Attributes', self.airtable_apikey) attribute_groups_table = Airtable(self.airtable_basekey, 'AttributeGroups', self.airtable_apikey) pilot_table.batch_delete( [entry['id'] for entry in pilot_table.get_all()]) character_table.batch_delete( [entry['id'] for entry in character_table.get_all()]) attribute_table.batch_delete( [entry['id'] for entry in attribute_table.get_all()]) attribute_groups_table.batch_delete( [entry['id'] for entry in attribute_groups_table.get_all()]) logger.debug("Previous table entries deleted!") # copy pilots table logger.debug("Copying Pilots table...") pilots = session.query(Pilot) pilot_records = [] for pilot in pilots: pilot_records.append({ 'id': pilot.id, 'discord_id': pilot.discord_id, 'discord_name': pilot.discord_name, 'discord_discriminator': pilot.discord_discriminator }) pilot_table.batch_insert(pilot_records) logger.debug("Pilots table copied!") # copy characters table logger.debug("Copying Characters table...") characters = session.query(Character) character_records = [] for character in characters: character_records.append({ 'id': character.id, 'pilot_id': character.pilot_id, 'name': character.name }) character_table.batch_insert(character_records) logger.debug("Characters table copied!") # copy attributes logger.debug("Copying Attribute table...") attributes = session.query(Attribute) attribute_records = [] for attribute in attributes: attribute_records.append({ 'id': attribute.id, 'attribute_group_id': attribute.attribute_group_id, 'key': attribute.key, 'value': attribute.value, 'friendly_name': attribute.friendly_name }) attribute_table.batch_insert(attribute_records) logger.debug("Attribute table copied!") # copy attributegroups logger.debug("Copying AttributeGroup table...") attribute_groups = session.query(AttributeGroup) attribute_group_records = [] for attribute_group in attribute_groups: attribute_group_records.append({ 'id': attribute_group.id, 'pilot_id': attribute_group.pilot_id, 'name': attribute_group.name, 'description': attribute_group.description }) attribute_groups_table.batch_insert(attribute_group_records) logger.debug("AttributeGroup table copied!") # set the ready state to true to indicate that it's ready state_table = Airtable(self.airtable_basekey, 'State', self.airtable_apikey) ready_id = state_table.match('key', 'ready')['id'] fields = {'key': 'ready', 'value': 'true'} state_table.replace(ready_id, fields) logger.debug('Airtable copy complete!') except: logger.error( f"Failed to copy to airtable:\n{traceback.format_exc()}")
dns_short_name = dns_short_name.replace('-demo', 'Demo') dns_short_name = dns_short_name.replace('-hkdemo', 'HKDemo') row = { 'ShortName': dns_short_name, 'Name': dns_name, 'IP': dns_content, 'Environment': dns_env, 'LastUpdated': str(datetime.utcnow()), 'ModifiedOn': dns_modified_on } # Search by name print('* Update environment for record %s' % dns_name) exist_row = airtable.search('Name', dns_name) if len(exist_row) == 0: # Insert new row airtable.insert(row) elif len(exist_row) == 1: last_modified = datetime.strptime(exist_row[0]['fields']['ModifiedOn'], '%Y-%m-%dT%H:%M:%S.%fZ') current_modified = datetime.strptime(dns_modified_on, '%Y-%m-%dT%H:%M:%S.%fZ') # Only update the row when dns record was modified. if last_modified < current_modified: # Replace exist row with new one airtable.replace(exist_row[0]['id'], row) else: exit('Airtable has multiple rows with name %s' % dns_name) print('Updated!') exit(0)