-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_post.py
More file actions
83 lines (65 loc) · 2.6 KB
/
Copy pathcreate_post.py
File metadata and controls
83 lines (65 loc) · 2.6 KB
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
83
"""Create a post and publish it.
Point it at a local dev API:
export OWLSTACK_API_KEY=osk_...
export OWLSTACK_BASE_URL=http://localhost:8080/api/v1
python examples/create_post.py "Hello from the Python SDK"
Without --publish it stops at a draft, so you can run it against a real
workspace without anything going out.
"""
from __future__ import annotations
import argparse
import os
import sys
from owlstack import DEFAULT_BASE_URL, Owlstack, OwlstackError
def main() -> int:
parser = argparse.ArgumentParser(description="Create a post with the OwlStack SDK.")
parser.add_argument(
"text",
nargs="?",
default="Hello from the OwlStack Python SDK 🦉",
help="post body",
)
parser.add_argument("--workspace", help="workspace id (defaults to the first one)")
parser.add_argument(
"--publish",
action="store_true",
help="publish immediately instead of leaving a draft",
)
args = parser.parse_args()
api_key = os.environ.get("OWLSTACK_API_KEY")
if not api_key:
print("Set OWLSTACK_API_KEY first.", file=sys.stderr)
return 1
base_url = os.environ.get("OWLSTACK_BASE_URL", DEFAULT_BASE_URL)
with Owlstack(api_key=api_key, base_url=base_url) as client:
try:
workspace_id = args.workspace
if not workspace_id:
workspaces = client.workspaces.list()
if not workspaces:
print("No workspaces on this key.", file=sys.stderr)
return 1
workspace_id = workspaces[0].id
print(f"Workspace: {workspaces[0].name} ({workspace_id})")
accounts = client.accounts.list(workspace_id=workspace_id)
if not accounts:
print("No connected accounts in this workspace.", file=sys.stderr)
return 1
for account in accounts:
print(f" · {account.platform}: @{account.username}")
post = client.posts.create(
workspace_id=workspace_id,
content=args.text,
accounts=[a.id for a in accounts],
)
print(f"Created post {post.id} ({post.status})")
if args.publish:
client.posts.publish(post.id)
for delivery in client.posts.deliveries(post.id):
print(f" · {delivery.platform}: {delivery.status}")
return 0
except OwlstackError as err:
print(f"API error: {err}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())