File size: 3,022 Bytes
479c5b5
d1c6587
ebf808d
0f61c06
b516947
 
 
 
 
479c5b5
d1c6587
b516947
d1c6587
 
b516947
d1c6587
 
 
 
 
 
ebf808d
 
b516947
ebf808d
 
 
 
37ae3c7
 
 
 
b516947
 
 
 
 
 
37ae3c7
ebf808d
37ae3c7
d1c6587
 
 
b516947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1c6587
3317470
b516947
d1c6587
 
 
 
 
479c5b5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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()