hlydecker commited on
Commit
b516947
1 Parent(s): 37ae3c7

feature: add posting

Browse files
Files changed (1) hide show
  1. app.py +45 -7
app.py CHANGED
@@ -1,12 +1,18 @@
1
  import gradio as gr
2
  import subprocess
3
  import json
 
 
 
 
 
 
4
 
5
  def api_call(input_text):
6
- # Replace 'YOUR_CURL_COMMAND' with the actual cURL command you want to use
7
  curl_command = [
8
  'curl', '-s', '--request', 'GET',
9
- '--url', f"https://api.padlet.dev/v1/boards/{input_text}?include=posts%2Csections",
10
  '--header', 'X-Api-Key: pdltp_0e380a0de1ff32d77b12dbcc030b1373199b7525681ddc81bd1b9ef3e4e3dd49577a23',
11
  '--header', 'accept: application/vnd.api+json'
12
  ]
@@ -15,7 +21,7 @@ def api_call(input_text):
15
  response = subprocess.check_output(curl_command, universal_newlines=True)
16
  response_data = json.loads(response)
17
 
18
- # Extract the contents of all posts
19
  posts_data = response_data.get("included", [])
20
  post_contents = []
21
  for post in posts_data:
@@ -24,17 +30,49 @@ def api_call(input_text):
24
  subject = attributes.get("subject", "")
25
  body_html = attributes.get("bodyHtml", "")
26
 
27
- if subject or body_html:
28
- post_content = f"Subject: {subject}\nBody HTML: {body_html}"
 
 
 
 
29
  post_contents.append(post_content)
30
 
31
  return "\n\n".join(post_contents) if post_contents else "No post contents found."
32
  except subprocess.CalledProcessError:
33
  return "Error: Unable to fetch data using cURL."
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  iface = gr.Interface(
36
- fn=api_call,
37
- inputs=gr.inputs.Textbox(),
38
  outputs=gr.outputs.Textbox(),
39
  live=True,
40
  title="Padlet API Caller with cURL",
 
1
  import gradio as gr
2
  import subprocess
3
  import json
4
+ from bs4 import BeautifulSoup
5
+
6
+ def strip_html_tags(html_text):
7
+ # Use BeautifulSoup to parse and clean HTML content
8
+ soup = BeautifulSoup(html_text, 'html.parser')
9
+ return soup.get_text()
10
 
11
  def api_call(input_text):
12
+
13
  curl_command = [
14
  'curl', '-s', '--request', 'GET',
15
+ '--url', f"https://api.padlet.dev/v1/boards/{board_id}?include=posts%2Csections",
16
  '--header', 'X-Api-Key: pdltp_0e380a0de1ff32d77b12dbcc030b1373199b7525681ddc81bd1b9ef3e4e3dd49577a23',
17
  '--header', 'accept: application/vnd.api+json'
18
  ]
 
21
  response = subprocess.check_output(curl_command, universal_newlines=True)
22
  response_data = json.loads(response)
23
 
24
+ # Extract the contents of all posts, stripping HTML tags from bodyHtml
25
  posts_data = response_data.get("included", [])
26
  post_contents = []
27
  for post in posts_data:
 
30
  subject = attributes.get("subject", "")
31
  body_html = attributes.get("bodyHtml", "")
32
 
33
+ if subject:
34
+ post_content = f"Subject: {subject}"
35
+ if body_html:
36
+ cleaned_body = strip_html_tags(body_html)
37
+ post_content += f"\nBody Text: {cleaned_body}"
38
+
39
  post_contents.append(post_content)
40
 
41
  return "\n\n".join(post_contents) if post_contents else "No post contents found."
42
  except subprocess.CalledProcessError:
43
  return "Error: Unable to fetch data using cURL."
44
 
45
+ def create_post(board_id, post_content):
46
+
47
+ curl_command = [
48
+ 'curl', '-s', '--request', 'POST',
49
+ '--url', f"https://api.padlet.dev/v1/boards/{board_id}/posts",
50
+ '--header', 'X-Api-Key: pdltp_0e380a0de1ff32d77b12dbcc030b1373199b7525681ddc81bd1b9ef3e4e3dd49577a23',
51
+ '--header', 'accept: application/vnd.api+json',
52
+ '--header', 'content-type: application/vnd.api+json',
53
+ '--data',
54
+ json.dumps({
55
+ "data": {
56
+ "type": "post",
57
+ "attributes": {
58
+ "content": {
59
+ "subject": post_content
60
+ }
61
+ }
62
+ }
63
+ })
64
+ ]
65
+
66
+ try:
67
+ response = subprocess.check_output(curl_command, universal_newlines=True)
68
+ response_data = json.loads(response)
69
+ return "Post created successfully."
70
+ except subprocess.CalledProcessError as e:
71
+ return f"Error: Unable to create post - {str(e)}"
72
+
73
  iface = gr.Interface(
74
+ fn=[api_call,create_post]
75
+ inputs=[gr.inputs.Textbox(label="Board ID"), gr.inputs.Textbox(label="Post Content")],
76
  outputs=gr.outputs.Textbox(),
77
  live=True,
78
  title="Padlet API Caller with cURL",