channel post: compatible with old API
This commit is contained in:
parent
fd7f66630c
commit
1de56fe936
1 changed files with 29 additions and 26 deletions
25
cli.py
25
cli.py
|
@ -23,6 +23,7 @@ def req(args, location, data=None):
|
||||||
r = requests.post(
|
r = requests.post(
|
||||||
args.endpoint + location, auth=tuple(args.auth.split(":", 2)), **kwargs
|
args.endpoint + location, auth=tuple(args.auth.split(":", 2)), **kwargs
|
||||||
)
|
)
|
||||||
|
r.raise_for_status()
|
||||||
# TODO: handle r.status_code != 200
|
# TODO: handle r.status_code != 200
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ def main_forum_list(args):
|
||||||
print(color(item["mGroupName"], style=style, fg="green"))
|
print(color(item["mGroupName"], style=style, fg="green"))
|
||||||
print(" " + color(item["mGroupId"], style="underline"))
|
print(" " + color(item["mGroupId"], style="underline"))
|
||||||
|
|
||||||
|
|
||||||
def main_forum_read(args):
|
def main_forum_read(args):
|
||||||
r = req(args, "/rsGxsForums/getForumMsgMetaData", {"forumId": args.forum_id})
|
r = req(args, "/rsGxsForums/getForumMsgMetaData", {"forumId": args.forum_id})
|
||||||
items = r.json()["msgMetas"]
|
items = r.json()["msgMetas"]
|
||||||
|
@ -52,12 +54,11 @@ def main_forum_read(args):
|
||||||
"/rsGxsForums/getForumContent",
|
"/rsGxsForums/getForumContent",
|
||||||
{"forumId": args.forum_id, "msgsIds": msgs},
|
{"forumId": args.forum_id, "msgsIds": msgs},
|
||||||
)
|
)
|
||||||
pprint(items_r.json())
|
|
||||||
items = items_r.json()["msgs"]
|
items = items_r.json()["msgs"]
|
||||||
for item in items:
|
for item in items:
|
||||||
print(color(item["mMeta"]["mMsgName"], style="bold", fg="green"))
|
print(color(item["mMeta"]["mMsgName"], style="bold", fg="green"))
|
||||||
print()
|
print()
|
||||||
print(color(item["mMsg"])) # TODO: html2txt
|
print(item["mMsg"]) # TODO: html2txt
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
for item in posts[: args.num_posts]:
|
for item in posts[: args.num_posts]:
|
||||||
|
@ -95,7 +96,7 @@ def main_channel_read(args):
|
||||||
for post in posts:
|
for post in posts:
|
||||||
print(color(post["mMeta"]["mMsgName"], style="bold", fg="green"))
|
print(color(post["mMeta"]["mMsgName"], style="bold", fg="green"))
|
||||||
print()
|
print()
|
||||||
print(color(post["mMsg"]))
|
print(post["mMsg"])
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
for post in posts[: args.num_posts]:
|
for post in posts[: args.num_posts]:
|
||||||
|
@ -120,9 +121,10 @@ def main_channel_post_v1(args):
|
||||||
args,
|
args,
|
||||||
"/rsGxsChannels/createPost",
|
"/rsGxsChannels/createPost",
|
||||||
{
|
{
|
||||||
"channelId": args.channel_id,
|
"post": {
|
||||||
"title": args.post_title,
|
"mMeta": {"mGroupId": args.channel_id, "mMsgName": args.post_title},
|
||||||
"mBody": args.post_body,
|
"mMsg": args.post_body,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
|
@ -130,10 +132,8 @@ def main_channel_post_v1(args):
|
||||||
print("Error %d" % r.status_code)
|
print("Error %d" % r.status_code)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
ret = r.json()
|
ret = r.json()
|
||||||
print(r.json())
|
|
||||||
if ret.get("retval", True) is False:
|
if ret.get("retval", True) is False:
|
||||||
print(color("ERROR: could not create post", fg="red", style="bold"))
|
print(color("ERROR: could not create post", fg="red", style="bold"))
|
||||||
print(ret["errorMessage"])
|
|
||||||
pprint(ret)
|
pprint(ret)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
pprint(ret)
|
pprint(ret)
|
||||||
|
@ -141,6 +141,7 @@ def main_channel_post_v1(args):
|
||||||
|
|
||||||
def main_channel_post(args):
|
def main_channel_post(args):
|
||||||
chid = args.channel_id
|
chid = args.channel_id
|
||||||
|
try:
|
||||||
r = req(
|
r = req(
|
||||||
args,
|
args,
|
||||||
"/rsGxsChannels/createPostV2",
|
"/rsGxsChannels/createPostV2",
|
||||||
|
@ -150,8 +151,10 @@ def main_channel_post(args):
|
||||||
"mBody": args.post_body,
|
"mBody": args.post_body,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if r.status_code == 404:
|
except requests.exceptions.HTTPError as exc:
|
||||||
return main_channel_post_v1(args)
|
if exc.response.status_code != 404:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
ret = r.json()
|
ret = r.json()
|
||||||
if ret.get("retval", True) is False:
|
if ret.get("retval", True) is False:
|
||||||
print(color("ERROR: could not create post", fg="red", style="bold"))
|
print(color("ERROR: could not create post", fg="red", style="bold"))
|
||||||
|
@ -159,6 +162,7 @@ def main_channel_post(args):
|
||||||
pprint(ret)
|
pprint(ret)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
pprint(ret)
|
pprint(ret)
|
||||||
|
return main_channel_post_v1(args)
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
def get_parser():
|
||||||
|
@ -210,7 +214,6 @@ def get_parser():
|
||||||
forum_read.add_argument("--num-posts", type=int, default=10)
|
forum_read.add_argument("--num-posts", type=int, default=10)
|
||||||
forum_read.set_defaults(mainfunc=main_forum_read)
|
forum_read.set_defaults(mainfunc=main_forum_read)
|
||||||
|
|
||||||
|
|
||||||
# TODO: channel rss -> read and convert to rss
|
# TODO: channel rss -> read and convert to rss
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
Loading…
Reference in a new issue