May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import requests
|
||||
|
||||
# Stałe konfiguracyjne
|
||||
API_KEY = 'patXWerdvwWJHRGBz.26eb663a53b03fa137ad219857cc935c514340086681d2ef8d762063bd3153cb' # Zamień na swój klucz API
|
||||
BASE_ID = 'appSTkCUy8IPoutrO' # Zamień na swój identyfikator bazy danych
|
||||
TABLE_NAME = 'tbl1Q8MNq8EGCzOHp' # Zamień na nazwę swojej tabeli
|
||||
|
||||
def record_exists(api_key, base_id, table_name, link):
|
||||
"""
|
||||
Sprawdza, czy rekord z podanym linkiem już istnieje w Airtable.
|
||||
|
||||
:param api_key: Klucz API Airtable
|
||||
:param base_id: Identyfikator bazy danych
|
||||
:param table_name: Nazwa tabeli
|
||||
:param link: Link do sprawdzenia
|
||||
:return: True, jeśli rekord istnieje, False w przeciwnym razie
|
||||
"""
|
||||
url = f'https://api.airtable.com/v0/{base_id}/{table_name}'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {api_key}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
params = {
|
||||
'filterByFormula': f'{{Link}}="{link}"'
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=headers, params=params)
|
||||
|
||||
if response.status_code == 200:
|
||||
records = response.json().get('records', [])
|
||||
return len(records) > 0
|
||||
else:
|
||||
print(f"Nie udało się sprawdzić rekordu. Status: {response.status_code}")
|
||||
print(response.json())
|
||||
return False
|
||||
|
||||
def add_record_to_airtable(wydawnictwo, link):
|
||||
"""
|
||||
Dodaje rekord do tabeli Airtable, jeśli nie istnieje już rekord z tym linkiem.
|
||||
|
||||
:param wydawnictwo: Wydawnictwo do dodania
|
||||
:param link: Link do dodania
|
||||
:return: Odpowiedź z API Airtable lub komunikat o istnieniu rekordu
|
||||
"""
|
||||
response = None # Inicjalizowanie zmiennej `response`
|
||||
|
||||
if not record_exists(API_KEY, BASE_ID, TABLE_NAME, link):
|
||||
url = f'https://api.airtable.com/v0/{BASE_ID}/{TABLE_NAME}'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {API_KEY}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
data = {
|
||||
"fields": {
|
||||
"Wydawnictwo": wydawnictwo,
|
||||
"Link": link,
|
||||
"Status": "Nowy"
|
||||
}
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
|
||||
if response.status_code == 201:
|
||||
print("Rekord został dodany pomyślnie.")
|
||||
print(response.json()) # Wyświetla odpowiedź API (nowo dodany rekord)
|
||||
else:
|
||||
print(f"Nie udało się dodać rekordu. Status: {response.status_code}")
|
||||
print(response.json()) # Wyświetla szczegóły błędu
|
||||
else:
|
||||
print("Rekord z tym linkiem już istnieje.")
|
||||
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user