cli.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/usr/bin/env python3
  2. import sys
  3. import time
  4. import uuid
  5. from pprint import pprint
  6. import requests
  7. import argparse
  8. import os.path
  9. try:
  10. from colors import color
  11. except ImportError:
  12. def color(text, *args, **kwargs):
  13. return text
  14. try:
  15. from fsdb import Fsdb
  16. except ImportError:
  17. Fsdb = None
  18. def err(msg):
  19. print(color(msg, fg='red', style='bold'))
  20. def is_group_subscribed(mSubscribeFlags):
  21. return bool(mSubscribeFlags & 4)
  22. def req(args, location, data=None):
  23. kwargs = {}
  24. if data is not None:
  25. kwargs["json"] = data
  26. r = requests.post(
  27. args.endpoint + location, auth=tuple(args.auth.split(":", 2)), **kwargs
  28. )
  29. r.raise_for_status()
  30. # TODO: handle r.status_code != 200
  31. return r
  32. def main_forum_list(args):
  33. r = req(args, "/rsGxsForums/getForumsSummaries")
  34. forums = r.json()["forums"]
  35. for item in forums:
  36. if is_group_subscribed(item["mSubscribeFlags"]):
  37. if "notsubscribed" in args.select:
  38. continue
  39. style = "bold"
  40. else:
  41. if "subscribed" in args.select:
  42. continue
  43. style = None
  44. print(color(item["mGroupName"], style=style, fg="green"))
  45. print(" " + color(item["mGroupId"], style="underline"))
  46. def main_forum_read(args):
  47. r = req(args, "/rsGxsForums/getForumMsgMetaData", {"forumId": args.forum_id})
  48. items = r.json()["msgMetas"]
  49. if args.long:
  50. msgs = [item["mMsgId"] for item in items[: args.num_posts]]
  51. items_r = req(
  52. args,
  53. "/rsGxsForums/getForumContent",
  54. {"forumId": args.forum_id, "msgsIds": msgs},
  55. )
  56. items = items_r.json()["msgs"]
  57. for item in items:
  58. print(color(item["mMeta"]["mMsgName"], style="bold", fg="green"))
  59. print()
  60. print(item["mMsg"]) # TODO: html2txt
  61. print()
  62. else:
  63. for item in posts[: args.num_posts]:
  64. print(color(item["mMsgName"], style="bold", fg="green"))
  65. print(" " + color(item["mMsgId"], style="underline"))
  66. def main_channel_list(args):
  67. r = req(args, "/rsGxsChannels/getChannelsSummaries")
  68. channels = r.json()["channels"]
  69. for item in channels:
  70. if is_group_subscribed(item["mSubscribeFlags"]):
  71. if "notsubscribed" in args.select:
  72. continue
  73. style = "bold"
  74. else:
  75. if "subscribed" in args.select:
  76. continue
  77. style = None
  78. print(color(item["mGroupName"], style=style, fg="green"))
  79. print(" " + color(item["mGroupId"], style="underline"))
  80. def main_channel_read(args):
  81. r = req(args, "/rsGxsChannels/getContentSummaries", {"channelId": args.channel_id})
  82. posts = r.json()["summaries"]
  83. if args.long:
  84. msgs = [post["mMsgId"] for post in posts[: args.num_posts]]
  85. posts_r = req(
  86. args,
  87. "/rsGxsChannels/getChannelContent",
  88. {"channelId": args.channel_id, "contentsIds": msgs},
  89. )
  90. posts = posts_r.json()["posts"]
  91. for post in posts:
  92. print(color(post["mMeta"]["mMsgName"], style="bold", fg="green"))
  93. print()
  94. print(post["mMsg"])
  95. print()
  96. else:
  97. for post in posts[: args.num_posts]:
  98. print(color(post["mMsgName"], style="bold", fg="green"))
  99. print(" " + color(post["mMsgId"], style="underline"))
  100. def main_channel_show(args):
  101. r = req(args, "/rsGxsChannels/getChannelsInfo", {"chanIds": [args.channel_id]})
  102. data = r.json()
  103. channels = data["channelsInfo"]
  104. for chan in channels:
  105. print(color(chan["mMeta"]["mGroupName"], style="bold", fg="green"))
  106. print(" " + color(chan["mMeta"]["mGroupId"], style="underline"))
  107. print(" " + chan["mDescription"])
  108. print(color("Last Post:", style="bold") + " \t%s" % chan["mMeta"]["mLastPost"])
  109. def main_channel_post_v1(args):
  110. chid = args.channel_id
  111. r = req(
  112. args,
  113. "/rsGxsChannels/createPost",
  114. {
  115. "post": {
  116. "mMeta": {"mGroupId": args.channel_id, "mMsgName": args.post_title},
  117. "mMsg": args.post_body,
  118. }
  119. },
  120. )
  121. if r.status_code != 200:
  122. print(color("ERROR: could not create post", fg="red", style="bold"))
  123. print("Error %d" % r.status_code)
  124. sys.exit(1)
  125. ret = r.json()
  126. if ret.get("retval", True) is False:
  127. print(color("ERROR: could not create post", fg="red", style="bold"))
  128. pprint(ret)
  129. sys.exit(1)
  130. pprint(ret)
  131. def main_channel_post(args):
  132. chid = args.channel_id
  133. try:
  134. r = req(
  135. args,
  136. "/rsGxsChannels/createPostV2",
  137. {
  138. "channelId": args.channel_id,
  139. "title": args.post_title,
  140. "mBody": args.post_body,
  141. },
  142. )
  143. except requests.exceptions.HTTPError as exc:
  144. if exc.response.status_code != 404:
  145. raise
  146. else:
  147. ret = r.json()
  148. if ret.get("retval", True) is False:
  149. print(color("ERROR: could not create post", fg="red", style="bold"))
  150. print(ret["errorMessage"])
  151. pprint(ret)
  152. sys.exit(1)
  153. pprint(ret)
  154. return main_channel_post_v1(args)
  155. def _file_publish(args, fnames):
  156. if Fsdb is None:
  157. raise Exception('ERROR: library Fsdb is needed for file publishing')
  158. store_dir = os.path.expanduser('~/.config/rscli/store/default')
  159. fsdb = Fsdb(store_dir, fmode='660')
  160. virtualname_path = os.path.join(store_dir, 'virtualname.txt')
  161. if os.path.exists(virtualname_path):
  162. virtualname = open(virtualname_path).read().strip()
  163. else:
  164. virtualname = 'rscli-%s' % uuid.uuid4()
  165. open(virtualname_path, 'w').write(virtualname)
  166. r = req(args, '/rsFiles/getSharedDirectories')
  167. if virtualname not in [shared['virtualname'] for shared in r.json()['dirs']]:
  168. r = req(args, '/rsFiles/addSharedDirectory', {'dir':{ 'filename': store_dir,
  169. 'virtualname': virtualname
  170. }})
  171. if not r.json()['retval']:
  172. raise Exception("Error: could not create shared dir for default store")
  173. r = req(args, '/rsFiles/getSharedDirectories')
  174. dir_filename = [d['filename']
  175. for d in r.json()['dirs']
  176. if d['virtualname'] == virtualname][0]
  177. r = req(args, '/rsFiles/requestDirDetails', { 'handle': 0 })
  178. children = [c for c in r.json()['details']['children']
  179. if c['name'] != '[Extra List]']
  180. for possibile_root in children:
  181. r = req(args, '/rsFiles/requestDirDetails',
  182. { 'handle': possibile_root['handle'] })
  183. found = [c for c in r.json()['details']['children']
  184. if c['name'] == dir_filename]
  185. if not found:
  186. raise Exception ('Error: could not find shared file in RS')
  187. handle = found[0]['handle']
  188. for fname in fnames:
  189. digest = fsdb.add(fname)
  190. r = req(args, '/rsFiles/ForceDirectoryCheck')
  191. time.sleep(5)
  192. # mo lo ricerchiamo va
  193. looking_for = os.path.relpath(fsdb.get_file_path(digest),
  194. store_dir).split(os.path.sep)
  195. for next_component in looking_for:
  196. r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
  197. found = [c for c in r.json()['details']['children']
  198. if c['name'] == next_component]
  199. if not found:
  200. raise Exception('Error: could not find shared file in RS')
  201. handle = found[0]['handle']
  202. r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
  203. filehash = r.json()['details']['hash']
  204. yield filehash
  205. def main_file_publish(args):
  206. ret = _file_publish(args, args.fnames)
  207. for filehash, fname in zip(ret, args.fnames):
  208. print(color(filehash, fg='green') + ' \t%s' % fname)
  209. def get_parser():
  210. p = argparse.ArgumentParser()
  211. p.add_argument("--endpoint", default="http://127.0.0.1:9092")
  212. p.add_argument("-u", "--auth", dest="auth")
  213. p_sub = p.add_subparsers()
  214. ch = p_sub.add_parser("channel")
  215. ch.add_argument("--channel-id")
  216. ch_sub = ch.add_subparsers()
  217. ch_list = ch_sub.add_parser("list")
  218. ch_list.add_argument(
  219. "--select",
  220. nargs="+",
  221. choices=["all", "subscribed", "notsubscribed"],
  222. default=["all"],
  223. )
  224. ch_list.set_defaults(mainfunc=main_channel_list)
  225. ch_show = ch_sub.add_parser("show")
  226. ch_show.set_defaults(mainfunc=main_channel_show)
  227. ch_read = ch_sub.add_parser("read")
  228. ch_read.add_argument("--long", action="store_true", default=False)
  229. ch_read.add_argument("--num-posts", type=int, default=10)
  230. ch_read.set_defaults(mainfunc=main_channel_read)
  231. ch_post = ch_sub.add_parser("post")
  232. ch_post.set_defaults(mainfunc=main_channel_post)
  233. ch_post.add_argument("--post-title")
  234. ch_post.add_argument("--post-body")
  235. forum = p_sub.add_parser("forum")
  236. forum.add_argument("--forum-id")
  237. forum_sub = forum.add_subparsers()
  238. forum_list = forum_sub.add_parser("list")
  239. forum_list.add_argument(
  240. "--select",
  241. nargs="+",
  242. choices=["all", "subscribed", "notsubscribed"],
  243. default=["all"],
  244. )
  245. forum_list.set_defaults(mainfunc=main_forum_list)
  246. forum_read = forum_sub.add_parser("read")
  247. forum_read.add_argument("--long", action="store_true", default=False)
  248. forum_read.add_argument("--num-posts", type=int, default=10)
  249. forum_read.set_defaults(mainfunc=main_forum_read)
  250. files = p_sub.add_parser("file")
  251. files_sub = files.add_subparsers()
  252. files_list = files_sub.add_parser("publish")
  253. files_list.add_argument(
  254. "--fname",
  255. nargs="+",
  256. required=True,
  257. dest='fnames'
  258. )
  259. files_list.set_defaults(mainfunc=main_file_publish)
  260. # TODO: channel rss -> read and convert to rss
  261. return p
  262. def main():
  263. p = get_parser()
  264. args = p.parse_args()
  265. if getattr(args, "mainfunc", None) is None:
  266. print("Not a complete command")
  267. sys.exit(2)
  268. args.mainfunc(args)
  269. if __name__ == "__main__":
  270. main()