url.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 fetch an LBRY URL directly and print out various
  30. # options that the user may do with the publication.
  31. from subprocess import *
  32. import json
  33. import os
  34. from flbry.variables import *
  35. from flbry import markdown
  36. def get(url=""):
  37. # The user might type the word url and nothing else.
  38. if not url:
  39. url = input(" LBRY url :: ")
  40. else:
  41. print(" "+clr["bold"]+clr["bdma"]+"LBRY URL:",
  42. wdth(url, 78), clr["norm"])
  43. # Then let's fetch the url from our beloved SDK.
  44. out = check_output(["flbry/lbrynet",
  45. "resolve", url])
  46. # Now we want to parse the json
  47. try:
  48. out = json.loads(out)
  49. except:
  50. print(" Connect to LBRY first.")
  51. return
  52. out = out[url]
  53. # Now let's print some useful information
  54. try:
  55. # This prints out the title
  56. print(" "+clr["bold"]+clr["bbma"]+" TITLE:",
  57. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["value"]["title"], 78),
  58. clr["norm"])
  59. # This print out the channel name
  60. print(" "+clr["bold"]+clr["bdma"]+" CHANNEL:",
  61. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(out["signing_channel"]["name"], 38),
  62. clr["norm"]+clr["tbwh"]+clr["bdbu"]+wdth(out["signing_channel"]["value"]["title"], 39),
  63. clr["norm"]
  64. )
  65. except:
  66. pass
  67. try:
  68. # This will print the LBC amounts line
  69. fullamount = float(out["amount"]) + float(out["meta"]["support_amount"])
  70. print(" "+clr["bold"]+clr["bbma"]+" LBC:",
  71. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(fullamount, 19),
  72. clr["bold"]+clr["bbma"]+" UPLOAD:",
  73. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["amount"], 19),
  74. clr["bold"]+clr["bbma"]+" SUPPORT:",
  75. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["meta"]["support_amount"], 18),
  76. clr["norm"])
  77. except:
  78. pass
  79. try:
  80. # This line will print out the License.
  81. print(" "+clr["bold"]+clr["bdma"]+" LICENSE:",
  82. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(out["value"]["license"], 78),
  83. clr["norm"])
  84. # This line will print out file type and size of the file
  85. print(" "+clr["bold"]+clr["bbma"]+" TYPE:",
  86. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["value"]["source"]["media_type"], 33),
  87. clr["bold"]+clr["bbma"]+" SIZE:",
  88. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(csize(out["value"]["source"]["size"]), 34),
  89. clr["norm"])
  90. # This line will print out tags
  91. print(" "+clr["bold"]+clr["bdma"]+" TAGS:",
  92. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(" "+tgz(out["value"]["tags"]), 78),
  93. clr["norm"])
  94. except:
  95. pass
  96. # Some things are too big to output like this in the terminal
  97. # so for them I want the user to type a command.
  98. print(" "+clr["bold"]+clr["bdma"],
  99. " --- for publication commands list type help --- ",
  100. wdth(" ", 20), clr["norm"])
  101. # So we are going to start a new while loop here. IK crazy.
  102. # this one will handle all the commands associated with the
  103. # currently selected publication.
  104. while True:
  105. c = input(typing_dots())
  106. if not c:
  107. break
  108. elif c == "help":
  109. markdown.draw("help/url.md", "Publication Help")
  110. elif c == "https":
  111. Popen(['xdg-open',
  112. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  113. stdout=DEVNULL,
  114. stderr=STDOUT)
  115. elif c == "odysee":
  116. Popen(['xdg-open',
  117. url.replace("lbry://", "https://odysee.com/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  118. stdout=DEVNULL,
  119. stderr=STDOUT)
  120. elif c.startswith("open"):
  121. # Selecting the software command in a smart way
  122. if len(c) < 6:
  123. p = input(" Open in : ")
  124. else:
  125. p = c[5:]
  126. Popen([p,
  127. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  128. stdout=DEVNULL,
  129. stderr=STDOUT)
  130. elif c == "description":
  131. #print(out["value"]["description"])
  132. # Here I want to print out the description of the publication.
  133. # but since, they are most likely in the markdown format I
  134. # need to implement a simple markdown parser. Oh wait.... I
  135. # have one. For the article read function. How about using it
  136. # here?
  137. # First we need to save the description into a file. Let's use
  138. # /tmp/ since there files are automatically cleaned up by the
  139. # system.
  140. try:
  141. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  142. savedes.write(out["value"]["description"])
  143. savedes.close()
  144. except:
  145. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  146. savedes.write("This file has no description.")
  147. savedes.close()
  148. # Now let's just simply load the markdown on this file.
  149. markdown.draw("/tmp/fastlbrylastdescription.md", "Description")
  150. elif c == "play":
  151. # Then we want to tell the SDK to start downloading.
  152. playout = check_output(["flbry/lbrynet",
  153. "get", url])
  154. # Parsing the Json
  155. playout = json.loads(playout)
  156. # Then we want to launch the player
  157. Popen(["xdg-open",
  158. playout['download_path']],
  159. stdout=DEVNULL,
  160. stderr=STDOUT)
  161. elif c == "save":
  162. # Then we want to tell the SDK to start downloading.
  163. playout = check_output(["flbry/lbrynet",
  164. "get", url])
  165. # Parsing the Json
  166. playout = json.loads(playout)
  167. print(" Saved to :", playout['download_path'])
  168. elif c == "read":
  169. # Then we want to tell the SDK to start downloading.
  170. playout = check_output(["flbry/lbrynet",
  171. "get", url])
  172. # Parsing the Json
  173. playout = json.loads(playout)
  174. # Present the article to the user.
  175. markdown.draw(playout['download_path'], out["value"]["title"])