url.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. import os
  30. import time
  31. import urllib.request
  32. import threading
  33. import json
  34. from subprocess import *
  35. from gi.repository import Gtk
  36. from gi.repository import Gdk
  37. from gi.repository import GLib
  38. from gi.repository import Pango
  39. from gi.repository import GdkPixbuf
  40. from PIL import Image, ImageSequence
  41. from flbry import markdown
  42. from flbry import data_view
  43. from flbry import ui
  44. from flbry import fetch
  45. from flbry import claim_search
  46. from flbry import comments
  47. from flbry import follow
  48. from flbry import publish
  49. from flbry import analytics
  50. from flbry import livestreams
  51. from flbry import suggest
  52. from flbry import odysee
  53. from flbry import oscalls
  54. def resolve(w, win, url):
  55. # This function will draw a widget of resolved url
  56. #####################################################
  57. # RESOLVING PART ( NO GTK ) #
  58. #####################################################
  59. out = fetch.lbrynet("resolve",
  60. {"urls":[url]}
  61. )
  62. out = out[url]
  63. # Saving the resolved thing into the win for later use
  64. win.resolved = out
  65. # out = check_output(["flbry/lbrynet",
  66. # "resolve", url])
  67. # raw_data = out.decode("utf-8")
  68. # # Now we want to parse the json
  69. # try:
  70. # out = json.loads(out)
  71. # out = out[url]
  72. # except:
  73. # print("Resolve Failed")
  74. # return False
  75. try:
  76. is_channel = "value_type" in out and out["value_type"] == "channel"
  77. percentage = downloaded(out["claim_id"])
  78. except:
  79. # If this fails we activate search
  80. return ["search", False, False, w, win, url, False]
  81. # make sure the url path starts with lbry://
  82. if not url.startswith("lbry://"):
  83. url = "lbry://"+url
  84. win.url.set_text(url)
  85. if is_channel:
  86. stream_data = livestreams.get_data(out["claim_id"])
  87. else:
  88. try:
  89. stream_data = livestreams.get_data(out["signing_channel"]["claim_id"])
  90. except:
  91. stream_data = {}
  92. # Aditional data
  93. out["address_is_mine"] = fetch.lbrynet("address_is_mine", {"address":out["address"]})
  94. out["supports_preview"] = fetch.lbrynet("support_abandon", {"claim_id":out["claim_id"],
  95. "preview":True})
  96. return [out, is_channel, percentage, w, win, url, stream_data]
  97. def render_resolve(data):
  98. out, is_channel, percentage, w, win, url, stream_data = data
  99. # It could be failed
  100. if out == "search":
  101. return ui.load(win, claim_search.find, claim_search.render, win, win.url.get_text(), [], 1, {"order_by":""})
  102. #####################################################
  103. # DRAWING PART ( GTK ) #
  104. #####################################################
  105. try:
  106. price = out["value"]["fee"]["amount"]
  107. except:
  108. price = 0
  109. try:
  110. currency = out["value"]["fee"]["currency"]
  111. except:
  112. currency = "LBC"
  113. box = Gtk.HBox()
  114. outbox = Gtk.VBox()
  115. #### THUMBNAIL ###
  116. thumb = ""
  117. try:
  118. thumb = out["value"]["thumbnail"]["url"]
  119. thumb_url = ui.image_save_name(thumb)
  120. def thumb_open(w):
  121. oscalls.Open(thumb_url)
  122. thumb_button = Gtk.Button()
  123. thumb_button.set_tooltip_text(thumb)
  124. if is_channel:
  125. thumb_image = ui.load(win, ui.net_image_calculation, ui.net_image_render, thumb, 150, "", True)
  126. else:
  127. thumb_image = ui.load(win, ui.net_image_calculation, ui.net_image_render, thumb, 400, "", True)
  128. thumb_button.add(thumb_image)
  129. thumb_button.set_relief(Gtk.ReliefStyle.NONE)
  130. thumb_button.connect("clicked", thumb_open)
  131. box.pack_start(thumb_button, False, False, False)
  132. except:
  133. pass
  134. outbox.pack_start(box, False, False, False)
  135. from_right_to_thumbnail = Gtk.VBox()
  136. box.pack_end(from_right_to_thumbnail, True, True, False)
  137. # If channel load banner
  138. if is_channel:
  139. if "cover" in out["value"]:
  140. # This is a hack to set the scroller at the center
  141. # of the cover image
  142. def render_cover(calc):
  143. # So this fucntion pretends to be the net_image_render
  144. r = ui.net_image_render(calc) # It does call it though
  145. # But we need the size of the image and we need to do
  146. # something after the image is loaded.
  147. def wait():
  148. # This will execute after a delay
  149. v = channel_scroll.get_vadjustment()
  150. h = channel_scroll.get_hadjustment()
  151. # TODO: Check that the math is correct. It seems
  152. # to move the scrolls a bit too far.
  153. v.set_value( r.get_pixbuf().get_height() / 2 - 75)
  154. h.set_value( r.get_pixbuf().get_width() / 2 - 100)
  155. def start_waiting():
  156. # This is the delay thread
  157. time.sleep(0.1)
  158. GLib.idle_add(wait) # Making sure to use GLib so not
  159. # to get Segmentation Fault
  160. load_thread = threading.Thread(target=start_waiting)
  161. load_thread.start()
  162. # And we need to give the renderer the image itself, so it
  163. # could be added into the UI.
  164. return r # This happens before we scroll to the center
  165. # So think about it:
  166. # 1. ui.load loads the ui.net_image_calclation which downloads the cover
  167. # 2. This function runs. Activating ui.net_image_render getting the Gtk.Image
  168. # 3. We setup the thread that waits for 0.1 seconds
  169. # 4. Meanwhile ui.load recieves the ui.image and window updates with it
  170. # 5. 0.1 seconds later: we calculate the width and height of the image
  171. # and set the scroll adjustments accordilgly.
  172. # Simple isn't it?
  173. channel_banner = ui.load(win, ui.net_image_calculation, render_cover, out["value"]["cover"]["url"], False, "", True)
  174. channel_scroll = Gtk.ScrolledWindow()
  175. channel_scroll.set_size_request(400,150)
  176. channel_scroll.add_with_viewport(channel_banner)
  177. from_right_to_thumbnail.pack_start(channel_scroll, False, False, False)
  178. else:
  179. try:
  180. channel_scroll.destroy()
  181. except:
  182. pass
  183. #### NAME / CHANNEL ####
  184. the_packing_box = from_right_to_thumbnail
  185. if is_channel:
  186. the_packing_box = outbox
  187. name_channel_box = Gtk.VBox()
  188. the_packing_box.pack_start(name_channel_box, False, False, False)
  189. # Channel
  190. if "signing_channel" in out:
  191. chbox = Gtk.HBox()
  192. name_channel_box.pack_start(chbox, False, False, 0)
  193. chbox.pack_start(ui.go_to_channel(win, out["signing_channel"]),True,True,False)
  194. fbutton = follow.button(win, out["signing_channel"]["permanent_url"])
  195. chbox.pack_start(fbutton, False, False, 0 )
  196. elif is_channel:
  197. fbutton = follow.button(win, out["permanent_url"])
  198. name_channel_box.pack_start(fbutton, False, False, False)
  199. # name it self
  200. title = out["name"]
  201. try:
  202. title = out["value"]["title"]
  203. except:
  204. pass
  205. title_label = Gtk.Label()
  206. title_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  207. title_label.set_line_wrap(True)
  208. title_label.set_markup('<span size="x-large"> '+title+'</span> ')
  209. title_label.set_selectable(True)
  210. title_label.set_css_name("")
  211. name_channel_box.pack_start(title_label, False, False, False)
  212. ################# TOOL BAR ##################
  213. print("streamdata ++++++++++++++++++++++++++++++++++++ ", stream_data)
  214. toolbox = Gtk.HBox()
  215. the_packing_box.pack_start(toolbox, False, False,False)
  216. # The channel might be live from a different publication. I want to give a button to resolve it.
  217. active_id = stream_data.get("data",{}).get("ActiveClaim", {}).get("ClaimID", "")
  218. if stream_data.get("data",{}).get("Live", False) and active_id != out["claim_id"]:
  219. live_elsewhere_box = Gtk.HBox()
  220. the_packing_box.pack_start(Gtk.HSeparator(), False, False, 5)
  221. the_packing_box.pack_start(live_elsewhere_box, False, False,False)
  222. live_elsewhere_box.pack_start(Gtk.VSeparator(), 0,0,10)
  223. live_elsewhere_box.pack_start(ui.icon(win, "dialog-warning"), 0,0,0)
  224. live_elsewhere_box.pack_start(Gtk.Label(" Currently Streaming! "), 0,0,0)
  225. def go_to_livestream(widget):
  226. resolve_url = fetch.lbrynet("claim_search", {"claim_id":active_id}).get("items", [{}])[0].get("canonical_url","")
  227. win.resolve_tab = "new_tab"
  228. win.url.set_text(resolve_url)
  229. win.url.activate()
  230. go_button = Gtk.Button()
  231. go_button.set_relief(Gtk.ReliefStyle.NONE)
  232. go_box = Gtk.HBox()
  233. go_button.add(go_box)
  234. go_box.pack_start(ui.icon(win, "go-jump"), 0,0,0)
  235. go_box.pack_start(Gtk.Label(" Go To Livestream "),0,0,0)
  236. go_button.connect("clicked", go_to_livestream)
  237. live_elsewhere_box.pack_start(go_button, 0,0,0)
  238. if not is_channel:
  239. # LIVESTREAM LAUNCH
  240. if stream_data.get("data",{}).get("Live", False) and active_id == out["claim_id"]:
  241. def live_launch_action(w):
  242. suggest.record_tags_to_suggestions(out.get("value", {}).get("tags", []))
  243. Popen([win.settings["live_stream_player"], win.settings["librarian_instance"]+"/live/content/"+out["signing_channel"]["claim_id"]+"/master.m3u8"])
  244. live_launch_button = Gtk.Button()
  245. live_launch_button.connect("clicked", live_launch_action)
  246. live_launch_button.set_relief(Gtk.ReliefStyle.NONE)
  247. live_launch_box = Gtk.HBox()
  248. live_launch_button.add(live_launch_box)
  249. live_launch_icon = ui.icon(win, "media-playback-start")
  250. live_launch_box.pack_start(live_launch_icon, False, False, False)
  251. live_launch_box.pack_start(Gtk.Label(" Watch Livestream "), False, False, False)
  252. toolbox.pack_start(live_launch_button, False,False,False)
  253. else:
  254. def download_action(w):
  255. suggest.record_tags_to_suggestions(out.get("value", {}).get("tags", []))
  256. start_downloading(url)
  257. download_button = Gtk.Button()
  258. download_button.connect("clicked", download_action)
  259. download_button.set_relief(Gtk.ReliefStyle.NONE)
  260. download_box = Gtk.HBox()
  261. download_button.add(download_box)
  262. download_icon = ui.icon(win, "go-down")
  263. download_box.pack_start(download_icon, False, False, False)
  264. #filesize
  265. labeltext = " Download"
  266. if price:
  267. labeltext = " Buy for "+str(price)+" "+currency
  268. try:
  269. filesize = out["value"]["source"]["size"]
  270. labeltext = labeltext + " ("+csize(filesize)+")"
  271. except:
  272. filesize = 0
  273. download_box.pack_start(Gtk.Label(labeltext), False, False, False)
  274. toolbox.pack_start(download_button, False,False,False)
  275. download_bar = Gtk.ProgressBar()
  276. the_packing_box.pack_start(download_bar, False, False,False)
  277. def delete_action(w):
  278. filename = get_downloaded_file(out["claim_id"])
  279. delete_file(out["claim_id"])
  280. try:
  281. os.remove(filename)
  282. except:
  283. pass
  284. delete_button = Gtk.Button()
  285. delete_button.connect("clicked", delete_action)
  286. delete_button.set_relief(Gtk.ReliefStyle.NONE)
  287. delete_box = Gtk.HBox()
  288. delete_button.add(delete_box)
  289. delete_icon = ui.icon(win,"edit-delete")
  290. delete_box.pack_start(delete_icon, False, False, False)
  291. delete_box.pack_start(Gtk.Label(" Delete "), False, False, False)
  292. toolbox.pack_start(delete_button, False,False,False)
  293. def launch_action(w):
  294. oscalls.Open(get_downloaded_file(out["claim_id"]))
  295. launch_button = Gtk.Button()
  296. launch_button.connect("clicked", launch_action)
  297. launch_button.set_relief(Gtk.ReliefStyle.NONE)
  298. launch_box = Gtk.HBox()
  299. launch_button.add(launch_box)
  300. launch_icon = ui.icon(win, "media-playback-start")
  301. launch_box.pack_start(launch_icon, False, False, False)
  302. launch_box.pack_start(Gtk.Label(" Launch "), False, False, False)
  303. toolbox.pack_start(launch_button, False,False,False)
  304. win.download_buttons[out["claim_id"]] = True
  305. t = threading.Thread(target=downloading_check_thread, args=(win, out["claim_id"],download_button, delete_button, launch_button, download_bar))
  306. t.setDaemon(True)
  307. t.start()
  308. def kill_daemon(w):
  309. win.download_buttons[out["claim_id"]] = False
  310. download_button.connect("destroy", kill_daemon)
  311. #################### REPOST BUTTON #######################
  312. try:
  313. toolbox.pack_start(Gtk.HSeparator(), False,False,5)
  314. def repost_action(w):
  315. print("name", repost_name_entry.get_text())
  316. print("bid", repost_bid_entry.get_value())
  317. print("channel", win.channel["name"])
  318. print("channel_id", win.channel["claim_id"])
  319. repost_out = fetch.lbrynet("stream_repost",
  320. {"name":repost_name_entry.get_text(),
  321. "bid":str(float(repost_bid_entry.get_value())),
  322. "claim_id":out["claim_id"],
  323. "channel_id":win.channel["claim_id"]})
  324. print(repost_out)
  325. if "error" in repost_out:
  326. ui.notify(win, "Error while reposting", str(repost_out["error"]))
  327. else:
  328. ui.notify(win, "Reposted succesfully.", "lbry://"+win.channel["name"]+"/"+repost_name_entry.get_text())
  329. repost_menu.hide()
  330. repost_menu = Gtk.Popover()
  331. repost_menu_box = Gtk.VBox()
  332. repost_menu.add(repost_menu_box)
  333. # Repost requires 3 entries of data:
  334. # Name:
  335. def on_url(w):
  336. w.set_text(publish.lbryname(w.get_text(), force=False))
  337. repost_name_box = Gtk.HBox()
  338. repost_menu_box.pack_start(repost_name_box, 0,0,0)
  339. channel_name = win.channel.get("name", "")
  340. repost_name_box.pack_start(Gtk.Label(" lbry://"), 0,0,0)
  341. repost_name_entry = Gtk.Entry()
  342. repost_name_entry.connect("changed", on_url)
  343. repost_name_box.pack_start(repost_name_entry, 1,1,0)
  344. # Bid:
  345. repost_bid_box = Gtk.HBox()
  346. repost_menu_box.pack_start(repost_bid_box, 0,0,0)
  347. bid_adjust = Gtk.Adjustment(0.01,
  348. lower=0.0001,
  349. upper=1000000000,
  350. step_increment=0.1)
  351. repost_bid_entry = Gtk.SpinButton(adjustment=bid_adjust,
  352. digits=4)
  353. repost_bid_box.pack_start(Gtk.Label(" Bid: "), False, False, 0)
  354. repost_bid_box.pack_end(repost_bid_entry, False, False, 0)
  355. # Repost button
  356. do_repost_button = Gtk.Button()
  357. do_repost_button.connect("clicked", repost_action)
  358. do_repost_button.set_relief(Gtk.ReliefStyle.NONE)
  359. do_repost_box = Gtk.HBox()
  360. do_repost_box.pack_start(ui.icon(win, "media-playlist-repeat"), 0,0,0)
  361. do_repost_box.pack_start(Gtk.Label(" Do Re-Post "), False, False, False)
  362. do_repost_button.add(do_repost_box)
  363. repost_menu_box.pack_start(do_repost_button, 0,0,0)
  364. repost_menu_box.show_all()
  365. repost_button = Gtk.MenuButton(popover=repost_menu)
  366. repost_button.set_relief(Gtk.ReliefStyle.NONE)
  367. repost_box = Gtk.HBox()
  368. repost_button.add(repost_box)
  369. repost_icon = ui.icon(win, "media-playlist-repeat")
  370. repost_box.pack_start(repost_icon, False, False, False)
  371. try:
  372. reposted_times = out["meta"]["reposted"]
  373. if not reposted_times:
  374. 1/0 # kill switch to go straig to except LOL. I'm hacking. What
  375. # do you want from me.
  376. repost_label = " Re-Post ( "+str(reposted_times)+" ) "
  377. except:
  378. repost_label = " Re-Post "
  379. repost_box.pack_start(Gtk.Label(repost_label), False, False, False)
  380. toolbox.pack_start(repost_button, False,False,False)
  381. except Exception as e:
  382. print("Re-Post error: ", e)
  383. ############# SUPPORT BUTTON ############
  384. support_menu = Gtk.Popover()
  385. support_menu_box = Gtk.VBox()
  386. support_menu.add(support_menu_box)
  387. # Bid:
  388. support_bid_box = Gtk.HBox()
  389. support_menu_box.pack_start(support_bid_box, 0,0,0)
  390. support_bid_adjust = Gtk.Adjustment(0.01,
  391. lower=0.0001,
  392. upper=1000000000,
  393. step_increment=0.1)
  394. support_bid_entry = Gtk.SpinButton(adjustment=support_bid_adjust,
  395. digits=4)
  396. support_bid_box.pack_start(Gtk.Label(" Amount: "), False, False, 0)
  397. support_bid_box.pack_end(support_bid_entry, False, False, 0)
  398. # Do button
  399. def support_action(w):
  400. print("support amount", support_bid_entry.get_value())
  401. print("channel", win.channel["name"])
  402. print("channel_id", win.channel["claim_id"])
  403. support_out = fetch.lbrynet("support_create",
  404. {"amount":str(float(support_bid_entry.get_value())),
  405. "claim_id":out["claim_id"],
  406. "channel_id":win.channel["claim_id"],
  407. "tip":True})
  408. print(support_out)
  409. if "error" in support_out:
  410. ui.notify(win, "Error while reposting", str(support_out["error"]))
  411. else:
  412. ui.notify(win, "Supported succesfully.")
  413. support_menu.hide()
  414. do_support_button = Gtk.Button()
  415. do_support_button.connect("clicked", support_action)
  416. do_support_button.set_relief(Gtk.ReliefStyle.NONE)
  417. do_support_box = Gtk.HBox()
  418. do_support_box.pack_start(ui.icon(win, "emblem-favorite"), 0,0,0)
  419. do_support_box.pack_start(Gtk.Label(" Do Support "), False, False, False)
  420. do_support_button.add(do_support_box)
  421. support_menu_box.pack_start(do_support_button, 0,0,0)
  422. support_menu_box.show_all()
  423. support_button = Gtk.MenuButton(popover=support_menu)
  424. support_button.set_relief(Gtk.ReliefStyle.NONE)
  425. support_box = Gtk.HBox()
  426. support_button.add(support_box)
  427. support_icon = ui.icon(win, "emblem-favorite")
  428. support_box.pack_start(support_icon, False, False, False)
  429. try:
  430. rounded = round(float(out["meta"]["effective_amount"]), 2)
  431. if not rounded:
  432. rounded = out["meta"]["effective_amount"]
  433. support_label = " Support ( "+str(rounded)+" ) "
  434. except Exception as e:
  435. print(e)
  436. support_label = " Support "
  437. support_box.pack_start(Gtk.Label(support_label), False, False, False)
  438. toolbox.pack_start(support_button, False,False,False)
  439. ############# ADITIONAL MENU #############
  440. aditional_menu = Gtk.Popover()
  441. aditional_menu_box = Gtk.VBox()
  442. aditional_menu.add(aditional_menu_box)
  443. aditional_menu_button = Gtk.MenuButton(popover=aditional_menu)
  444. aditional_menu_button.add(ui.icon(win, "view-more"))
  445. aditional_menu_button.set_relief(Gtk.ReliefStyle.NONE)
  446. toolbox.pack_end(aditional_menu_button, False,False,False)
  447. # If the publication is mine.
  448. ## Unlock Tips ##
  449. unlock_menu = Gtk.Popover()
  450. unlock_menu_box = Gtk.VBox()
  451. unlock_menu.add(unlock_menu_box)
  452. unlock_button = Gtk.MenuButton(popover=unlock_menu)
  453. unlock_button.set_relief(Gtk.ReliefStyle.NONE)
  454. unlock_bbox = Gtk.HBox()
  455. unlock_button.add(unlock_bbox)
  456. unlock_bbox.pack_start(ui.icon(win, "insert-object-symbolic"), 0,0,0)
  457. unlock_bbox.pack_start(Gtk.Label("Unlock Tips"), 0,0,5)
  458. aditional_menu_box.pack_start(unlock_button, 0,0,5)
  459. unlockable = float(out["supports_preview"].get("total_output", 0))
  460. unlock_menu_box.pack_start(Gtk.Label(" You can unlock up to: "+str(unlockable)+" LBC "), 0,0,5)
  461. unlock_counter_box = Gtk.HBox()
  462. unlock_menu_box.pack_start(unlock_counter_box, 0,0,5)
  463. unlock_amount_adjust = Gtk.Adjustment(0.01,
  464. lower=0,
  465. upper=unlockable,
  466. step_increment=0.1)
  467. unlock_amount_entry = Gtk.SpinButton(adjustment=unlock_amount_adjust,
  468. digits=4)
  469. unlock_counter_box.pack_start(Gtk.Label(" Unlock:"), 0,0,5)
  470. unlock_counter_box.pack_end(unlock_amount_entry, 0,0,5)
  471. def do_unlock_action(w):
  472. result = fetch.lbrynet("support_abandon", {"claim_id":out["claim_id"],
  473. "keep": str(unlockable - unlock_amount_entry.get_value())})
  474. print("\n\nUNLOCKING RESULT:\n\n", result, "\n\n")
  475. unlock_menu.hide()
  476. aditional_menu.hide()
  477. do_unlock_button = Gtk.Button()
  478. do_unlock_button.set_relief(Gtk.ReliefStyle.NONE)
  479. do_unlock_button.connect("clicked", do_unlock_action)
  480. do_unlock_box = Gtk.HBox()
  481. do_unlock_button.add(do_unlock_box)
  482. do_unlock_box.pack_start(ui.icon(win, "insert-object-symbolic"), 0,0,0)
  483. do_unlock_box.pack_start(Gtk.Label(" Do Unlock "), 0,0,0)
  484. unlock_menu_box.pack_start(do_unlock_button, 0,0,5)
  485. unlock_menu_box.show_all()
  486. aditional_menu_box.show_all()
  487. ########### BOTTOM NOTEBOOK ##############
  488. notebook = Gtk.Notebook()
  489. notebook.set_scrollable(True)
  490. outbox.pack_start(notebook, True, True,False)
  491. # If article read article
  492. try:
  493. if out["value"]["source"]["media_type"] == "text/markdown" and price == 0:
  494. suggest.record_tags_to_suggestions(out.get("value", {}).get("tags", []))
  495. # We download it first
  496. playout = fetch.lbrynet("get", {"uri":url, "save_file":True})
  497. # playout = check_output(["flbry/lbrynet",
  498. # "get", url])
  499. # # Parsing the Json
  500. # playout = json.loads(playout)
  501. md_text = open(playout['download_path'])
  502. md_text = md_text.read()
  503. # Markdown covenreted
  504. md_scrl = Gtk.ScrolledWindow()
  505. md_view = Gtk.TextView()
  506. md_view.set_wrap_mode(Gtk.WrapMode.WORD )
  507. md_buffer = md_view.get_buffer()
  508. md_view.set_editable(False)
  509. md_scrl.add(md_view)
  510. md_buffer.set_text(md_text)
  511. markdown.convert(win, md_view)
  512. detailsbox = Gtk.HBox()
  513. detailsbox.pack_start(ui.icon(win, "text-x-generic"), False, False, False)
  514. detailsbox.pack_start(Gtk.Label(" Read Article "), True, True, True)
  515. detailsbox.show_all()
  516. notebook.append_page(md_scrl, detailsbox)
  517. # Markdown source
  518. md_scrl = Gtk.ScrolledWindow()
  519. md_view = Gtk.TextView()
  520. #md_view.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0.2,0.2,0.2, 1))
  521. #md_view.override_color(Gtk.StateType.NORMAL, Gdk.RGBA(0.9,0.9,0.9, 1))
  522. md_view.override_font(Pango.FontDescription("Monospace"))
  523. md_view.set_wrap_mode(Gtk.WrapMode.WORD )
  524. md_buffer = md_view.get_buffer()
  525. md_view.set_editable(False)
  526. md_scrl.add(md_view)
  527. md_buffer.set_text(md_text)
  528. detailsbox = Gtk.HBox()
  529. detailsbox.pack_start(ui.icon(win, "text-x-preview"), False, False, False)
  530. detailsbox.pack_start(Gtk.Label(" Source of Article "), True, True, True)
  531. detailsbox.show_all()
  532. notebook.append_page(md_scrl, detailsbox)
  533. except Exception as e:
  534. print("FUCKING ERROR")
  535. print(e)
  536. print()
  537. # Channel Uploads / Publications
  538. if is_channel:
  539. uploads_box = ui.load(win, claim_search.find, claim_search.render, win, "", [out["claim_id"]], )
  540. detailsbox = Gtk.HBox()
  541. detailsbox.pack_start(ui.icon(win, "folder-remote"), False, False, False)
  542. detailsbox.pack_start(Gtk.Label(" Publications "), True, True, True)
  543. detailsbox.show_all()
  544. notebook.append_page(uploads_box, detailsbox)
  545. ##### DESCRIPTION ####
  546. try:
  547. description_scrl = Gtk.ScrolledWindow()
  548. description_scrl.set_size_request(500,200)
  549. description_field = Gtk.TextView()
  550. description_field.set_wrap_mode(Gtk.WrapMode.WORD )
  551. description_field.set_editable(False)
  552. description_buffer = description_field.get_buffer()
  553. description_buffer.set_text(out["value"]["description"])
  554. markdown.convert(win, description_field)
  555. description_box = Gtk.VBox()
  556. detailsbox = Gtk.HBox()
  557. detailsbox.pack_start(ui.icon(win, "text-x-generic"), False, False, False)
  558. detailsbox.pack_start(Gtk.Label(" Description "), True, True, True)
  559. detailsbox.show_all()
  560. notebook.append_page(description_scrl, detailsbox)
  561. description_box.pack_start(description_scrl, False, False, False)
  562. description_scrl.add(description_field)
  563. except:
  564. pass
  565. ######## COMMENTS ########
  566. com_box = Gtk.VPaned()
  567. com_box.set_position(250)
  568. com_scrl = Gtk.ScrolledWindow()
  569. com_scrl.set_vexpand(0)
  570. #com_scrl.set_hexpand(0)
  571. comments_widget = ui.load(win, comments.list_comments, comments.render_comments, win, out )
  572. com_scrl.add(comments_widget)
  573. detailsbox = Gtk.HBox()
  574. detailsbox.pack_start(ui.icon(win, "document-send"), False, False, False)
  575. detailsbox.pack_start(Gtk.Label(" Comments "), True, True, True)
  576. detailsbox.show_all()
  577. com_box.add1(comments.comment_input(win, out["claim_id"]))
  578. com_box.add2(com_scrl)
  579. notebook.append_page(com_box, detailsbox)
  580. ##### Details #######
  581. # Almost like Raw Data but shows only the important stuff
  582. details = {"LBRY URL: ":url,
  583. "Price: ":str(price)+" "+str(currency)}
  584. try:
  585. details["Claim ID: "] = out["claim_id"]
  586. except:
  587. pass
  588. try:
  589. details["Upload Bid: "] = out["amount"]+" LBC"
  590. except:
  591. pass
  592. try:
  593. details["Support: "] = out["meta"]["support_amount"]+" LBC"
  594. except:
  595. pass
  596. try:
  597. if is_channel:
  598. details["Odysee Subscribers: "] = odysee.get_odysee_subs(win, out["claim_id"])[0]
  599. else:
  600. details["Odysee Views: "] = odysee.get_odysee_views(win, out["claim_id"])[0]
  601. except:
  602. pass
  603. try:
  604. details["Filename: "] = out["value"]["source"]["name"]
  605. except:
  606. pass
  607. try:
  608. details["File Size:"] = csize(filesize)
  609. except:
  610. pass
  611. try:
  612. details["License: "] = out["value"]["license"]
  613. except:
  614. pass
  615. try:
  616. details["Released at: "] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(out["value"]["release_time"])))
  617. except:
  618. pass
  619. try:
  620. #print(out["value"]["tags"])
  621. details["Tags: "] = out["value"]["tags"]
  622. except:
  623. pass
  624. det_scrl = Gtk.ScrolledWindow()
  625. det_view = data_view.data_widget(details)
  626. det_scrl.add(det_view)
  627. detailsbox = Gtk.HBox()
  628. detailsbox.pack_start(ui.icon(win, "dialog-information"), False, False, False)
  629. detailsbox.pack_start(Gtk.Label(" Details "), True, True, True)
  630. detailsbox.show_all()
  631. notebook.append_page(det_scrl, detailsbox)
  632. ######### ANALYTICS GRAPH #######
  633. plot_chart = fetch.lbrynet("txo_plot", { "days_back":1000, # Fetch 100 days of txo
  634. "exclude_internal_transfers":True, # Without crap
  635. "is_not_my_input":True, # Not from me ( as in support only )
  636. "claim_id":out["claim_id"]
  637. })
  638. if plot_chart:
  639. chart_box = Gtk.VBox()
  640. detailsbox = Gtk.HBox()
  641. detailsbox.pack_start(ui.icon(win, "text-csv"), False, False, False)
  642. detailsbox.pack_start(Gtk.Label(" Analytics "), True, True, True)
  643. detailsbox.show_all()
  644. notebook.append_page(chart_box, detailsbox)
  645. graph_data = {"items":[],
  646. "zoom":[0,0],
  647. "allow_negative":False
  648. }
  649. for i in plot_chart:
  650. a = {}
  651. a["amount"] = i["total"]
  652. a["timestamp"] = int(time.mktime(time.strptime(i["day"],"%Y-%m-%d")))
  653. graph_data["items"].append(a)
  654. the_graph = analytics.graph(win, graph_data, "Analytics")
  655. chart_box.pack_start(the_graph,1,1,1)
  656. try:
  657. t = title
  658. if is_channel:
  659. t = ""
  660. uploads_box = ui.load(win, claim_search.find, claim_search.render, win, t, [], 1, {"any_tags":out["value"]["tags"] , "claim_type":out["value_type"]} )
  661. detailsbox = Gtk.HBox()
  662. detailsbox.pack_start(ui.icon(win, "folder-remote"), False, False, False)
  663. detailsbox.pack_start(Gtk.Label(" Similar "), True, True, True)
  664. detailsbox.show_all()
  665. notebook.append_page(uploads_box, detailsbox)
  666. except:
  667. pass
  668. ##### Raw Data #######
  669. raw_scrl = Gtk.ScrolledWindow()
  670. raw_view = data_view.data_widget(out)
  671. raw_scrl.add(raw_view)
  672. detailsbox = Gtk.HBox()
  673. detailsbox.pack_start(ui.icon(win, "dialog-warning"), False, False, False)
  674. detailsbox.pack_start(Gtk.Label(" Extra Details "), True, True, True)
  675. detailsbox.show_all()
  676. notebook.append_page(raw_scrl, detailsbox)
  677. #outbox.show_all()
  678. return outbox
  679. def downloaded(claim_id):
  680. # Returns a fraction ( from 0 to 1 ) of the download
  681. # percentage. If it's a 0, we can use it to display
  682. # the download button.
  683. #out = check_output(["flbry/lbrynet",
  684. # "file", "list", "--claim_id="+claim_id])
  685. out = fetch.lbrynet("file_list", {"claim_id":claim_id})
  686. #print(out, '\n\n')
  687. try:
  688. #out = json.loads(out)
  689. out = out["items"][0]
  690. if out["status"] == "finished":
  691. return 1
  692. else:
  693. return out["written_bytes"] / out["total_bytes"]
  694. except:
  695. return 0
  696. def get_downloaded_file(claim_id):
  697. #out = check_output(["flbry/lbrynet",
  698. # "file", "list", "--claim_id="+claim_id])
  699. out = fetch.lbrynet("file_list", {"claim_id":claim_id})
  700. try:
  701. #out = json.loads(out)
  702. out = out["items"][0]
  703. return out["download_path"]
  704. except:
  705. return ""
  706. def delete_file(claim_id):
  707. #check_output(["flbry/lbrynet",
  708. # "file", "delete", "--claim_id="+claim_id])
  709. fetch.lbrynet("file_delete", {"claim_id":claim_id})
  710. def start_downloading(url):
  711. out = fetch.lbrynet("get", {"uri":url, "save_file":True})
  712. def downloading_check_thread(win, claim_id,
  713. download_button,
  714. delete_button,
  715. launch_button,
  716. progress_bar):
  717. # This is a thread that will toggle buttons on/off
  718. # based on a curretly downloading file.
  719. def update(fraction):
  720. if not fraction: # if it's 0
  721. download_button.set_visible(True)
  722. delete_button.set_visible(False)
  723. launch_button.set_visible(False)
  724. progress_bar.set_visible(False)
  725. else:
  726. download_button.set_visible(False)
  727. delete_button.set_visible(True)
  728. launch_button.set_visible(True)
  729. progress_bar.set_visible(True)
  730. progress_bar.set_fraction(fraction)
  731. if fraction == 1:
  732. progress_bar.set_visible(False)
  733. while True:
  734. fraction = downloaded(claim_id)
  735. GLib.idle_add(update, fraction)
  736. time.sleep(2) # The new algorithm is too fast LOL
  737. if not win.download_buttons[claim_id]:
  738. return
  739. def csize(x):
  740. x = float(x)
  741. l = ["B","KB", "MB", "GB", "TB"]
  742. for i in range(5):
  743. if x > 1024:
  744. x = x / 1024
  745. else:
  746. return str(round(x, 2))+" "+l[i]
  747. return str(round(x, 2))+" "+l[i]