comments.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. import urllib.request
  33. from flbry import url
  34. from flbry.variables import *
  35. from flbry import markdown
  36. from flbry import channel
  37. from flbry import settings
  38. def list(claim_id: str, link: str, comment_id: str = ""):
  39. """
  40. Lists comments in a short form, truncating some if needed. Comments can be selected for further actions.
  41. Keyword arguments:
  42. claim_id -- ID of the claim to get comments from
  43. link -- LBRY URL of the claim, used for display
  44. comment_id -- Comment ID to get sub-comments of, optional
  45. """
  46. w, h = tsize()
  47. page_size = h - 5
  48. page = 1
  49. while True:
  50. # Printing the search query and page number
  51. center("COMMENTS OF: "+link+" PAGE : "+str(page))
  52. params = {
  53. "claim_id": claim_id,
  54. "page": page,
  55. "page_size": page_size,
  56. "sort_by": 3,
  57. "top_level": True,
  58. }
  59. if comment_id:
  60. params["parent_id"] = comment_id
  61. params["top_level"] = False
  62. out = comment_request("comment.List", params)
  63. if not out:
  64. return
  65. out = out["result"]
  66. if not "items" in out:
  67. center("Publication has no comments", "bdrd")
  68. return
  69. d = {"categories":["Tip LBC", "Comments", "Channel", "Preview"],
  70. "size":[1,1,2,5],
  71. "data":[]}
  72. try:
  73. # List what we found
  74. for n, i in enumerate(out["items"]):
  75. preview = "---!Failed Loading comment---"
  76. support = 0
  77. replies = 0
  78. bywho = "[anonymous]"
  79. try:
  80. comment = i["comment"]
  81. preview = comment.replace("\n", " ")
  82. support = i["support_amount"]
  83. bywho = i["channel_name"]
  84. replies = i["replies"]
  85. except:
  86. pass
  87. d["data"].append([support, replies, bywho, preview])
  88. table(d)
  89. # Tell the user that they might want to load more
  90. center("---type 'more' to load more---")
  91. page = page +1
  92. # Error messages
  93. except Exception as e:
  94. if "code" in out:
  95. center("Error code: "+out["code"], "bdrd")
  96. if "message" in out:
  97. center("Error: "+out["message"], "bdrd")
  98. else:
  99. center("Error: "+e, "bdrd")
  100. return
  101. while True:
  102. # Making sure that we stop every time a new page is reached
  103. c = input(typing_dots())
  104. if c == "more":
  105. break
  106. try:
  107. c = int(c)
  108. except:
  109. return
  110. view(out["items"][c])
  111. # Print the list again
  112. table(d)
  113. center("---type 'more' to load more---")
  114. def show_view_info(i, derived):
  115. comment = derived[0]
  116. preview = derived[1]
  117. support = derived[2]
  118. bywho = derived[3]
  119. replies = derived[4]
  120. # TIP LBC # COMMENTS ( REPLIES ) # CHANNEL
  121. d = {"categories":["Tip LBC", "Comments", "Channel"],
  122. "size":[1,1,3],
  123. "data":[[support, replies, bywho]]}
  124. table(d, False)
  125. # Preview
  126. d = {"categories":["Preview"],
  127. "size":[1],
  128. "data":[[preview]]}
  129. table(d, False)
  130. # The help thing
  131. center("--- for comment commands list type 'help' --- ")
  132. def view(i):
  133. """Allows the user to interact with and/or read a given comment"""
  134. preview = "---!Failed Loading comment---"
  135. comment = ""
  136. support = 0
  137. bywho = "[unknown]"
  138. replies = 0
  139. try:
  140. comment = i["comment"]
  141. preview = comment.replace("\n", " ")
  142. support = i["support_amount"]
  143. bywho = i["channel_name"]
  144. replies = i["replies"]
  145. except:
  146. pass
  147. derived = [comment, preview, support, bywho, replies]
  148. # List of commands for autocomplete feature.
  149. complete([
  150. "help",
  151. "read",
  152. "channel",
  153. "comments",
  154. "reply",
  155. "delete",
  156. "edit"
  157. ])
  158. # let's implement commands
  159. while True:
  160. show_view_info(i, derived)
  161. c = input(typing_dots())
  162. if not c:
  163. break
  164. elif c == "help":
  165. markdown.draw("help/comments.md", "Comments Help")
  166. elif c == "read":
  167. savedes = open("/tmp/fastlbrylastcomment.md", "w")
  168. savedes.write(comment)
  169. savedes.close()
  170. markdown.draw("/tmp/fastlbrylastcomment.md", "Full Text Of a Comment")
  171. elif c == "channel":
  172. channel.simple(i["channel_url"])
  173. elif c == "comments":
  174. list(i["claim_id"], " ", i["comment_id"],)
  175. elif c.startswith("reply"):
  176. c = c + ' '
  177. post(i["claim_id"], c[c.find(" "):], i["comment_id"])
  178. elif c == "delete":
  179. delete(i["comment_id"], bywho)
  180. elif c.startswith("edit"):
  181. c = c + ' '
  182. update(i, c[c.find(" "):])
  183. def post(claim_id, args, parent_id=""):
  184. # This will post a comment under either a publication or a
  185. # comment as a reply.
  186. editor = settings.get("default_editor")
  187. text = "Type your reply here. Don't forget to save. Then return to FastLBRY."
  188. if len(args) > 1:
  189. text = file_or_editor(args, text)
  190. else:
  191. if editor:
  192. text = file_or_editor(args, text, editor)
  193. else:
  194. text = input(typing_dots("Comment text", give_space=True))
  195. post_as, post_as_id = channel.select("Reply as who? Select Channel.", True)
  196. if not post_as.startswith("@"):
  197. post_as = "@"+post_as
  198. sigs = channel.sign(text, post_as)
  199. if not sigs:
  200. return
  201. params = {
  202. "channel_id": post_as_id,
  203. "channel_name": post_as,
  204. "claim_id": claim_id,
  205. "comment": text,
  206. **sigs
  207. }
  208. if parent_id:
  209. params["parent_id"] = parent_id
  210. out = comment_request("comment.Create", params)
  211. if not out:
  212. return
  213. if "error" in out:
  214. center("Error: "+out["error"]["message"], "bdrd")
  215. else:
  216. center("Comment is sent.", "bdgr")
  217. def inbox(opt=10):
  218. # This function will return the latest comments from the latest
  219. # publications. Similar to an email inbox. But with a limitation.
  220. # There is no system in the SDK to implement a history of comments
  221. # seamlessly. So then I need to cash a large file of comments. Or
  222. # do something clever. I think there will be a few options.
  223. # You noticed the opt=10 preset on the top. It's the default value.
  224. # Basically the user might type one of 4 things.
  225. # inbox
  226. # inbox 40 (or any number what so ever)
  227. # inbox all
  228. # inbox cashed
  229. # Each will run a slightly different algorithm to get the inbox
  230. # comments.
  231. # inbox
  232. # This will use the predefined 10 and read last 10 publications
  233. # comments to add. It will combine them with the pre-cashed ones
  234. # for the user to view. As you may imagine, giving it a number as
  235. # in:
  236. # inbox 40
  237. # inbox 2
  238. # inbox 50
  239. # Will load this number of publications. To update with them the
  240. # cash and then present it to the user.
  241. # inbox all
  242. # This one will take longest. But might be useful for some users.
  243. # This will go through all publications and cash comments from all
  244. # of them.
  245. # inbox cashed
  246. # This one is the fastest of them. It will only read the cash file
  247. # and present it to the user. So for instance you want to quickly
  248. # go back to the inbox without loading anything at all.
  249. try:
  250. opt = int(opt)
  251. reached = opt
  252. goal = opt
  253. except:
  254. goal = 0
  255. if opt == "all":
  256. reached = True
  257. else:
  258. reached = False
  259. # Updating the cash file ( inbox.json )
  260. page = 0
  261. items_total = 0
  262. current_item = 0
  263. try:
  264. with open(settings.get_settings_folder()+'inbox.json') as json_file:
  265. comments_cache = json.load(json_file)
  266. except:
  267. comments_cache = []
  268. checked_publications = []
  269. while reached > 0:
  270. if type(reached) == int:
  271. reached = reached - 50
  272. page = page + 1
  273. page_size = 50
  274. # Getting data about publications.
  275. if page != 1:
  276. out = check_output([flbry_globals["lbrynet"],
  277. "stream", "list",
  278. '--page='+str(page),
  279. '--page_size='+str(page_size),
  280. "--no_totals"])
  281. out2 = check_output([flbry_globals["lbrynet"],
  282. "channel", "list",
  283. '--page='+str(page),
  284. '--page_size='+str(page_size),
  285. "--no_totals"])
  286. else:
  287. out = check_output([flbry_globals["lbrynet"],
  288. "stream", "list",
  289. '--page='+str(page),
  290. '--page_size='+str(page_size)])
  291. out2 = check_output([flbry_globals["lbrynet"],
  292. "channel", "list",
  293. '--page='+str(page),
  294. '--page_size='+str(page_size)])
  295. # Now we want to parse the json
  296. items = []
  297. try:
  298. out = json.loads(out)
  299. out2 = json.loads(out2)
  300. items = out["items"]
  301. try:
  302. items = items[:int(opt)]
  303. except:
  304. pass
  305. for i in out2["items"]:
  306. items.append(i)
  307. except:
  308. break
  309. if not items:
  310. break
  311. if page == 1:
  312. # Getting Totals to calculate the progress bar
  313. if reached == True:
  314. items_total = out["total_items"] + out2["total_items"]
  315. else:
  316. try:
  317. items_total = int(opt) + out2["total_items"]
  318. except:
  319. items_total = 0
  320. # Reading items from the items
  321. for publication in items:
  322. # skip dublicate publications. ( like when you edited
  323. # a publication )
  324. if publication["name"] in checked_publications:
  325. continue
  326. checked_publications.append(publication["name"])
  327. current_item = current_item + 1
  328. # If above the requested amount.
  329. #if current_item > items_total:
  330. #break
  331. # Draw progress bar
  332. progress_bar(current_item, items_total, publication["name"])
  333. # let's now get all the comments
  334. claim_id = publication["claim_id"]
  335. comment_page = 0
  336. while True:
  337. comment_page = comment_page + 1
  338. params = {
  339. "claim_id": claim_id,
  340. "page": comment_page,
  341. "page_size": 50,
  342. "sort_by": 3,
  343. "top_level": False,
  344. }
  345. out = comment_request("comment.List", params)
  346. if not out:
  347. return
  348. cout = out["result"]
  349. # TODO: For now I'm stopping on first page when ever I'm
  350. # loading channel's comments ( community disscussion ).
  351. # This is obviously not going to work with "inbox all",
  352. # so please make the logic a bit smarter, so it will work.
  353. if "items" not in cout or publication["value_type"] == "channel":
  354. break
  355. for i in cout["items"]:
  356. # I want to add a few things into the comment data
  357. i["publication_url"] = publication["permanent_url"]
  358. i["publication_name"] = publication["name"]
  359. try:
  360. i["publication_title"] = publication["value"]["title"]
  361. except:
  362. i["publication_title"] = publication["name"]
  363. if i not in comments_cache:
  364. comments_cache.append(i)
  365. print()
  366. # Let's sort the comments based on the time they were sent
  367. comments_cache = sorted(comments_cache, key=lambda k: k['timestamp'], reverse=True)
  368. # Let's remove duplicate comments
  369. tmp = []
  370. tmp_ids = []
  371. for comment in comments_cache:
  372. if comment["comment_id"] not in tmp_ids:
  373. tmp_ids.append(comment["comment_id"])
  374. tmp.append(comment)
  375. comments_cache = tmp
  376. with open(settings.get_settings_folder()+'inbox.json', 'w') as fp:
  377. json.dump(comments_cache, fp , indent=4)
  378. # Now that we have comments cached and ready. I can start actually showing
  379. # them.
  380. w, h = tsize()
  381. page_size = (h-5)
  382. page = 0
  383. while True:
  384. d = {"categories":["Tip LBC", "Comments", "Publication", "Channel", "Preview"],
  385. "size":[1,1,4,2,4],
  386. "data":[]}
  387. items = []
  388. for n, i in enumerate(comments_cache):
  389. startfrom = int( page * page_size )
  390. endat = int( startfrom + page_size )
  391. if n in range(startfrom, endat):
  392. items.append(i)
  393. preview = "---!Failed Loading comment---"
  394. support = 0
  395. replies = 0
  396. where = "[some publication]"
  397. bywho = "[anonymous]"
  398. try:
  399. comment = i["comment"]
  400. preview = comment.replace("\n", " ")
  401. where = i["publication_title"]
  402. support = i["support_amount"]
  403. bywho = i["channel_name"]
  404. replies = i["replies"]
  405. except:
  406. pass
  407. d["data"].append([support, replies, where, bywho, preview])
  408. table(d)
  409. # Tell the user that they might want to load more
  410. center("---type 'more' to load more---")
  411. # Making sure that we stop every time a new page is reached
  412. c = input(typing_dots())
  413. if c == "more":
  414. page = page +1
  415. continue
  416. try:
  417. c = int(c)
  418. except:
  419. return
  420. view(items[c])
  421. c = input(typing_dots())
  422. def update(i, args):
  423. comment = i["comment"]
  424. comment_id = i["comment_id"]
  425. commenter = i["channel_name"]
  426. editor = settings.get("default_editor")
  427. if len(args) > 1:
  428. text = file_or_editor(args, comment)
  429. else:
  430. if editor:
  431. text = file_or_editor(args, comment, editor)
  432. else:
  433. print("Comment: "+comment)
  434. text = input(typing_dots("Edited comment", give_space=True))
  435. sigs = channel.sign(text, commenter)
  436. if not sigs:
  437. return
  438. params = {
  439. "comment": text,
  440. "comment_id": comment_id,
  441. **sigs
  442. }
  443. out = comment_request("comment.Edit", params)
  444. if "error" in out:
  445. center("Error updating comment: " + out["error"]["message"], "bdrd")
  446. center("Make sure you are editing a comment you posted.", "bdrd")
  447. elif "result" in out:
  448. center("Comment edited!", "bdgr")
  449. def delete(comment_id: str, commenter: str):
  450. """
  451. Deletes a comment you posted by its comment ID
  452. """
  453. sigs = channel.sign(comment_id, commenter)
  454. if not sigs:
  455. return
  456. params = {
  457. "comment_id": comment_id,
  458. **sigs
  459. }
  460. out = comment_request("comment.Abandon", params)
  461. if "result" in out:
  462. center("Comment deleted!", "bdgr")
  463. elif "error" in out:
  464. center("Error deleting comment: " + out["error"]["message"], "bdrd")
  465. center("Make sure you posted this comment.", "bdrd")
  466. def comment_request(method: str, params: dict):
  467. """
  468. Sends a request to the comment API
  469. """
  470. data = {
  471. "method": method,
  472. "id": 1,
  473. "jsonrpc":"2.0",
  474. "params": params
  475. }
  476. data = json.dumps(data).encode()
  477. headers = {
  478. "Content-Type": "application/json"
  479. }
  480. try:
  481. req = urllib.request.Request(flbry_globals["comment_api"], data, headers)
  482. res = urllib.request.urlopen(req)
  483. out = res.read().decode()
  484. return json.loads(out)
  485. except Exception as e:
  486. center("Comment Error: "+str(e))
  487. return