Browse Source

channel read

begin retro-compatibility createPost
boyska 4 years ago
parent
commit
53e1bd54b9
1 changed files with 81 additions and 28 deletions
  1. 81 28
      cli.py

+ 81 - 28
cli.py

@@ -7,6 +7,7 @@ import argparse
 try:
     from colors import color
 except ImportError:
+
     def color(text, *args, **kwargs):
         return text
 
@@ -14,50 +15,97 @@ except ImportError:
 def req(args, location, data=None):
     kwargs = {}
     if data is not None:
-        kwargs['json'] = data
-    r = requests.post(args.endpoint + location,
-        auth=tuple(args.auth.split(":", 2)),
-                         **kwargs
-                        )
+        kwargs["json"] = data
+    r = requests.post(
+        args.endpoint + location, auth=tuple(args.auth.split(":", 2)), **kwargs
+    )
+    # TODO: handle r.status_code != 200
     return r
 
+
 def main_channel_list(args):
     r = req(args, "/rsGxsChannels/getChannelsSummaries")
-    channels = r.json()['channels']
+    channels = r.json()["channels"]
     for chan in channels:
-        print(color(chan['mGroupName'], style='bold', fg='green'))
-        print('   ' + color(chan['mGroupId'], style='underline'))
+        print(color(chan["mGroupName"], style="bold", fg="green"))
+        print("   " + color(chan["mGroupId"], style="underline"))
+
+
+def main_channel_read(args):
+    r = req(args, "/rsGxsChannels/getContentSummaries", {"channelId": args.channel_id})
+    posts = r.json()["summaries"]
+    if args.long:
+        msgs = [post["mMsgId"] for post in posts[: args.num_posts]]
+        posts_r = req(
+            args,
+            "/rsGxsChannels/getChannelContent",
+            {"channelId": args.channel_id, "contentsIds": msgs},
+        )
+        posts = posts_r.json()["posts"]
+        for post in posts:
+            print(color(post["mMeta"]["mMsgName"], style="bold", fg="green"))
+            print()
+            print(color(post["mMsg"]))
+            print()
+    else:
+        for post in posts[:args.num_posts]:
+            print(color(post["mMsgName"], style="bold", fg="green"))
+            print("   " + color(post["mMsgId"], style="underline"))
+
 
 def main_channel_show(args):
+    r = req(args, "/rsGxsChannels/getChannelsInfo", {"chanIds": [args.channel_id]})
+    data = r.json()
+    channels = data["channelsInfo"]
+    for chan in channels:
+        print(color(chan["mMeta"]["mGroupName"], style="bold", fg="green"))
+        print("   " + color(chan["mMeta"]["mGroupId"], style="underline"))
+        print("   " + chan["mDescription"])
+        print(color("Last Post:", style="bold") + " \t%s" % chan["mMeta"]["mLastPost"])
+
+
+def main_channel_post_v1(args):
+    chid = args.channel_id
     r = req(
-        args, "/rsGxsChannels/getChannelsInfo",
+        args,
+        "/rsGxsChannels/createPost",
         {
-            'chanIds': [args.channel_id]
-        }
+            "channelId": args.channel_id,
+            "title": args.post_title,
+            "mBody": args.post_body,
+        },
     )
-    data = r.json()
-    channels = data['channelsInfo']
-    for chan in channels:
-        print(color(chan['mMeta']['mGroupName'], style='bold', fg='green'))
-        print('   ' + color(chan['mMeta']['mGroupId'], style='underline'))
-        print('   ' + chan['mDescription'])
-        print(color('Last Post:', style='bold') + ' \t%s' % chan['mMeta']['mLastPost'])
+    if r.status_code != 200:
+        print(color("ERROR: could not create post", fg="red", style="bold"))
+        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)
 
 
 def main_channel_post(args):
     chid = args.channel_id
     r = req(
-        args, "/rsGxsChannels/createPostV2",
+        args,
+        "/rsGxsChannels/createPostV2",
         {
-            'channelId': args.channel_id,
-            'title': args.post_title,
-            'mBody': args.post_body,
-        }
+            "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'])
+    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)
@@ -79,10 +127,15 @@ def get_parser():
     ch_show = ch_sub.add_parser("show")
     ch_show.set_defaults(mainfunc=main_channel_show)
 
+    ch_read = ch_sub.add_parser("read")
+    ch_read.add_argument("--long", action="store_true", default=False)
+    ch_read.add_argument("--num-posts", type=int, default=10)
+    ch_read.set_defaults(mainfunc=main_channel_read)
+
     ch_post = ch_sub.add_parser("post")
     ch_post.set_defaults(mainfunc=main_channel_post)
-    ch_post.add_argument('--post-title')
-    ch_post.add_argument('--post-body')
+    ch_post.add_argument("--post-title")
+    ch_post.add_argument("--post-body")
 
     return p