Spaces:
Running
Running
Update apis/Bubble_API_Calls.py
Browse files- apis/Bubble_API_Calls.py +72 -21
apis/Bubble_API_Calls.py
CHANGED
@@ -91,7 +91,28 @@ def fetch_linkedin_token_from_bubble(url_user_token_str: str):
|
|
91 |
print(status_message) # Log the final status message
|
92 |
return parsed_token_dict
|
93 |
|
94 |
-
def fetch_linkedin_posts_data_from_bubble(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
bubble_api_key = os.environ.get("Bubble_API")
|
96 |
if not bubble_api_key:
|
97 |
error_msg = "❌ Bubble API Error: The 'Bubble_API' environment variable is not set."
|
@@ -99,34 +120,64 @@ def fetch_linkedin_posts_data_from_bubble(constraint_value: str, data_type:str,
|
|
99 |
return None, error_msg
|
100 |
|
101 |
base_url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}"
|
|
|
|
|
|
|
102 |
constraints = [{"key": constraint_key, "constraint_type": constraint_type, "value": constraint_value}]
|
103 |
if additional_constraints:
|
104 |
constraints.extend(additional_constraints)
|
105 |
-
params = {'constraints': json.dumps(constraints)}
|
106 |
-
headers = {"Authorization": f"Bearer {bubble_api_key}"}
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
print(
|
121 |
-
return
|
122 |
-
else:
|
123 |
-
print("No posts found for the given org_urn.")
|
124 |
-
return pd.DataFrame(), None
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
|
132 |
# def bulk_upload_to_bubble(data, data_type):
|
|
|
91 |
print(status_message) # Log the final status message
|
92 |
return parsed_token_dict
|
93 |
|
94 |
+
def fetch_linkedin_posts_data_from_bubble(
|
95 |
+
constraint_value: str,
|
96 |
+
data_type: str,
|
97 |
+
constraint_key: str,
|
98 |
+
constraint_type: str,
|
99 |
+
additional_constraints: list = None
|
100 |
+
):
|
101 |
+
"""
|
102 |
+
Fetches data from the Bubble.io Data API, handling pagination to retrieve all results.
|
103 |
+
|
104 |
+
Args:
|
105 |
+
constraint_value: The value to match in the constraint.
|
106 |
+
data_type: The Bubble data type (table name) to query.
|
107 |
+
constraint_key: The field key for the constraint.
|
108 |
+
constraint_type: The type of constraint (e.g., 'equals', 'contains').
|
109 |
+
additional_constraints: A list of additional constraint dictionaries.
|
110 |
+
|
111 |
+
Returns:
|
112 |
+
A tuple containing a pandas DataFrame with all results and an error message string.
|
113 |
+
If successful, the error message is None.
|
114 |
+
If an error occurs, the DataFrame is None.
|
115 |
+
"""
|
116 |
bubble_api_key = os.environ.get("Bubble_API")
|
117 |
if not bubble_api_key:
|
118 |
error_msg = "❌ Bubble API Error: The 'Bubble_API' environment variable is not set."
|
|
|
120 |
return None, error_msg
|
121 |
|
122 |
base_url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}"
|
123 |
+
headers = {"Authorization": f"Bearer {bubble_api_key}"}
|
124 |
+
|
125 |
+
# --- Main Constraint Setup ---
|
126 |
constraints = [{"key": constraint_key, "constraint_type": constraint_type, "value": constraint_value}]
|
127 |
if additional_constraints:
|
128 |
constraints.extend(additional_constraints)
|
|
|
|
|
129 |
|
130 |
+
# --- Pagination Logic ---
|
131 |
+
all_results = []
|
132 |
+
cursor = 0 # Start at the beginning
|
133 |
+
|
134 |
+
print(f"Attempting to fetch data from Bubble for {constraint_key} = {constraint_value}...")
|
135 |
|
136 |
+
while True: # Loop until all pages are fetched
|
137 |
+
# Parameters for the API call, including constraints and pagination info
|
138 |
+
params = {
|
139 |
+
'constraints': json.dumps(constraints),
|
140 |
+
'cursor': cursor,
|
141 |
+
'limit': 100 # Fetch up to 100 items per request (the max)
|
142 |
+
}
|
143 |
+
|
144 |
+
try:
|
145 |
+
# --- Make the API Request ---
|
146 |
+
response = requests.get(base_url, params=params, headers=headers, timeout=30)
|
147 |
+
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
|
148 |
+
|
149 |
+
data = response.json().get("response", {})
|
150 |
+
results_on_page = data.get("results", [])
|
151 |
|
152 |
+
if results_on_page:
|
153 |
+
all_results.extend(results_on_page)
|
154 |
+
print(f"Retrieved {len(results_on_page)} results on this page. Total so far: {len(all_results)}.")
|
155 |
+
|
156 |
+
# --- Check if there are more pages ---
|
157 |
+
# The API returns the number of results remaining. If 0, we're done.
|
158 |
+
remaining = data.get("remaining", 0)
|
159 |
+
if remaining == 0:
|
160 |
+
print("No more pages to fetch.")
|
161 |
+
break # Exit the while loop
|
162 |
+
|
163 |
+
# --- Update cursor for the next page ---
|
164 |
+
# The new cursor is the current cursor + number of items just received.
|
165 |
+
cursor += len(results_on_page)
|
166 |
|
167 |
+
except requests.exceptions.RequestException as e:
|
168 |
+
error_msg = f"❌ Bubble API Error: {str(e)}"
|
169 |
+
print(error_msg)
|
170 |
+
return None, error_msg
|
|
|
|
|
|
|
171 |
|
172 |
+
# --- Final Processing ---
|
173 |
+
if all_results:
|
174 |
+
df = pd.DataFrame(all_results)
|
175 |
+
print(f"✅ Successfully retrieved a total of {len(df)} posts.")
|
176 |
+
return df, None
|
177 |
+
else:
|
178 |
+
print("No posts found for the given constraints.")
|
179 |
+
# Return an empty DataFrame if nothing was found
|
180 |
+
return pd.DataFrame(), None
|
181 |
|
182 |
|
183 |
# def bulk_upload_to_bubble(data, data_type):
|