cli.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env python3
  2. import sys
  3. from pprint import pprint
  4. import requests
  5. import argparse
  6. try:
  7. from colors import color
  8. except ImportError:
  9. def color(text, *args, **kwargs):
  10. return text
  11. def is_group_subscribed(mSubscribeFlags):
  12. return bool(mSubscribeFlags & 4)
  13. def req(args, location, data=None):
  14. kwargs = {}
  15. if data is not None:
  16. kwargs["json"] = data
  17. r = requests.post(
  18. args.endpoint + location, auth=tuple(args.auth.split(":", 2)), **kwargs
  19. )
  20. # TODO: handle r.status_code != 200
  21. return r
  22. def main_forum_list(args):
  23. r = req(args, "/rsGxsForums/getForumsSummaries")
  24. forums = r.json()["forums"]
  25. for item in forums:
  26. if is_group_subscribed(item["mSubscribeFlags"]):
  27. if "notsubscribed" in args.select:
  28. continue
  29. style = "bold"
  30. else:
  31. if "subscribed" in args.select:
  32. continue
  33. style = None
  34. print(color(item["mGroupName"], style=style, fg="green"))
  35. print(" " + color(item["mGroupId"], style="underline"))
  36. def main_forum_read(args):
  37. r = req(args, "/rsGxsForums/getForumMsgMetaData", {"forumId": args.forum_id})
  38. items = r.json()["msgMetas"]
  39. if args.long:
  40. msgs = [item["mMsgId"] for item in items[: args.num_posts]]
  41. items_r = req(
  42. args,
  43. "/rsGxsForums/getForumContent",
  44. {"forumId": args.forum_id, "msgsIds": msgs},
  45. )
  46. pprint(items_r.json())
  47. items = items_r.json()["msgs"]
  48. for item in items:
  49. print(color(item["mMeta"]["mMsgName"], style="bold", fg="green"))
  50. print()
  51. print(color(item["mMsg"])) # TODO: html2txt
  52. print()
  53. else:
  54. for item in posts[: args.num_posts]:
  55. print(color(item["mMsgName"], style="bold", fg="green"))
  56. print(" " + color(item["mMsgId"], style="underline"))
  57. def main_channel_list(args):
  58. r = req(args, "/rsGxsChannels/getChannelsSummaries")
  59. channels = r.json()["channels"]
  60. for item in channels:
  61. if is_group_subscribed(item["mSubscribeFlags"]):
  62. if "notsubscribed" in args.select:
  63. continue
  64. style = "bold"
  65. else:
  66. if "subscribed" in args.select:
  67. continue
  68. style = None
  69. print(color(item["mGroupName"], style=style, fg="green"))
  70. print(" " + color(item["mGroupId"], style="underline"))
  71. def main_channel_read(args):
  72. r = req(args, "/rsGxsChannels/getContentSummaries", {"channelId": args.channel_id})
  73. posts = r.json()["summaries"]
  74. if args.long:
  75. msgs = [post["mMsgId"] for post in posts[: args.num_posts]]
  76. posts_r = req(
  77. args,
  78. "/rsGxsChannels/getChannelContent",
  79. {"channelId": args.channel_id, "contentsIds": msgs},
  80. )
  81. posts = posts_r.json()["posts"]
  82. for post in posts:
  83. print(color(post["mMeta"]["mMsgName"], style="bold", fg="green"))
  84. print()
  85. print(color(post["mMsg"]))
  86. print()
  87. else:
  88. for post in posts[: args.num_posts]:
  89. print(color(post["mMsgName"], style="bold", fg="green"))
  90. print(" " + color(post["mMsgId"], style="underline"))
  91. def main_channel_show(args):
  92. r = req(args, "/rsGxsChannels/getChannelsInfo", {"chanIds": [args.channel_id]})
  93. data = r.json()
  94. channels = data["channelsInfo"]
  95. for chan in channels:
  96. print(color(chan["mMeta"]["mGroupName"], style="bold", fg="green"))
  97. print(" " + color(chan["mMeta"]["mGroupId"], style="underline"))
  98. print(" " + chan["mDescription"])
  99. print(color("Last Post:", style="bold") + " \t%s" % chan["mMeta"]["mLastPost"])
  100. def main_channel_post_v1(args):
  101. chid = args.channel_id
  102. r = req(
  103. args,
  104. "/rsGxsChannels/createPost",
  105. {
  106. "channelId": args.channel_id,
  107. "title": args.post_title,
  108. "mBody": args.post_body,
  109. },
  110. )
  111. if r.status_code != 200:
  112. print(color("ERROR: could not create post", fg="red", style="bold"))
  113. print("Error %d" % r.status_code)
  114. sys.exit(1)
  115. ret = r.json()
  116. print(r.json())
  117. if ret.get("retval", True) is False:
  118. print(color("ERROR: could not create post", fg="red", style="bold"))
  119. print(ret["errorMessage"])
  120. pprint(ret)
  121. sys.exit(1)
  122. pprint(ret)
  123. def main_channel_post(args):
  124. chid = args.channel_id
  125. r = req(
  126. args,
  127. "/rsGxsChannels/createPostV2",
  128. {
  129. "channelId": args.channel_id,
  130. "title": args.post_title,
  131. "mBody": args.post_body,
  132. },
  133. )
  134. if r.status_code == 404:
  135. return main_channel_post_v1(args)
  136. ret = r.json()
  137. if ret.get("retval", True) is False:
  138. print(color("ERROR: could not create post", fg="red", style="bold"))
  139. print(ret["errorMessage"])
  140. pprint(ret)
  141. sys.exit(1)
  142. pprint(ret)
  143. def get_parser():
  144. p = argparse.ArgumentParser()
  145. p.add_argument("--endpoint", default="http://127.0.0.1:9092")
  146. p.add_argument("-u", "--auth", dest="auth")
  147. p_sub = p.add_subparsers()
  148. ch = p_sub.add_parser("channel")
  149. ch.add_argument("--channel-id")
  150. ch_sub = ch.add_subparsers()
  151. ch_list = ch_sub.add_parser("list")
  152. ch_list.add_argument(
  153. "--select",
  154. nargs="+",
  155. choices=["all", "subscribed", "notsubscribed"],
  156. default=["all"],
  157. )
  158. ch_list.set_defaults(mainfunc=main_channel_list)
  159. ch_show = ch_sub.add_parser("show")
  160. ch_show.set_defaults(mainfunc=main_channel_show)
  161. ch_read = ch_sub.add_parser("read")
  162. ch_read.add_argument("--long", action="store_true", default=False)
  163. ch_read.add_argument("--num-posts", type=int, default=10)
  164. ch_read.set_defaults(mainfunc=main_channel_read)
  165. ch_post = ch_sub.add_parser("post")
  166. ch_post.set_defaults(mainfunc=main_channel_post)
  167. ch_post.add_argument("--post-title")
  168. ch_post.add_argument("--post-body")
  169. forum = p_sub.add_parser("forum")
  170. forum.add_argument("--forum-id")
  171. forum_sub = forum.add_subparsers()
  172. forum_list = forum_sub.add_parser("list")
  173. forum_list.add_argument(
  174. "--select",
  175. nargs="+",
  176. choices=["all", "subscribed", "notsubscribed"],
  177. default=["all"],
  178. )
  179. forum_list.set_defaults(mainfunc=main_forum_list)
  180. forum_read = forum_sub.add_parser("read")
  181. forum_read.add_argument("--long", action="store_true", default=False)
  182. forum_read.add_argument("--num-posts", type=int, default=10)
  183. forum_read.set_defaults(mainfunc=main_forum_read)
  184. # TODO: channel rss -> read and convert to rss
  185. return p
  186. def main():
  187. p = get_parser()
  188. args = p.parse_args()
  189. if getattr(args, "mainfunc", None) is None:
  190. print("Not a complete command")
  191. sys.exit(2)
  192. args.mainfunc(args)
  193. if __name__ == "__main__":
  194. main()