url.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 time
  33. import json
  34. import os
  35. from flbry.variables import *
  36. from flbry import markdown
  37. from flbry import channel
  38. from flbry import search
  39. from flbry import comments
  40. import urllib.request
  41. from flbry import following
  42. from flbry import settings
  43. from flbry import wallet
  44. from flbry import plugin
  45. from flbry import channel
  46. from flbry import analytics
  47. import urllib.parse
  48. def print_url_info(url, out):
  49. """Prints some information about the URL"""
  50. ##### NAME URL INFORMATION #####
  51. center("Publication Information")
  52. d = {"categories":["lbry url", "title"],
  53. "size":[1,1],
  54. "data":[[url]]}
  55. try:
  56. # This prints out the title
  57. d["data"][0].append(out["value"]["title"])
  58. except:
  59. d["data"][0] = [url]
  60. d["data"][0].append("[no title]")
  61. table(d, False)
  62. #### LICENSE ####
  63. try:
  64. dateis = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(out["value"]["release_time"])))
  65. except:
  66. dateis = "[Failed to load release date]"
  67. try:
  68. licenseis = out["value"]["license"]
  69. except:
  70. licenseis = "[Failed to load License]"
  71. d = {"categories":["License", "Release Date"],
  72. "size":[1,1],
  73. "data":[[licenseis, dateis]]}
  74. table(d, False)
  75. #### TAGS #####
  76. d = {"categories":[],
  77. "size":[],
  78. "data":[[]]}
  79. try:
  80. for tag in out["value"]["tags"]:
  81. d["categories"].append(" ")
  82. d["size"].append(1)
  83. d["data"][0].append(tag)
  84. except:
  85. d = {"categories":[" "],
  86. "size":[1],
  87. "data":[["[no tags found]"]]}
  88. table(d, False)
  89. #### FILE INFO #####
  90. d = {"categories":["Value Type", "File Type", "File Size", "Duration"],
  91. "size":[1,1,1, 1],
  92. "data":[[]]}
  93. try:
  94. d["data"][0].append(what[out["value_type"]])
  95. except:
  96. d["data"][0].append("[no value type]")
  97. try:
  98. d["data"][0].append(out["value"]["source"]["media_type"])
  99. except:
  100. d["data"][0].append("[no file type]")
  101. try:
  102. d["data"][0].append(csize(out["value"]["source"]["size"]))
  103. except:
  104. d["data"][0].append("[no file size]")
  105. try:
  106. d["data"][0].append(timestring(float(out["value"]["video"]["duration"])))
  107. except:
  108. d["data"][0].append("[no duration]")
  109. table(d, False)
  110. ##### CHANNEL INFORMATION ####
  111. center("Channel Information")
  112. d = {"categories":["lbry url", "title"],
  113. "size":[1,1],
  114. "data":[[]]}
  115. try:
  116. # This prints out the title
  117. d["data"][0].append(out["signing_channel"]["name"])
  118. title = "[no title]"
  119. try:
  120. title = out["signing_channel"]["value"]["title"]
  121. except:
  122. pass
  123. d["data"][0].append(title)
  124. except:
  125. d["data"][0] = []
  126. d["data"][0].append("[no url]")
  127. d["data"][0].append("[anonymous publisher]")
  128. table(d, False)
  129. #### LBC INFORMATION ####
  130. center("LBRY Coin ( LBC ) Information")
  131. d = {"categories":["combined", "at upload", "support"],
  132. "size":[1,1,1],
  133. "data":[[]]}
  134. try:
  135. fullamount = float(out["amount"]) + float(out["meta"]["support_amount"])
  136. # This prints out the title
  137. d["data"][0].append(fullamount)
  138. d["data"][0].append(out["amount"])
  139. d["data"][0].append(out["meta"]["support_amount"])
  140. except:
  141. d["data"][0] = []
  142. d["data"][0].append("[no data]")
  143. d["data"][0].append("[no data]")
  144. d["data"][0].append("[no data]")
  145. table(d, False)
  146. #### PRICE ####
  147. try:
  148. # Print the prince of this publication in LBC
  149. center("PRICE: "+out["value"]["fee"]["amount"]+" "+out["value"]["fee"]["currency"], "bdrd", blink=True)
  150. except:
  151. pass
  152. # Some things are too big to output like this in the terminal
  153. # so for them I want the user to type a command.
  154. center("--- for publication commands list type 'help' --- ")
  155. def get(url="", do_search=True):
  156. """Allows user to interact with the given URL"""
  157. # The user might type the word url and nothing else.
  158. if not url:
  159. url = input(typing_dots("LBRY url", give_space=True, to_add_dots=True))
  160. # Decode the URL to turn percent encoding into normal characters
  161. # Example: "%C3%A9" turns into "é"
  162. url = urllib.parse.unquote(url)
  163. ##### Converting an HTTPS domain ( of any website ) into lbry url ###
  164. # Any variation of this:
  165. # https://odysee.com/@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  166. # customlbry.com/@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  167. # Should become this:
  168. # lbry://@blenderdumbass:f/hacking-fastlbry-live-with-you:6
  169. if "/" in url and not url.startswith("@") and not url.startswith("lbry://"):
  170. if "@" in url:
  171. url = url[url.find("@"):]
  172. else:
  173. url = url[text.rfind("/")-1:]
  174. # Some web interfaces pass data through URLs using a '?'.
  175. # This is not valid for a LBRY URL, so we remove everything
  176. # after it to avoid errors.
  177. url = url.split("?")[0]
  178. url = "lbry://"+url
  179. # If a url looks like it might be a claim id, try to get the claim from it
  180. if len(url) == 40 and not url.startswith("lbry://"):
  181. x = check_output([flbry_globals["lbrynet"], "claim", "search", "--claim_id="+url])
  182. try:
  183. x = json.loads(x)
  184. except:
  185. center("Connect to LBRY first.", "bdrd")
  186. return
  187. if len(x["items"]) == 1:
  188. # If "items" has something in it we know it was a claim id
  189. out = x["items"][0]
  190. url = out["canonical_url"]
  191. else:
  192. # Let's fetch the url from our beloved SDK.
  193. out = check_output([flbry_globals["lbrynet"],
  194. "resolve", url])
  195. # Now we want to parse the json
  196. try:
  197. out = json.loads(out)
  198. except:
  199. center("Connect to LBRY first.", "bdrd")
  200. return
  201. out = out[url]
  202. # In case there are plugins that want to modify resolved data.
  203. out = plugin.run(out)
  204. ### REPOST ####
  205. # Sometimes a user wants to select a repost. This will not
  206. # load anything of a value. A repost is an empty blob that
  207. # links to another blob. So I want to automatically load
  208. # the actuall publication it self here.
  209. if "value_type" in out and out["value_type"] == "repost":
  210. get(out["reposted_claim"]["canonical_url"])
  211. return
  212. #### FORCE SEARCH ###
  213. # Sometimes user might type something that is not a url
  214. # in this case ["value_type"] will not be loaded. And in
  215. # this case we can load search instead.
  216. if "value_type" not in out and do_search:
  217. search.simple(url)
  218. return out
  219. elif "value_type" not in out:
  220. return out
  221. # Now that we know that don't search for it. We can make
  222. # one thing less broken. Sometimes a user might type a
  223. # urls that's going to be resolved but that doesn't have
  224. # the lbry:// in the beginning of it. Like typing
  225. # @blenderdumbass instead of lbry://@blenderdumbass
  226. # I want to add the lbry:// to it anyway. So none of the
  227. # stuff later will break.
  228. if not url.startswith("lbry://"):
  229. url = "lbry://" + url
  230. print_url_info(url, out)
  231. # So we are going to start a new while loop here. IK crazy.
  232. # this one will handle all the commands associated with the
  233. # currently selected publication.
  234. # Completer thingy
  235. url_commands = [
  236. "help",
  237. "link",
  238. "id",
  239. "web",
  240. "description",
  241. "read",
  242. "channel",
  243. "comments",
  244. "reply",
  245. "open",
  246. "play",
  247. "music",
  248. "save",
  249. "rss",
  250. "thumbnail",
  251. "follow",
  252. "unfollow",
  253. "boost",
  254. "tip",
  255. "repost",
  256. "analytics",
  257. "sales"
  258. ]
  259. complete(url_commands)
  260. settings_cache = settings.get_all_settings()
  261. while True:
  262. plugin.run(execute=False)
  263. c = input(typing_dots())
  264. # Some strings are only one line, so it's a waste to reprint the info.
  265. reprint = True
  266. # Some commands were changing url, which we don't want, so now
  267. # it uses a temporary variable instead.
  268. tmpurl = ""
  269. if not c:
  270. break
  271. elif c == "help":
  272. markdown.draw("help/url.md", "Publication Help")
  273. elif c == "web":
  274. print_web_instance(url)
  275. elif c == "link":
  276. center(url)
  277. reprint = False
  278. elif c == "id":
  279. center(out["claim_id"])
  280. reprint = False
  281. elif c.startswith("open"):
  282. # If open has an argument (like `open mpv`) it opens it in that
  283. # Next it tries to open it with the default opener
  284. # If neither of those find an opener, it prompts the user.
  285. if len(c) > 5:
  286. p = c[5:]
  287. elif settings_cache["default_opener"]:
  288. p = settings_cache["default_opener"]
  289. else:
  290. p = input(typing_dots("Open in"))
  291. p = p.split()
  292. Popen([*p,
  293. url.replace("lbry://", "https://spee.ch/").replace("#", ":").replace("(", "%28").replace(")", "%29")],
  294. stdout=DEVNULL,
  295. stderr=STDOUT)
  296. elif c == "description":
  297. # Here I want to print out the description of the publication.
  298. # but since, they are most likely in the markdown format I
  299. # need to implement a simple markdown parser. Oh wait.... I
  300. # have one. For the article read function. How about using it
  301. # here?
  302. # First we need to save the description into a file. Let's use
  303. # /tmp/ since there files are automatically cleaned up by the
  304. # system.
  305. try:
  306. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  307. savedes.write(out["value"]["description"])
  308. savedes.close()
  309. except:
  310. savedes = open("/tmp/fastlbrylastdescription.md", "w")
  311. savedes.write("This file has no description.")
  312. savedes.close()
  313. # Now let's just simply load the markdown on this file.
  314. markdown.draw("/tmp/fastlbrylastdescription.md", "Description")
  315. elif c.startswith("play"):
  316. # Then we want to tell the SDK to start downloading.
  317. playout = check_output([flbry_globals["lbrynet"],
  318. "get", url, "--save_file=True"])
  319. # Parsing the JSON
  320. playout = json.loads(playout)
  321. # Same thing as in open
  322. if len(c) > 5:
  323. p = c[5:]
  324. elif settings_cache["player"]:
  325. p = settings_cache["player"]
  326. else:
  327. p = input(typing_dots("Play in"))
  328. p = p.split()
  329. # Then we want to launch the player
  330. Popen([*p,
  331. playout['download_path']],
  332. stdout=DEVNULL,
  333. stderr=STDOUT)
  334. elif c.startswith("music"):
  335. # Then we want to tell the SDK to start downloading.
  336. playout = check_output([flbry_globals["lbrynet"],
  337. "get", url])
  338. # Parsing the Json
  339. playout = json.loads(playout)
  340. # And again for music player
  341. if len(c) > 6:
  342. p = c[6:]
  343. elif settings_cache["music_player"]:
  344. p = settings_cache["music_player"]
  345. else:
  346. p = input(typing_dots("Play in"))
  347. p = p.split()
  348. # Then we want to launch the player
  349. Popen([*p,
  350. playout['download_path']],
  351. stdout=DEVNULL,
  352. stderr=STDOUT)
  353. elif c == "save":
  354. # Then we want to tell the SDK to start downloading.
  355. playout = check_output([flbry_globals["lbrynet"],
  356. "get", url, "--save_file=True"])
  357. # Parsing the Json
  358. playout = json.loads(playout)
  359. center("Saved to "+playout['download_path'])
  360. reprint = False
  361. elif c == "read":
  362. # Then we want to tell the SDK to start downloading.
  363. playout = check_output([flbry_globals["lbrynet"],
  364. "get", url, "--save_file=True"])
  365. # Parsing the Json
  366. playout = json.loads(playout)
  367. # Present the article to the user.
  368. markdown.draw(playout['download_path'], out["value"]["title"])
  369. elif c == "channel":
  370. # This a weird one. If the publication is a channel we
  371. # want it to list the publications by that channel.
  372. # If a publication is a publication. We want it to list
  373. # publications by the channel that made the publication.
  374. if out["value_type"] == "channel":
  375. channel.simple(url)
  376. else:
  377. try:
  378. channel.simple(out["signing_channel"]["canonical_url"].replace("lbry://",""))
  379. except:
  380. center("Publication is anonymous", "bdrd")
  381. elif c == "comments":
  382. comments.list(out["claim_id"], url)
  383. elif c.startswith("reply"):
  384. c = c + ' '
  385. comments.post(out["claim_id"], c[c.find(" "):])
  386. elif c == "rss":
  387. if out["value_type"] == "channel":
  388. tmpurl = out["short_url"].replace("#", ":")
  389. if tmpurl.startswith("lbry://"):
  390. tmpurl = url.split("lbry://", 1)[1]
  391. center("https://odysee.com/$/rss/"+tmpurl)
  392. else:
  393. try:
  394. tmpurl = out["signing_channel"]["short_url"].replace("#", ":")
  395. tmpurl = tmpurl.split("lbry://", 1)[1]
  396. center("https://odysee.com/$/rss/"+tmpurl)
  397. except:
  398. center("Publication is anonymous!", "bdrd")
  399. reprint = False
  400. elif c == "thumbnail":
  401. try:
  402. thumb_url = out["value"]["thumbnail"]["url"]
  403. urllib.request.urlretrieve(thumb_url, "/tmp/fastlbrythumbnail")
  404. Popen(["xdg-open",
  405. "/tmp/fastlbrythumbnail"],
  406. stdout=DEVNULL,
  407. stderr=STDOUT)
  408. except:
  409. center("Publication does not have a thumbnail", "bdrd")
  410. elif c == "follow":
  411. if out["value_type"] == "channel":
  412. try:
  413. name = out["value"]["title"]
  414. except:
  415. name = out["normalized_name"]
  416. tmpurl = out["permanent_url"]
  417. following.follow_channel(tmpurl, name)
  418. else:
  419. try:
  420. try:
  421. name = out["signing_channel"]["value"]["title"]
  422. except:
  423. name = out["signing_channel"]["normalized_name"]
  424. tmpurl = out["signing_channel"]["permanent_url"]
  425. following.follow_channel(tmpurl, name)
  426. except:
  427. center("Publication is anonymous.", "bdrd")
  428. elif c == "unfollow":
  429. if out["value_type"] == "channel":
  430. try:
  431. name = out["value"]["title"]
  432. except:
  433. name = out["normalized_name"]
  434. tmpurl = out["permanent_url"]
  435. following.unfollow_channel(tmpurl, name)
  436. else:
  437. try:
  438. try:
  439. name = out["signing_channel"]["value"]["title"]
  440. except:
  441. name = out["signing_channel"]["normalized_name"]
  442. tmpurl = out["signing_channel"]["permanent_url"]
  443. following.unfollow_channel(tmpurl, name)
  444. except:
  445. center("Publication is anonymous.", "bdrd")
  446. elif c.startswith("boost"):
  447. if " " in c:
  448. wallet.support(out["claim_id"], amount=c[c.find(" ")+1:])
  449. else:
  450. wallet.support(out["claim_id"])
  451. elif c.startswith("tip"):
  452. if " " in c:
  453. wallet.support(out["claim_id"], amount=c[c.find(" ")+1:], tip=True)
  454. else:
  455. wallet.support(out["claim_id"], tip=True)
  456. elif c.startswith("repost"):
  457. name = input(typing_dots("Name (enter for name of publication)", give_space=True))
  458. if not name:
  459. name = out["normalized_name"]
  460. a = c.split()
  461. try:
  462. bid = a[1]
  463. except:
  464. bid = input(typing_dots("Bid"))
  465. claim_id = out["claim_id"]
  466. ch, chid = channel.select("Channel to repost to:", True)
  467. repost_out = check_output([flbry_globals["lbrynet"], "stream", "repost", "--name="+name, "--bid="+bid, "--claim_id="+claim_id, "--channel_id="+chid])
  468. repost_out = json.loads(repost_out)
  469. if "message" in repost_out:
  470. center("Error reposting: "+repost_out["message"], "bdrd")
  471. else:
  472. center("Successfully reposted to "+ch, "bdgr")
  473. elif c == "sales":
  474. items = analytics.get_data(out["claim_id"])
  475. try:
  476. analytics.graph_loop(items)
  477. except Exception as e:
  478. print(e)
  479. print()
  480. elif c == "analytics":
  481. items = analytics.get_data(out["claim_id"], mode="analytics")
  482. try:
  483. analytics.graph_loop(items)
  484. except Exception as e:
  485. print(e)
  486. print()
  487. elif c:
  488. out = plugin.run(out, command=c)
  489. complete(url_commands)
  490. # Print the publication information again
  491. if reprint:
  492. print()
  493. print_url_info(url, out)