def __init__(self, bot: MrFreeze) -> None: self.bot: MrFreeze = bot self.inkydb: Set[InkyTuple] = set() self.inkdb_path: str = f"{bot.db_prefix}/inkcyclopedia.csv" self.inkdb_enc: str = "utf-8-sig" self.airtable: Optional[Airtable] = None self.bracketmatch = re.compile(r"[{]([\w\-\s]+)[}]") # File config/airtable should have format: # base = <your base id here> # table = <your table name here> # apikey = <your api_key here> try: with open("config/airtable", "r") as airtable_file: content = [i.split("=") for i in airtable_file.readlines()] content = [[i[0].strip(), i[1].strip()] for i in content] keys = {i[0]: i[1] for i in content} self.airtable = Airtable( keys["base"], keys["table"], api_key=keys["apikey"] ) except Exception: print("Failed to open or parse ./config/airtable.") print("The Inkcyclopedia will not be able to update.")
def Pull_info(): try: at = Airtable('appEh3MpOXliHvAjw', 'NanTV_channels', api_key='keyOHaxsTGzHU9EEh') match = at.get_all(maxRecords=700, sort=['channel']) results = re.compile( "fanart': u'(.+?)'.+?link': u'(.+?)'.+?thumbnail': u'(.+?)'.+?channel': u'(.+?)'.+?summary': u'(.+?)'", re.DOTALL).findall(str(match)) for fanart, link, thumbnail, channel, summary in results: print_xml(fanart, link, thumbnail, channel, summary) except: pass
def _airtable_fallback_iterate( base: airtable.Airtable, table: str, view: Optional[str], alt_table: Optional[str]) \ -> Iterable[airtable.Record[Mapping[str, Any]]]: try: yield from base.iterate(table, view=view) return except AttributeError as error: if not alt_table: raise if not isinstance(error.__context__, requests.exceptions.HTTPError): raise except requests.exceptions.HTTPError: if not alt_table: raise yield from base.iterate(alt_table, view=view)
def get_courts_table(self, limit=None): at = Airtable(self.airtable_app, self.airtable_key) offset = '' records = [] while True: table = at.get(self.at_table, offset=offset) records += table["records"] if "offset" in table: offset = table["offset"] else: break # print(records) if limit is not None: return records[:limit] else: return records
def Pull_info(): try: at = Airtable('app4O4BNC5yEy9wNa', 'Newest_releases', api_key='keyOHaxsTGzHU9EEh') match = at.get_all(maxRecords=700, sort=['title']) results = re.compile("link5': u'(.+?)'.+?link4': u'(.+?)'.+?link1': u'(.+?)'.+?link3': u'(.+?)'.+?link2': u'(.+?)'.+?title': u'(.+?)'.+?year': u'(.+?)'",re.DOTALL).findall(str(match)) for link5,link4,link1,link3,link2,title,year in results: if "-*-" in link5: link5 = link5.replace("-*-","") if "-*-" in link4: link4 = link4.replace("-*-","") if "-*-" in link3: link3 = link3.replace("-*-","") if "-*-" in link2: link2 = link2.replace("-*-","") (thumbnail, fanart, imdb, summary) = pull_tmdb(title,year) print_xml(link5,link4,link1,link3,link2,title,year,imdb,summary,fanart,thumbnail) except:pass
class Inkcyclopedia(CogBase): """Type an ink inside {curly brackets} and I'll tell you what it looks like!""" def __init__(self, bot: MrFreeze) -> None: self.bot: MrFreeze = bot self.inkydb: Set[InkyTuple] = set() self.inkdb_path: str = f"{bot.db_prefix}/inkcyclopedia.csv" self.inkdb_enc: str = "utf-8-sig" self.airtable: Optional[Airtable] = None self.bracketmatch = re.compile(r"[{]([\w\-\s]+)[}]") # File config/airtable should have format: # base = <your base id here> # table = <your table name here> # apikey = <your api_key here> try: with open("config/airtable", "r") as airtable_file: content = [i.split("=") for i in airtable_file.readlines()] content = [[i[0].strip(), i[1].strip()] for i in content] keys = {i[0]: i[1] for i in content} self.airtable = Airtable( keys["base"], keys["table"], api_key=keys["apikey"] ) except Exception: print("Failed to open or parse ./config/airtable.") print("The Inkcyclopedia will not be able to update.") @CogBase.listener() async def on_ready(self) -> None: # Fetch inks if db does not exist if not os.path.isfile(self.inkdb_path): await self.fetch_inks() # Load up the ink db! await self.update_db() # Print that the ink database has been loaded and with how many inks. print( f"{colors.CYAN}The ink database has been loaded with " + f"{colors.MAGENTA_B}{len(self.inkydb)} inks{colors.CYAN}!{colors.RESET}" ) async def fetch_inks(self) -> None: """Fetches the latest version of the Inkcyclopedia from Airtable.""" with open(self.inkdb_path, "w", encoding=self.inkdb_enc) as inkfile: # Abort if self.airtable is not set. if self.airtable is not None: fetch = self.airtable.get_all() else: return writer = csv.writer(inkfile) for row in fetch: try: fields = row["fields"] inkname = fields["Ink Name"] to_file: List[str] = [ fields["Ink Name"], fields["RegEx"], fields["Inkbot version"] ] if "N38sjv2.jpg" not in to_file[2]: writer.writerow(to_file) except Exception: # One of the fields is missing, we can't use this row print(f"Failed to add {inkname}") pass async def update_db(self) -> None: with open(self.inkdb_path, encoding=self.inkdb_enc) as inkfile: reader = csv.reader(inkfile) self.inkydb = set() for row in reader: ink = row[0] regex = row[1] url = row[2] self.inkydb.add( InkyTuple(ink, url, re.compile(regex, re.IGNORECASE)) ) @discord.ext.commands.command(name="inkupdate") @discord.ext.commands.check(checks.is_owner) async def inkupdate(self, ctx: Context) -> None: await self.fetch_inks() await self.update_db() await ctx.send( f"There are now {len(self.inkydb)} inks in the database!") self.log_command( ctx, f"Inkcyclopedia updated, now has {len(self.inkydb)} entries.") @CogBase.listener() async def on_message(self, message: Message) -> None: matches: List[str] = self.bracketmatch.findall(message.content) # Stop the function if message was sent by a bot or contains no matches if message.author.bot or not matches: return for match in matches: for ink in self.inkydb: if ink.regex.findall(match): image = discord.Embed() image.set_image(url=ink.url) await message.channel.send( f"Found a match for {ink.name}!", embed=image) # Only return the first hit, then return. return
from django.shortcuts import render, redirect from django.contrib import messages from airtable.airtable import Airtable import os AT = Airtable('apppzWhomfy2IdWfl', 'Movies', api_key='keyJZjCM7eJRvrdPE') # Create your views here. def home_page(request): user_query = (str(request.GET.get('username', ''))) search_result = AT.get_all(formula="FIND('" + user_query.lower() + "', LOWER({Name}))") stuff_for_frentend = {'search_result': search_result} return render(request, 'movie_app/movie_stuff.html', stuff_for_frentend) def create(request): if request.method == 'POST': data = { 'Name': request.POST.get('name'), 'Pictures': [{ 'url': request.POST.get('url') }], 'Rating': int(request.POST.get('rating')), 'Notes': request.POST.get('notes') } response = AT.insert(data) messages.success(
import datetime from airtable.airtable import Airtable from twilio.rest import Client from os.path import join, dirname from dotenv import get_variables dotenv_path = join(dirname(__file__), '.env') env = get_variables(dotenv_path) airtable_client = Airtable(env['AIRTABLE_BASE_ID'],env['AIRTABLE_API_KEY']) def text_reminder(client, records): return def archive_records(client, table_name, records): return
def setUp(self): self.base_id = FAKE_BASE_ID self.api_key = FAKE_API_KEY self.airtable = Airtable(self.base_id, self.api_key)
class TestAirtable(unittest.TestCase): def setUp(self): self.base_id = FAKE_BASE_ID self.api_key = FAKE_API_KEY self.airtable = Airtable(self.base_id, self.api_key) def test_build_base_url(self): self.assertEqual(self.airtable.base_url, 'https://api.airtable.com/v0/app12345') def test_build_headers(self): self.assertEqual(self.airtable.headers['Authorization'], 'Bearer fake_api_key') @mock.patch.object(requests, 'request') def test_get_all(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'records': [ { 'id': 'reccA6yaHKzw5Zlp0', 'fields': { 'Name': 'John', 'Number': '(987) 654-3210' } }, { 'id': 'reccg3Kke0QvTDW0H', 'fields': { 'Name': 'Nico', 'Number': '(123) 222-1131' } } ], 'offset': 'reccg3Kke0QvTDW0H' } mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME) self.assertEqual(len(r['records']), 2) self.assertEqual(r['offset'], 'reccg3Kke0QvTDW0H') @mock.patch.object(requests, 'request') def test_order_of_fields_is_preserved(self, mock_request): mock_response = requests.Response() mock_response.status_code = 200 # Set the text content of the response to a JSON string so we can test # how it gets deserialized mock_response._content = '''{ "records": [ { "id": "reccA6yaHKzw5Zlp0", "fields": { "a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9, "j": 10, "k": 11, "l": 12, "m": 13 } }, { "id": "reccg3Kke0QvTDW0H", "fields": { "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26 } } ] }''' mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME) self.assertEqual(r['records'][0]['fields'].keys(), list(u'abcdefghijklm')) self.assertEqual(r['records'][1]['fields'].keys(), list(u'nopqrstuvwxyz')) @mock.patch.object(requests, 'request') def test_get_by_id(self, mock_request): fake_id = 'reccA6yaHKzw5Zlp0' mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'id': 'reccA6yaHKzw5Zlp0', 'fields': { 'Name': 'John', 'Number': '(987) 654-3210' } } mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, fake_id) self.assertEqual(r['id'], fake_id) @mock.patch.object(requests, 'request') def test_get_not_found(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 404 mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, '123') self.assertEqual(r['error']['code'], 404) def test_invalid_get(self): with self.assertRaises(airtable.IsNotString): self.airtable.get(FAKE_TABLE_NAME, 123) self.airtable.get(FAKE_TABLE_NAME, offset=123) with self.assertRaises(airtable.IsNotInteger): self.airtable.get(FAKE_TABLE_NAME, limit='1') @mock.patch.object(requests, 'request') def test_delete(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'deleted': True, 'id': '1234' } mock_request.return_value = mock_response r = self.airtable.delete(FAKE_TABLE_NAME, '1234') self.assertTrue(r['deleted']) self.assertEqual(r['id'], '1234') def test_invalid_delete(self): with self.assertRaises(airtable.IsNotString): self.airtable.delete(FAKE_TABLE_NAME, 123)
#algorithm for counting scores #if we see a +, then we add with the score weight, else we subtract if it is - def countScores(scores, scoreweight, newchart): for j in range(1, len(newchart[0])): for i in range(0, len(newchart)): if (newchart[i][j] == '+'): scores[j] = int(scores[j]) + scoreweight[i] elif (newchart[i][j] == '-'): scores[j] = int(scores[j]) - scoreweight[i] #print(scores) #print(scores) tablename = input("Input name of table (cAsE sENsiTiVE): ") airtable = Airtable('base_key', tablename, api_key='api_key') #create dictionary, key is solution name, value is list containing recorded data from each category data = {} accept = input("Names of Columns to take (cAsE sENsiTiVE): ") solncol = input("Input name of solution column (cAsE sENsiTiVE): ") #obtain the weights that the user wants, store them in score weight scoreweightin = input( "Weights of columns (negative values to min, seperate by commas): ") scoreweight = list(map(int, scoreweightin.split(","))) categories = []
class TestAirtable(unittest.TestCase): def setUp(self): self.base_id = FAKE_BASE_ID self.api_key = FAKE_API_KEY self.airtable = Airtable(self.base_id, self.api_key) def test_build_base_url(self): self.assertEqual(self.airtable.base_url, 'https://api.airtable.com/v0/app12345') def test_build_headers(self): self.assertEqual(self.airtable.headers['Authorization'], 'Bearer fake_api_key') @mock.patch.object(requests, 'request') def test_get_all(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'records': [ { 'id': 'reccA6yaHKzw5Zlp0', 'fields': { 'Name': 'John', 'Number': '(987) 654-3210' } }, { 'id': 'reccg3Kke0QvTDW0H', 'fields': { 'Name': 'Nico', 'Number': '(123) 222-1131' } } ], 'offset': 'reccg3Kke0QvTDW0H' } mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME) self.assertEqual(len(r['records']), 2) self.assertEqual(r['offset'], 'reccg3Kke0QvTDW0H') @mock.patch.object(requests, 'request') def test_order_of_fields_is_preserved(self, mock_request): mock_response = requests.Response() mock_response.status_code = 200 # Set the text content of the response to a JSON string so we can test # how it gets deserialized mock_response._content = '''{ "records": [ { "id": "reccA6yaHKzw5Zlp0", "fields": { "a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9, "j": 10, "k": 11, "l": 12, "m": 13 } }, { "id": "reccg3Kke0QvTDW0H", "fields": { "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26 } } ] }''' mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME) self.assertEqual(r['records'][0]['fields'].keys(), list(u'abcdefghijklm')) self.assertEqual(r['records'][1]['fields'].keys(), list(u'nopqrstuvwxyz')) @mock.patch.object(requests, 'request') def test_get_by_id(self, mock_request): fake_id = 'reccA6yaHKzw5Zlp0' mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'id': 'reccA6yaHKzw5Zlp0', 'fields': { 'Name': 'John', 'Number': '(987) 654-3210' } } mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, fake_id) self.assertEqual(r['id'], fake_id) @mock.patch.object(requests, 'request') def test_get_not_found(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 404 mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, '123') self.assertEqual(r['error']['code'], 404) @mock.patch.object(requests, 'request') def test_get_view(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = {'records': []} mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, view='My view') mock_request.assert_called_once_with( 'GET', 'https://api.airtable.com/v0/app12345/TableName', data=None, headers={'Authorization': 'Bearer fake_api_key'}, params={'view': 'My view'}) @mock.patch.object(requests, 'request') def test_get_filter_by_formula(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = {'records': []} mock_request.return_value = mock_response r = self.airtable.get(FAKE_TABLE_NAME, filter_by_formula="{title} = ''") mock_request.assert_called_once_with( 'GET', 'https://api.airtable.com/v0/app12345/TableName', data=None, headers={'Authorization': 'Bearer fake_api_key'}, params={'filterByFormula': "{title} = ''"}) def test_invalid_get(self): with self.assertRaises(airtable.IsNotString): self.airtable.get(FAKE_TABLE_NAME, 123) self.airtable.get(FAKE_TABLE_NAME, offset=123) with self.assertRaises(airtable.IsNotInteger): self.airtable.get(FAKE_TABLE_NAME, limit='1') @mock.patch.object(requests, 'request') def test_delete(self, mock_request): mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.json.return_value = { 'deleted': True, 'id': '1234' } mock_request.return_value = mock_response r = self.airtable.delete(FAKE_TABLE_NAME, '1234') self.assertTrue(r['deleted']) self.assertEqual(r['id'], '1234') def test_invalid_delete(self): with self.assertRaises(airtable.IsNotString): self.airtable.delete(FAKE_TABLE_NAME, 123)