pagination

This commit is contained in:
Karol Wypchlo 2021-03-01 23:08:38 +01:00
parent 6af13b4715
commit 002ae6c6d8
1 changed files with 23 additions and 14 deletions

View File

@ -6,26 +6,35 @@ from bot_utils import setup, send_msg
bot_token = setup() bot_token = setup()
client = discord.Client() client = discord.Client()
AIRTABLE_TABLE = "app89plJvA9EqTJEc"
AIRTABLE_FIELD = "Link"
AIRTABLE_API_KEY = os.getenv('AIRTABLE_API_KEY') AIRTABLE_API_KEY = os.getenv('AIRTABLE_API_KEY')
AIRTABLE_TABLE = os.getenv('AIRTABLE_TABLE', 'app89plJvA9EqTJEc')
AIRTABLE_FIELD = os.getenv('AIRTABLE_FIELD', 'Link')
async def block_skylinks_from_airtable(): async def block_skylinks_from_airtable():
print("Pulling blocked skylinks from airtable via api integration") print("Pulling blocked skylinks from airtable via api integration")
headers = { "Authorization": "Bearer " + AIRTABLE_API_KEY } headers = { "Authorization": "Bearer " + AIRTABLE_API_KEY }
skylinks = []
offset = ''
while len(skylinks) == 0 or offset:
query = '&'.join(['fields%5B%5D=' + AIRTABLE_FIELD, ('offset=' + offset) if offset else ''])
airtable = requests.get( airtable = requests.get(
"https://api.airtable.com/v0/" + AIRTABLE_TABLE + "/Table%201?fields%5B%5D=" + AIRTABLE_FIELD, headers=headers "https://api.airtable.com/v0/" + AIRTABLE_TABLE + "?" + query, headers=headers
) )
if airtable.status_code != 200: if airtable.status_code != 200:
message = "Airtable blocklist integration responded with code " + str(airtable.status_code) + ": " + (airtable.text or "empty response") message = "Airtable blocklist integration responded with code " + str(airtable.status_code) + ": " + (airtable.text or "empty response")
return print(message) or await send_msg(client, message, force_notify=True) return print(message) or await send_msg(client, message, force_notify=True)
skylinks = [entry['fields'][AIRTABLE_FIELD] for entry in airtable.json()['records']] airtable_data = airtable.json()
skylinks = skylinks + [entry['fields'][AIRTABLE_FIELD] for entry in airtable_data['records']]
if len(skylinks) == 0: if len(skylinks) == 0:
return print("Airtable returned 0 skylinks to block - make sure your table configuration is correct") return print("Airtable returned 0 skylinks - make sure your configuration is correct")
else:
print(airtable_data.offset)
offset = airtable_data.offset
print(airtable_data.offset)
print("Airtable returned " + str(len(skylinks)) + " skylinks to block") print("Airtable returned " + str(len(skylinks)) + " skylinks to block")
apipassword = os.popen('docker exec sia cat /sia-data/apipassword').read().strip() apipassword = os.popen('docker exec sia cat /sia-data/apipassword').read().strip()