url.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. def get(url=""):
  36. # The user might type the word url and nothing else.
  37. if not url:
  38. url = input(" LBRY url :: ")
  39. else:
  40. print(" "+clr["bold"]+clr["bdma"]+"LBRY URL:",
  41. wdth(url, 78), clr["norm"])
  42. # Then let's fetch the url from our beloved SDK.
  43. out = check_output(["flbry/lbrynet",
  44. "resolve", url])
  45. # Now we want to parse the json
  46. try:
  47. out = json.loads(out)
  48. except:
  49. print(" Connect to LBRY first.")
  50. return
  51. out = out[url]
  52. # Now let's print some useful information
  53. try:
  54. # This prints out the title
  55. print(" "+clr["bold"]+clr["bbma"]+" TITLE:",
  56. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["value"]["title"], 78),
  57. clr["norm"])
  58. # This print out the channel name
  59. print(" "+clr["bold"]+clr["bdma"]+" CHANNEL:",
  60. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(out["signing_channel"]["name"], 38),
  61. clr["norm"]+clr["tbwh"]+clr["bdbu"]+wdth(out["signing_channel"]["value"]["title"], 39),
  62. clr["norm"]
  63. )
  64. except:
  65. pass
  66. try:
  67. # This will print the LBC amounts line
  68. fullamount = float(out["amount"]) + float(out["meta"]["support_amount"])
  69. print(" "+clr["bold"]+clr["bbma"]+" LBC:",
  70. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(fullamount, 19),
  71. clr["bold"]+clr["bbma"]+" UPLOAD:",
  72. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["amount"], 19),
  73. clr["bold"]+clr["bbma"]+" SUPPORT:",
  74. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["meta"]["support_amount"], 18),
  75. clr["norm"])
  76. except:
  77. pass
  78. try:
  79. # This line will print out the License.
  80. print(" "+clr["bold"]+clr["bdma"]+" LICENSE:",
  81. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(out["value"]["license"], 78),
  82. clr["norm"])
  83. # This line will print out file type and size of the file
  84. print(" "+clr["bold"]+clr["bbma"]+" TYPE:",
  85. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(out["value"]["source"]["media_type"], 33),
  86. clr["bold"]+clr["bbma"]+" SIZE:",
  87. clr["norm"]+clr["tbwh"]+clr["bbbu"]+wdth(csize(out["value"]["source"]["size"]), 34),
  88. clr["norm"])
  89. # This line will print out tags
  90. print(" "+clr["bold"]+clr["bdma"]+" TAGS:",
  91. clr["norm"]+clr["bold"]+clr["bdbu"]+wdth(" "+tgz(out["value"]["tags"]), 78),
  92. clr["norm"])
  93. except:
  94. pass
  95. # Some things are too big to output like this in the terminal
  96. # so for them I want the user to type a command.
  97. print(" "+clr["bold"]+clr["bdma"],
  98. " --- for publication commands list type help --- ",
  99. wdth(" ", 20), clr["norm"])
  100. # So we are going to start a new while loop here. IK crazy.
  101. # this one will handle all the commands associated with the
  102. # currently selected publication.
  103. while True:
  104. c = input(" ::: ")
  105. if not c:
  106. break
  107. elif c == "help":
  108. print("""
  109. Notice the ::: in this input. The tripple : like ::: means that this
  110. section is not related to the main section. Thus it has different
  111. functions.
  112. help - Returns this help message
  113. play - Downloads the file and opens it in a default application.
  114. save - Downloads the file only.
  115. https - Will load a direct file link in you default browser.
  116. odysee - Will open the odysee page.
  117. open - Will send an https link into a program of choice.
  118. description - Will print out the description.
  119. """)
  120. elif c == "https":
  121. Popen(['xdg-open',
  122. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  123. stdout=DEVNULL,
  124. stderr=STDOUT)
  125. elif c == "odysee":
  126. Popen(['xdg-open',
  127. url.replace("lbry://", "https://odysee.com/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  128. stdout=DEVNULL,
  129. stderr=STDOUT)
  130. elif c.startswith("open"):
  131. # Selecting the software command in a smart way
  132. if len(c) < 6:
  133. p = input(" Open in : ")
  134. else:
  135. p = c[5:]
  136. Popen([p,
  137. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  138. stdout=DEVNULL,
  139. stderr=STDOUT)
  140. elif c == "description":
  141. print("--------------| DESCRIPTION START |---------------")
  142. print(out["value"]["description"])
  143. print("--------------| DESCRIPTION END |---------------")
  144. elif c == "play":
  145. # Then we want to tell the SDK to start downloading.
  146. playout = check_output(["flbry/lbrynet",
  147. "get", url])
  148. # Parsing the Json
  149. playout = json.loads(playout)
  150. # Then we want to launch the player
  151. Popen(["xdg-open",
  152. playout['download_path']],
  153. stdout=DEVNULL,
  154. stderr=STDOUT)
  155. elif c == "save":
  156. # Then we want to tell the SDK to start downloading.
  157. playout = check_output(["flbry/lbrynet",
  158. "get", url])
  159. # Parsing the Json
  160. playout = json.loads(playout)
  161. print(" Saved to :", playout['download_path'])