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