Spaces:
Runtime error
Runtime error
import gradio as gr | |
import subprocess | |
import json | |
from bs4 import BeautifulSoup | |
def strip_html_tags(html_text): | |
# Use BeautifulSoup to parse and clean HTML content | |
soup = BeautifulSoup(html_text, 'html.parser') | |
return soup.get_text() | |
def api_call(input_text): | |
curl_command = [ | |
'curl', '-s', '--request', 'GET', | |
'--url', f"https://api.padlet.dev/v1/boards/{board_id}?include=posts%2Csections", | |
'--header', 'X-Api-Key: pdltp_0e380a0de1ff32d77b12dbcc030b1373199b7525681ddc81bd1b9ef3e4e3dd49577a23', | |
'--header', 'accept: application/vnd.api+json' | |
] | |
try: | |
response = subprocess.check_output(curl_command, universal_newlines=True) | |
response_data = json.loads(response) | |
# Extract the contents of all posts, stripping HTML tags from bodyHtml | |
posts_data = response_data.get("included", []) | |
post_contents = [] | |
for post in posts_data: | |
if post.get("type") == "post": | |
attributes = post.get("attributes", {}).get("content", {}) | |
subject = attributes.get("subject", "") | |
body_html = attributes.get("bodyHtml", "") | |
if subject: | |
post_content = f"Subject: {subject}" | |
if body_html: | |
cleaned_body = strip_html_tags(body_html) | |
post_content += f"\nBody Text: {cleaned_body}" | |
post_contents.append(post_content) | |
return "\n\n".join(post_contents) if post_contents else "No post contents found." | |
except subprocess.CalledProcessError: | |
return "Error: Unable to fetch data using cURL." | |
def create_post(board_id, post_content): | |
curl_command = [ | |
'curl', '-s', '--request', 'POST', | |
'--url', f"https://api.padlet.dev/v1/boards/{board_id}/posts", | |
'--header', 'X-Api-Key: pdltp_0e380a0de1ff32d77b12dbcc030b1373199b7525681ddc81bd1b9ef3e4e3dd49577a23', | |
'--header', 'accept: application/vnd.api+json', | |
'--header', 'content-type: application/vnd.api+json', | |
'--data', | |
json.dumps({ | |
"data": { | |
"type": "post", | |
"attributes": { | |
"content": { | |
"subject": post_content | |
} | |
} | |
} | |
}) | |
] | |
try: | |
response = subprocess.check_output(curl_command, universal_newlines=True) | |
response_data = json.loads(response) | |
return "Post created successfully." | |
except subprocess.CalledProcessError as e: | |
return f"Error: Unable to create post - {str(e)}" | |
iface = gr.Interface( | |
fn=[api_call,create_post], | |
inputs=[gr.inputs.Textbox(label="Board ID"), gr.inputs.Textbox(label="Post Content")], | |
outputs=gr.outputs.Textbox(), | |
live=True, | |
title="Padlet API Caller with cURL", | |
description="Enter Padlet board ID and get board details using cURL" | |
) | |
iface.launch() |