url.py 31 KB

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