comments.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. # This file will perform a simple search on the LBRY network.
  30. from subprocess import *
  31. import json
  32. from flbry import url
  33. from flbry.variables import *
  34. from flbry import markdown
  35. from flbry import channel
  36. def list(claim_id, url, comment_id=""):
  37. # This function will list a list of comments of a certain claim_id
  38. # It will preview a very basic form of comment. You will have
  39. # to select a comment to interact with it further. Like read the
  40. # whole text, if it not short ( or in a markdown format ). Or
  41. # do other things like replies.
  42. w, h = tsize()
  43. page_size = h - 5
  44. page = 1
  45. while True:
  46. # Printing the search query and page number
  47. center("COMMENTS OF: "+url+" PAGE : "+str(page))
  48. if not comment_id:
  49. out = check_output(["flbry/lbrynet",
  50. "comment", "list", claim_id,
  51. '--page='+str(page),
  52. '--page_size='+str(page_size)])
  53. else:
  54. out = check_output(["flbry/lbrynet",
  55. "comment", "list", '--claim_id='+claim_id,
  56. '--parent_id='+comment_id,
  57. '--page='+str(page),
  58. '--page_size='+str(page_size),
  59. '--include_replies'])
  60. # Now we want to parse the json
  61. try:
  62. out = json.loads(out)
  63. except:
  64. print(" Connect to LBRY first.")
  65. return
  66. d = {"categories":["Tip LBC", "Comments", "Channel", "Preview"],
  67. "size":[1,1,2,5],
  68. "data":[]}
  69. try:
  70. # List what we found
  71. for n, i in enumerate(out["items"]):
  72. preview = "---!Failed Loading comment---"
  73. support = 0
  74. replies = 0
  75. bywho = "@Anonymous"
  76. try:
  77. comment = i["comment"]
  78. preview = comment.replace("\n", " ")
  79. support = i["support_amount"]
  80. bywho = i["channel_name"]
  81. replies = i["replies"]
  82. except:
  83. pass
  84. d["data"].append([support, replies, bywho, preview])
  85. table(d)
  86. # Tell the user that he might want to load more
  87. center("---type 'more' to load more---")
  88. page = page +1
  89. # Error messages
  90. except Exception as e:
  91. if "code" in out:
  92. print(" Error code: ", out["code"] )
  93. if out["code"] == -32500:
  94. print(" SDK is still starting. Patience!")
  95. else:
  96. print( e)
  97. return
  98. # Making sure that we stop every time a new page is reached
  99. c = input(typing_dots())
  100. if c != "more":
  101. break
  102. try:
  103. c = int(c)
  104. except:
  105. return
  106. while True:
  107. view(out["items"][c])
  108. c = input(typing_dots())
  109. if not c:
  110. break
  111. try:
  112. c = int(c)
  113. except:
  114. return
  115. def view(i):
  116. # This function will give a user an ability to interact / read a
  117. # given comment.
  118. preview = "---!Failed Loading comment---"
  119. comment = ""
  120. support = 0
  121. bywho = "@Anonymous"
  122. replies = 0
  123. try:
  124. comment = i["comment"]
  125. preview = comment.replace("\n", " ")
  126. support = i["support_amount"]
  127. bywho = i["channel_name"]
  128. replies = i["replies"]
  129. except:
  130. pass
  131. # TIP LBC # COMMENTS ( REPLIES ) # CHANNEL
  132. d = {"categories":["Tip LBC", "Comments", "Channel"],
  133. "size":[1,1,3],
  134. "data":[[support, replies, bywho]]}
  135. table(d, False)
  136. # Preview
  137. d = {"categories":["Preview"],
  138. "size":[1],
  139. "data":[[preview]]}
  140. table(d, False)
  141. # The help thing
  142. center("--- for comment commands list type 'help' --- ")
  143. # let's implement commands
  144. while True:
  145. c = input(typing_dots())
  146. if not c:
  147. break
  148. elif c == "help":
  149. markdown.draw("help/comments.md", "Comments Help")
  150. elif c == "read":
  151. savedes = open("/tmp/fastlbrylastcomment.md", "w")
  152. savedes.write(comment)
  153. savedes.close()
  154. markdown.draw("/tmp/fastlbrylastcomment.md", "Full Text Of a Comment")
  155. elif c == "channel":
  156. channel.simple(i["channel_url"])
  157. elif c == "comments":
  158. list(i["claim_id"], " ", i["comment_id"],)
  159. elif c.startswith("reply"):
  160. c = c + ' '
  161. post(i["claim_id"], c[c.find(" "):], i["comment_id"])
  162. def post(claim_id, args, parent_id=""):
  163. # This will post a comment under either a publication or a
  164. # comment as a reply.
  165. # We gonna check if dunring typing reply the user added
  166. # anything after the word reply.
  167. # reply emacs
  168. # reply gedit
  169. # reply vim
  170. # Or something like
  171. # reply /home/username/filename.txt
  172. # reply /home/username/filename.md
  173. # That might be useful to input multiline text.
  174. if len(args) > 1:
  175. a = args.split()[0]
  176. try:
  177. text = open(a, "r")
  178. text = text.read()
  179. except:
  180. text = open("/tmp/fastlbrycommentwriter.txt", "w")
  181. text.write("Type your reply here. Don't forget to save. Then return to FastLBRY.")
  182. text.close()
  183. import os
  184. os.system(a+" /tmp/fastlbrycommentwriter.txt")
  185. center("Press Enter when the file is ready and saved.")
  186. text = open("/tmp/fastlbrycommentwriter.txt", "r")
  187. text = text.read()
  188. else:
  189. text = input(" Text: ")
  190. post_as = channel.select("Reply as who? Select Channel.")
  191. if not post_as.startswith("@"):
  192. post_as = "@"+post_as
  193. if not parent_id:
  194. out = check_output(["flbry/lbrynet",
  195. "comment", "create",
  196. text,
  197. '--channel_name='+post_as,
  198. '--claim_id='+claim_id])
  199. else:
  200. out = check_output(["flbry/lbrynet",
  201. "comment", "create",
  202. text,
  203. '--channel_name='+post_as,
  204. '--parent_id='+parent_id,
  205. '--claim_id='+claim_id])
  206. out = json.loads(out)
  207. if "message" in out:
  208. print(" "+clr["bbrd"]+clr["bold"]+wdth(out["message"], 89)+clr["norm"])
  209. else:
  210. print(" "+clr["bdgr"]+clr["bold"]+wdth("Comment is sent.", 89)+clr["norm"])