url.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. from flbry import channel
  37. from flbry import search
  38. from flbry import comments
  39. import urllib.request
  40. from flbry import following
  41. from flbry import settings
  42. from flbry import wallet
  43. def get(url=""):
  44. # The user might type the word url and nothing else.
  45. if not url:
  46. url = input(" LBRY url :: ")
  47. ##### Converting an HTTPS domain ( of any website ) into lbry url ###
  48. # Any variation of this:
  49. # https://odysee.com/@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  50. # customlbry.com/@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  51. # Should become this:
  52. # lbry://@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  53. if "/" in url and not url.startswith("@") and not url.startswith("lbry://"):
  54. if "@" in url:
  55. url = url[url.find("@"):]
  56. else:
  57. url = url[text.rfind("/")-1:]
  58. url = "lbry://"+url
  59. # Then let's fetch the url from our beloved SDK.
  60. out = check_output(["flbry/lbrynet",
  61. "resolve", url])
  62. # Now we want to parse the json
  63. try:
  64. out = json.loads(out)
  65. except:
  66. print(" Connect to LBRY first.")
  67. return
  68. out = out[url]
  69. #### FORCE SEARCH ###
  70. # Sometimes user might type something that is not a url
  71. # in this case ["value_type"] will not be loaded. And in
  72. # this case we can load search instead.
  73. if "value_type" not in out:
  74. search.simple(url)
  75. return
  76. # Now that we know that don't search for it. We can make
  77. # one thing less broken. Sometimes a user might type a
  78. # urls that's going to be resolved but that doesn't have
  79. # the lbry:// in the beginning of it. Like typing
  80. # @blenderdumbass instead of lbry://blenderdumbass
  81. # I want to add the lbry:// to it anyway. So non of the
  82. # stuff later will break.
  83. if not url.startswith("lbry://") and not url.startswith("@"):
  84. url = "lbry://" + url
  85. # Now let's print some useful information
  86. ##### NAME URL INFORMATION #####
  87. center("Publication Information")
  88. d = {"categories":["lbry url", "title"],
  89. "size":[1,1],
  90. "data":[[url]]}
  91. try:
  92. # This prints out the title
  93. d["data"][0].append(out["value"]["title"])
  94. except:
  95. d["data"][0] = [url]
  96. d["data"][0].append("[no title]")
  97. table(d, False)
  98. #### LICENSE ####
  99. try:
  100. d = {"categories":["License"],
  101. "size":[1],
  102. "data":[[out["value"]["license"]]]}
  103. except:
  104. d = {"categories":["License"],
  105. "size":[1],
  106. "data":[["[failed to load license]"]]}
  107. table(d, False)
  108. #### TAGS #####
  109. d = {"categories":[],
  110. "size":[],
  111. "data":[[]]}
  112. try:
  113. for tag in out["value"]["tags"]:
  114. d["categories"].append(" ")
  115. d["size"].append(1)
  116. d["data"][0].append(tag)
  117. except:
  118. d = {"categories":[" "],
  119. "size":[1],
  120. "data":[["[no tags found]"]]}
  121. table(d, False)
  122. #### FILE INFO #####
  123. d = {"categories":["Value Type", "File Type", "File Size", "Duration"],
  124. "size":[1,1,1, 1],
  125. "data":[[]]}
  126. try:
  127. d["data"][0].append(what[out["value_type"]])
  128. except:
  129. d["data"][0].append("[no value type]")
  130. try:
  131. d["data"][0].append(out["value"]["source"]["media_type"])
  132. except:
  133. d["data"][0].append("[no file type]")
  134. try:
  135. d["data"][0].append(csize(out["value"]["source"]["size"]))
  136. except:
  137. d["data"][0].append("[no file size]")
  138. try:
  139. d["data"][0].append(timestring(float(out["value"]["video"]["duration"])))
  140. except:
  141. d["data"][0].append("[no duration]")
  142. table(d, False)
  143. ##### CHANNEL INFORMATION ####
  144. center("Channel Information")
  145. d = {"categories":["lbry url", "title"],
  146. "size":[1,1],
  147. "data":[[]]}
  148. try:
  149. # This prints out the title
  150. d["data"][0].append(out["signing_channel"]["name"])
  151. title = "[no title]"
  152. try:
  153. title = out["signing_channel"]["value"]["title"]
  154. except:
  155. pass
  156. d["data"][0].append(title)
  157. except:
  158. d["data"][0] = []
  159. d["data"][0].append("[no url]")
  160. d["data"][0].append("[anonymous publisher]")
  161. table(d, False)
  162. #### LBC INFORMATION ####
  163. center("LBRY Coin ( LBC ) Information")
  164. d = {"categories":["combined", "at upload", "support"],
  165. "size":[1,1,1],
  166. "data":[[]]}
  167. try:
  168. fullamount = float(out["amount"]) + float(out["meta"]["support_amount"])
  169. # This prints out the title
  170. d["data"][0].append(fullamount)
  171. d["data"][0].append(out["amount"])
  172. d["data"][0].append(out["meta"]["support_amount"])
  173. except:
  174. d["data"][0] = []
  175. d["data"][0].append("[no data]")
  176. d["data"][0].append("[no data]")
  177. d["data"][0].append("[no data]")
  178. table(d, False)
  179. #### PRICE ####
  180. try:
  181. # Print the prince of this publication in LBC
  182. center("PRICE: "+out["value"]["fee"]["amount"]+" "+out["value"]["fee"]["currency"], "bdrd", blink=True)
  183. except:
  184. pass
  185. ### REPOST ####
  186. # Sometimes a user wants to select a repost. This will not
  187. # load anything of a value. A repost is an empty blob that
  188. # links to another blob. So I want to automatically load
  189. # the actuall publication it self here.
  190. if out["value_type"] == "repost":
  191. get(out["reposted_claim"]["canonical_url"])
  192. return
  193. # Some things are too big to output like this in the terminal
  194. # so for them I want the user to type a command.
  195. center("--- for publication commands list type 'help' --- ")
  196. # So we are going to start a new while loop here. IK crazy.
  197. # this one will handle all the commands associated with the
  198. # currently selected publication.
  199. # Completer thingy
  200. url_commands = [
  201. "help",
  202. "link",
  203. "id",
  204. "web",
  205. "description",
  206. "read",
  207. "channel",
  208. "comments",
  209. "reply",
  210. "open",
  211. "play",
  212. "save",
  213. "rss",
  214. "thumbnail",
  215. "follow",
  216. "unfollow",
  217. "support",
  218. "tip"
  219. ]
  220. complete(url_commands)
  221. settings_cache = settings.get_all_settings()
  222. while True:
  223. c = input(typing_dots())
  224. if not c:
  225. break
  226. elif c == "help":
  227. markdown.draw("help/url.md", "Publication Help")
  228. elif c == "web":
  229. d = {"categories":["Name", "URL", "Maintainer", "Interface"],
  230. "size":[1,2,1,1],
  231. "data":web_instances}
  232. table(d)
  233. center("")
  234. # Choose an instance
  235. which = input(typing_dots())
  236. try:
  237. print(" "+url.replace("lbry://", web_instances[int(which)][1]))
  238. except:
  239. print(" "+url.replace("lbry://", web_instances[0][1]))
  240. elif c == "link":
  241. print(" "+url)
  242. elif c == "id":
  243. print(" "+out["claim_id"])
  244. elif c.startswith("open"):
  245. if settings_cache["default_opener"]:
  246. p = settings_cache["default_opener"]
  247. else:
  248. # Selecting the software command in a smart way
  249. if len(c) < 6:
  250. p = input(" Open in : ")
  251. else:
  252. p = c[5:]
  253. Popen([p,
  254. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  255. stdout=DEVNULL,
  256. stderr=STDOUT)
  257. elif c == "description":
  258. #print(out["value"]["description"])
  259. # Here I want to print out the description of the publication.
  260. # but since, they are most likely in the markdown format I
  261. # need to implement a simple markdown parser. Oh wait.... I
  262. # have one. For the article read function. How about using it
  263. # here?
  264. # First we need to save the description into a file. Let's use
  265. # /tmp/ since there files are automatically cleaned up by the
  266. # system.
  267. try:
  268. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  269. savedes.write(out["value"]["description"])
  270. savedes.close()
  271. except:
  272. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  273. savedes.write("This file has no description.")
  274. savedes.close()
  275. # Now let's just simply load the markdown on this file.
  276. markdown.draw("/tmp/fastlbrylastdescription.md", "Description")
  277. elif c == "play":
  278. # Then we want to tell the SDK to start downloading.
  279. playout = check_output(["flbry/lbrynet",
  280. "get", url])
  281. # Parsing the Json
  282. playout = json.loads(playout)
  283. # Then we want to launch the player
  284. Popen([settings_cache["player"],
  285. playout['download_path']],
  286. stdout=DEVNULL,
  287. stderr=STDOUT)
  288. elif c == "music":
  289. # Then we want to tell the SDK to start downloading.
  290. playout = check_output(["flbry/lbrynet",
  291. "get", url])
  292. # Parsing the Json
  293. playout = json.loads(playout)
  294. # Then we want to launch the player
  295. Popen([settings.get("music_player"),
  296. playout['download_path']],
  297. stdout=DEVNULL,
  298. stderr=STDOUT)
  299. elif c == "save":
  300. # Then we want to tell the SDK to start downloading.
  301. playout = check_output(["flbry/lbrynet",
  302. "get", url])
  303. # Parsing the Json
  304. playout = json.loads(playout)
  305. print(" Saved to :", playout['download_path'])
  306. elif c == "read":
  307. # Then we want to tell the SDK to start downloading.
  308. playout = check_output(["flbry/lbrynet",
  309. "get", url])
  310. # Parsing the Json
  311. playout = json.loads(playout)
  312. # Present the article to the user.
  313. markdown.draw(playout['download_path'], out["value"]["title"])
  314. elif c == "channel":
  315. # This a weird one. If the publication is a channel we
  316. # want it to list the publications by that channel.
  317. # If a publication is a publication. We want it to list
  318. # publications by the channel that made the publication.
  319. if out["value_type"] == "channel":
  320. channel.simple(url)
  321. else:
  322. try:
  323. channel.simple(out["signing_channel"]["canonical_url"].replace("lbry://",""))
  324. except:
  325. center("Publication is anonymous", "bdrd")
  326. elif c == "comments":
  327. comments.list(out["claim_id"], url)
  328. elif c.startswith("reply"):
  329. c = c + ' '
  330. comments.post(out["claim_id"], c[c.find(" "):])
  331. elif c == "rss":
  332. if out["value_type"] == "channel":
  333. url = out["short_url"].replace("#", ":")
  334. if url.startswith("lbry://"):
  335. url = url.split("lbry://", 1)[1]
  336. print(" https://odysee.com/$/rss/"+url)
  337. else:
  338. try:
  339. url = out["signing_channel"]["short_url"].replace("#", ":")
  340. url = url.split("lbry://", 1)[1]
  341. print(" https://odysee.com/$/rss/"+url)
  342. except:
  343. center("Publication is anonymous!", "bdrd")
  344. elif c == "thumbnail":
  345. try:
  346. thumb_url = out["value"]["thumbnail"]["url"]
  347. urllib.request.urlretrieve(thumb_url, "/tmp/fastlbrythumbnail")
  348. Popen(["xdg-open",
  349. "/tmp/fastlbrythumbnail"],
  350. stdout=DEVNULL,
  351. stderr=STDOUT)
  352. except:
  353. center("Publication does not have a thumbnail", "bdrd")
  354. elif c == "follow":
  355. if out["value_type"] == "channel":
  356. try:
  357. name = out["value"]["title"]
  358. except:
  359. name = out["normalized_name"]
  360. url = out["permanent_url"]
  361. following.follow_channel(url, name)
  362. else:
  363. try:
  364. try:
  365. name = out["signing_channel"]["value"]["title"]
  366. except:
  367. name = out["signing_channel"]["normalized_name"]
  368. url = out["signing_channel"]["permanent_url"]
  369. following.follow_channel(url, name)
  370. except:
  371. center("Publication is anonymous.", "bdrd")
  372. elif c == "unfollow":
  373. if out["value_type"] == "channel":
  374. try:
  375. name = out["value"]["title"]
  376. except:
  377. name = out["normalized_name"]
  378. url = out["permanent_url"]
  379. following.unfollow_channel(url, name)
  380. else:
  381. try:
  382. try:
  383. name = out["signing_channel"]["value"]["title"]
  384. except:
  385. name = out["signing_channel"]["normalized_name"]
  386. url = out["signing_channel"]["permanent_url"]
  387. following.unfollow_channel(url, name)
  388. except:
  389. center("Publication is anonymous.", "bdrd")
  390. elif c == "support":
  391. wallet.support(out["claim_id"])
  392. elif c == "tip":
  393. wallet.support(out["claim_id"], True)
  394. complete(url_commands)