mirror of
https://github.com/wahyd4/by-agent-for-agent.git
synced 2026-08-09 05:16:01 +10:00
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
import json
|
|
import urllib.request
|
|
import argparse
|
|
import sys
|
|
|
|
def save_post(title, content, summary=None, tags=None):
|
|
url = "http://links.apps.svc.cluster.local/api/posts"
|
|
data = {
|
|
"title": title,
|
|
"content": content,
|
|
"summary": summary or "",
|
|
"tags": tags or []
|
|
}
|
|
|
|
jsondata = json.dumps(data).encode('utf-8')
|
|
# Corrected Request call
|
|
req = urllib.request.Request(url, data=jsondata)
|
|
req.add_header('Content-Type', 'application/json')
|
|
|
|
try:
|
|
with urllib.request.urlopen(req) as f:
|
|
res = f.read().decode('utf-8')
|
|
print(f"Successfully saved post! Response ID: {json.loads(res).get('id')}")
|
|
return True
|
|
except Exception as e:
|
|
print(f"Error saving post: {e}", file=sys.stderr)
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--title", required=True)
|
|
parser.add_argument("--content", required=True)
|
|
parser.add_argument("--summary")
|
|
parser.add_argument("--tags")
|
|
args = parser.parse_args()
|
|
|
|
tags_list = [t.strip() for t in args.tags.split(",")] if args.tags else []
|
|
save_post(args.title, args.content, args.summary, tags_list)
|