run.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. # Preparing the executable of lbrynet to be running. If we don't that
  30. # there will be a nasty error of permission being denied.
  31. import os
  32. os.system("chmod u+x flbry/lbrynet")
  33. # I'm not trying to make it pretty. I'm trying to make it snappy.
  34. # It's FastLBRY not SlowLBRY.
  35. import gi
  36. import threading
  37. gi.require_version('Gtk', '3.0')
  38. from gi.repository import Gtk
  39. from gi.repository import Gdk
  40. from gi.repository import GLib
  41. # It's gonna take a couple of things to make the button run
  42. from flbry import connect
  43. from flbry import parse
  44. from flbry import settings
  45. from flbry import url
  46. from flbry import ui
  47. from flbry import claim_search
  48. from flbry import fetch
  49. from flbry import publish
  50. settings.make_sure_file_exists()
  51. ##########################################################################
  52. # #
  53. # THE MAIN WINDOW OF FASTLBRY GTK #
  54. # #
  55. ##########################################################################
  56. win = Gtk.Window() # Make a GTK window object
  57. win.connect("destroy", Gtk.main_quit) # If you close the window, program quits
  58. win.set_title("FastLBRY GTK") # Title
  59. win.set_default_icon_from_file("icon.png") # Icon
  60. win.set_size_request(800, 900) # Minimum size
  61. ##########################################################################
  62. # #
  63. # GLOBAL VARIABLES HACK ( adding things to 'win' ) #
  64. # #
  65. ##########################################################################
  66. # This hack is very simple. Our main Gtk.Window() is a class to which we
  67. # can add variables later on. Like the example you see below.
  68. # This way, giving some function only 'win' and nothing else, will give
  69. # that other function the entire list of global variable ( added this way ).
  70. win.settings = settings.load() # Settings
  71. win.SDK_running = connect.check() # Is the SDK running ( from 0 to 1 to draw progress bar)
  72. win.resolved = {} # Last resolved urls
  73. win.commenting = {} # Data for while chatting in comments
  74. ##########################################################################
  75. # #
  76. # TOP PANNEL #
  77. # #
  78. ##########################################################################
  79. # Gtk.HeaderBar() is like a box but it draws itself in the headerbar insead
  80. # of in the window itself.
  81. pannel = Gtk.HeaderBar()
  82. pannel.set_show_close_button(True)
  83. win.set_titlebar(pannel) # Dumbasses wrote GTK
  84. # Untill the user connected to the SDK, search and other functions should
  85. # not work. So we split the pannel into two parts. The other one is a
  86. # normal box.
  87. restofpannel = Gtk.HBox() # H for Horrizontal
  88. pannel.pack_end(restofpannel)
  89. ############## CONNECT BUTTON ################
  90. # Connect button will also be a disconnect button
  91. # Icons for both
  92. icon_connect = ui.icon(win, "connect")
  93. icon_disconnect = ui.icon(win, "disconnect")
  94. # Boxes for both
  95. connect_box = Gtk.HBox()
  96. disconnect_box = Gtk.HBox()
  97. # Connect box
  98. connect_box.pack_start(icon_connect, False, False, False)
  99. connect_box.set_tooltip_text("Connect to LBRY Network")
  100. connect_box.pack_start(Gtk.Label("Connect "), False, False, False)
  101. # Disconnect box
  102. disconnect_box.pack_start(icon_disconnect, False, False, False)
  103. disconnect_box.set_tooltip_text("Disconnect from LBRY Network")
  104. disconnect_box.pack_start(Gtk.Label("Disconnect "), False, False, False)
  105. # By whether SDK was running while the software was launched
  106. # we present the correct button to the user and set the rest-of-pannel
  107. # accordingly.
  108. if win.SDK_running:
  109. title = disconnect_box
  110. restofpannel.set_sensitive(True)
  111. else:
  112. title = connect_box
  113. restofpannel.set_sensitive(False)
  114. connect_disconncet_button = Gtk.Button()
  115. connect_disconncet_button.add(title)
  116. connect_disconncet_button.set_relief(Gtk.ReliefStyle.NONE)
  117. pannel.pack_start(connect_disconncet_button)#, False, False, False)
  118. ############### CONNECT / DISCONNECT BUTTONS FUNCTION ################
  119. # The button should do something.
  120. def connect_disconnect_function(w):
  121. w.set_sensitive(False)
  122. for i in dynamic_box.get_children():
  123. i.destroy()
  124. progress_connect = Gtk.ProgressBar(show_text=True)
  125. dynamic_box.pack_start(progress_connect, True, True, 30)
  126. win.show_all()
  127. def do_sdk(w, pb):
  128. wasrunning = win.SDK_running
  129. if win.SDK_running == 1:
  130. connect.stop()
  131. else:
  132. connect.start()
  133. def update_pb(pb):
  134. pb.set_fraction(win.SDK_running)
  135. pb.set_text("Connecting: "+str(int(round(win.SDK_running*100)))+"%")
  136. def update_buttons(w):
  137. for i in dynamic_box.get_children():
  138. i.destroy()
  139. w.get_child().destroy()
  140. if win.SDK_running == 1:
  141. w.add(disconnect_box)
  142. restofpannel.set_sensitive(True)
  143. else:
  144. w.add(connect_box)
  145. restofpannel.set_sensitive(False)
  146. w.show_all()
  147. w.set_sensitive(True)
  148. if win.SDK_running == 1:
  149. load_following()
  150. load_channel(win)
  151. ui.notify(win, "Connected to LBRY")
  152. if wasrunning == 0:
  153. while True:
  154. win.SDK_running = connect.check()
  155. if win.SDK_running == 1:
  156. break
  157. GLib.idle_add(update_pb, pb)
  158. GLib.idle_add(update_buttons, w)
  159. load_thread = threading.Thread(target=do_sdk, args=(w, progress_connect))
  160. load_thread.setDaemon(True)
  161. load_thread.start()
  162. connect_disconncet_button.connect("clicked", connect_disconnect_function)
  163. ##########################################################################
  164. # #
  165. # SEARCH / URL BAR #
  166. # #
  167. ##########################################################################
  168. # Note that I add this directly into the 'win' to make it a global variable.
  169. # This let's me to start any url from anywhere in the software.
  170. win.url = Gtk.Entry() # Gtk.SearchEntry() also looks good, but nah
  171. win.url.set_size_request(400,40)
  172. restofpannel.pack_start(win.url, True, True, False)
  173. ####### SEARCH FUNCTION #######
  174. def search(w):
  175. # DRAG AND DROP FOR PUBLISH
  176. if win.url.get_text().startswith("file://") or os.path.exists(win.url.get_text()):
  177. # TODO: Some filenames include weird characters like %20 for spacebar and
  178. # stuff like that. We need to filter that out.
  179. filepath = win.url.get_text().replace("file://", "")
  180. publish.window(win, {"file_path":filepath})
  181. win.url.set_text("")
  182. return
  183. for i in dynamic_box.get_children():
  184. i.destroy()
  185. ## IF WE ARE NOT FORCING IT TO SEACH, IT WILL TRY TO RESOLVE THE LBRY URL FIRST ##
  186. if not force_search.get_active() or win.url.get_text().startswith("lbry://"):
  187. win.url.set_text(parse.bar(win.url.get_text()))
  188. resolve = ui.load(win, url.resolve, url.render_resolve, w, win, win.url.get_text())
  189. dynamic_box.pack_start(resolve, True, True, True)
  190. win.show_all()
  191. else:
  192. resolve = ui.load(win, claim_search.find, claim_search.render, win, win.url.get_text())
  193. dynamic_box.pack_start(resolve, True, True, True)
  194. win.show_all()
  195. # Button to activate seach for those who don't know that you can press Enter
  196. search_button = Gtk.Button()
  197. search_icon = ui.icon(win, "search")
  198. search_button.add(search_icon)
  199. search_button.set_relief(Gtk.ReliefStyle.NONE)
  200. search_button.connect("clicked", search)
  201. win.url.connect("activate", search)
  202. restofpannel.pack_start(search_button, False, False, False)
  203. ##########################################################################
  204. # #
  205. # HAMBURGER MENU #
  206. # #
  207. ##########################################################################
  208. # Popover is the new GTK menu thingy that looks like a comic book dialog
  209. # box. Which is what we want here. Because we want it to look nice I suppose.
  210. hamburger = Gtk.Popover()
  211. hambutton = Gtk.MenuButton(popover=hamburger)
  212. hambutton_icon = ui.icon(win, "actor")
  213. hambutton.add(hambutton_icon)
  214. hambutton.set_relief(Gtk.ReliefStyle.NONE)
  215. pannel.pack_start(hambutton)
  216. # Let's now pack the hamburger menu with items
  217. hambox = Gtk.VBox()
  218. # For the elements inside the box we need a scrolled window, to fit a lot of
  219. # them there.
  220. hamscrl = Gtk.ScrolledWindow()
  221. hamscrl.set_size_request(200,200)
  222. hamburger.add(hambox)
  223. hambox.pack_start(hamscrl, False, False, False)
  224. # "hamchannelbox" will be empty untill the user connects and it loads his channel
  225. # list
  226. hamchannelbox = Gtk.VBox()
  227. hamscrl.add_with_viewport(hamchannelbox)
  228. #hambox.pack_start(hamchannelbox, False, False, False)
  229. ###### CHANNELS SELECTOR ########
  230. win.channel = False
  231. def load_channel(win):
  232. def change_channel(w, win, channel ):
  233. win.channel = channel
  234. win.settings["channel"] = win.channel["claim_id"]
  235. settings.save(win.settings)
  236. try:
  237. hambutton.get_child().destroy()
  238. try:
  239. newicon = ui.load(win, ui.net_image_calculation, ui.net_image_render, channel["value"]["thumbnail"]["url"], 40, "", False )
  240. except:
  241. newicon = ui.icon(win, "actor")
  242. hambutton.add(newicon)
  243. pannel.show_all()
  244. except Exception as e:
  245. print("What?", e)
  246. out = fetch.lbrynet("channel_list")
  247. win.my_channels = out
  248. # We are chooseing first only if there is no channel set in the settings
  249. try:
  250. first = out["items"][0]
  251. win.channel = first
  252. except:
  253. pass
  254. if "channel" in win.settings:
  255. try:
  256. for i in out["items"]:
  257. if i["claim_id"] == win.settings["channel"]:
  258. first = i
  259. break
  260. except:
  261. pass
  262. try:
  263. change_channel(False, win, first)
  264. except:
  265. pass
  266. try:
  267. ####### Go to channel button #####
  268. def go_to_channel(w):
  269. channel_url = win.channel["name"]
  270. try:
  271. channel_url = channel_url + "#" + win.channel["claim_id"]
  272. except:
  273. pass
  274. win.url.set_text(channel_url)
  275. win.url.activate()
  276. go = Gtk.Button(" Go To Channel ")
  277. go.set_relief(Gtk.ReliefStyle.NONE)
  278. go.connect("clicked", go_to_channel)
  279. hamchannelbox.pack_start(go, False, False, False)
  280. for i in out["items"]:
  281. channel_button = ui.go_to_channel(win, i, resolve=False)
  282. channel_button.connect("clicked", change_channel, win, i)
  283. hamchannelbox.pack_start(channel_button, False, False, False)
  284. hamchannelbox.show_all()
  285. except Exception as e:
  286. print("CHANNEL ERROR:", e)
  287. ######## SETTINGS BUTTON ############
  288. settings_button = Gtk.Button("Settings")
  289. settings_button.set_relief(Gtk.ReliefStyle.NONE)
  290. settings_button.connect("clicked", settings.dialogue, win)
  291. hamchannelbox.pack_end(settings_button, False, False, False)
  292. ######## FOLLOWING BUTTON ###########
  293. def load_following( w=False):
  294. for i in dynamic_box.get_children():
  295. i.destroy()
  296. try:
  297. out = fetch.lbrynet("preference_get")
  298. subs_raw = out["shared"]["value"]["subscriptions"]
  299. subs_raw = fetch.lbrynet("resolve", {"urls":subs_raw})
  300. subs = []
  301. for i in subs_raw:
  302. try:
  303. subs.append(subs_raw[i]["claim_id"])
  304. except:
  305. pass
  306. except Exception as e:
  307. print("\n\nERROR:", e, "\n\n")
  308. subs = []
  309. resolve = ui.load(win, claim_search.find, claim_search.render, win, "", subs)
  310. dynamic_box.pack_start(resolve, True, True, True)
  311. dynamic_box.show_all()
  312. following = Gtk.Button("Following")
  313. following.set_relief(Gtk.ReliefStyle.NONE)
  314. following.connect("clicked", load_following)
  315. hamchannelbox.pack_end(following, False, False,False)
  316. ########## FORCE SEARCH BUTTON #######
  317. force_search = Gtk.CheckButton(" Force Search ")
  318. force_search.set_tooltip_text("If not activated it will try to resolve the claim first.")
  319. hamchannelbox.pack_end(force_search, False, False, False)
  320. hambox.show_all()
  321. ##########################################################################
  322. # #
  323. # DYNAMIC BOX ( REST ) #
  324. # #
  325. ##########################################################################
  326. # Dynamic box is a box contents of which will change depending on the "page"
  327. # that the user is loading.
  328. box = Gtk.VBox() # I'm making a top level box just in case
  329. win.add(box)
  330. dynamic_box = Gtk.VBox() # And here our dynamic box
  331. scrl = Gtk.ScrolledWindow()
  332. box.pack_start(dynamic_box, True, True, False)
  333. # Just something to run if we are connected
  334. if win.SDK_running == 1:
  335. load_following()
  336. load_channel(win)
  337. else:
  338. dynamic_box.pack_start(Gtk.Label("LBRY is not connected. Please connect."), True, True, 20)
  339. ##########################################################################
  340. # #
  341. # DRAG AND DROP #
  342. # #
  343. ##########################################################################
  344. def on_drop(widget, drag_context, x, y, data, info, time):
  345. win.url.set_text(data.get_text())
  346. win.url.activate()
  347. enforce_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(4), 129)
  348. box.drag_dest_set(Gtk.DestDefaults.ALL, [enforce_target], Gdk.DragAction.COPY)
  349. box.connect("drag-data-received", on_drop)
  350. # Starting Everything
  351. win.show_all()
  352. Gtk.main()
  353. #### Clearing the cache of images ###
  354. ui.clean_image_cache()