claim_search.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 is the seach mechanism
  30. import threading
  31. from flbry import ui
  32. from flbry import fetch
  33. from gi.repository import Gtk
  34. from gi.repository import GLib
  35. def find(win, arg="", channel_ids=[], page=1, r={}, flowbox=False):
  36. # This is very dumb. I did a mess with the inputs of this command
  37. # now I'm trying to add a new feature, without rewriting a whole
  38. # lot. And it requires form me to give it a weird kind of thing.
  39. sr = r.copy()
  40. if "method" in r:
  41. method = r["method"]
  42. del r["method"]
  43. c = {}
  44. c["page"] = page
  45. else:
  46. method = "claim_search"
  47. c = {}
  48. c["text"] = arg
  49. c["channel_ids"] = channel_ids
  50. c["page"] = page
  51. c["order_by"] = "release_time" #{"order_by":"release_time"}
  52. c["remove_duplicates"] = True
  53. # TAGS TO EXCLUDE ( E.G. 'mature', 'porn' )
  54. tags = win.settings["filter_tags"] # It's set in the settings
  55. c["not_tags"] = tags
  56. # Any overwrites ?
  57. for i in r:
  58. if r[i]:
  59. c[i] = r[i]
  60. elif c[i]:
  61. del c[i]
  62. channel_load = True
  63. if len(channel_ids) == 1:
  64. channel_load = False
  65. out = fetch.lbrynet(method, c)
  66. same = [win, arg, channel_ids, page+1, sr]
  67. if flowbox:
  68. def add_to_flowbox(flowbox, out):
  69. for i in out["items"]:
  70. flowbox.add(ui.search_item(win, i, channel_load))
  71. if len(out["items"]) > 1:
  72. lastthing(win, same, flowbox)
  73. flowbox.show_all()
  74. GLib.idle_add(add_to_flowbox, flowbox, out)
  75. else:
  76. return [win, out, channel_load, same, sr]
  77. def render(out):
  78. win, out, channel_load, same, r = out
  79. print(out)
  80. overlay = Gtk.Overlay()
  81. scrolled = Gtk.ScrolledWindow()
  82. scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
  83. flowbox = Gtk.FlowBox()
  84. flowbox.set_valign(Gtk.Align.START)
  85. flowbox.set_max_children_per_line(30)
  86. flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
  87. scrolled.add(flowbox)
  88. box = Gtk.VBox()
  89. for i in out["items"]:
  90. flowbox.add(ui.search_item(win, i, channel_load))
  91. if len(out["items"]) > 1:
  92. lastthing(win, same, flowbox)
  93. overlay.add(scrolled)
  94. ############ SEARCH MENU ##############
  95. settings_menu = Gtk.Popover()
  96. settings_scroll = Gtk.ScrolledWindow()
  97. settings_menu.add(settings_scroll)
  98. settings_scroll.set_size_request(400,400)
  99. menu_box = Gtk.VBox()
  100. settings_scroll.add(menu_box)
  101. menu_box.pack_start(Gtk.Label(" Search Options: "), 0,0,5)
  102. #### SEARCH TERMS ###
  103. # name - the LBRY url of publication.
  104. collapsable = Gtk.Expander(label=" By Text Query: ")
  105. menu_box.pack_start(collapsable, 0,0,5)
  106. term_box = Gtk.VBox()
  107. collapsable.add(term_box)
  108. hbox = Gtk.HBox()
  109. term_box.pack_start(hbox, 0,0,5)
  110. hbox.pack_start(Gtk.Label(" LBRY url: "), 0,0,0)
  111. lbry_name_entry = Gtk.Entry()
  112. if same[-1].get("name"):
  113. lbry_name_entry.set_text(same[-1]["name"])
  114. hbox.pack_end(lbry_name_entry, 1,1,0)
  115. # text - the text in title
  116. hbox = Gtk.HBox()
  117. term_box.pack_start(hbox, 0,0,5)
  118. hbox.pack_start(Gtk.Label(" Search by Text: "), 0,0,0)
  119. text_entry = Gtk.Entry()
  120. if same[1]:
  121. text_entry.set_text(same[1])
  122. elif same[-1].get("text"):
  123. text_entry.set_text(same[-1]["text"])
  124. hbox.pack_end(text_entry, 1,1,0)
  125. # claim_id - search partial of full claim_id
  126. collapsable = Gtk.Expander(label=" By Claim ID: ")
  127. menu_box.pack_start(collapsable, 0,0,5)
  128. term_box = Gtk.VBox()
  129. collapsable.add(term_box)
  130. hbox = Gtk.HBox()
  131. term_box.pack_start(hbox, 0,0,5)
  132. hbox.pack_start(Gtk.Label(" Claim ID: "), 0,0,0)
  133. claim_id_entry = Gtk.Entry()
  134. if same[-1].get("claim_id"):
  135. claim_id_entry.set_text(same[-1]["claim_id"])
  136. hbox.pack_start(claim_id_entry, 1,1,0)
  137. # claim_ids - search partial of full claim_id
  138. term_box.pack_start(Gtk.Label(" Many Full Claim IDs "), 0,0,5)
  139. claim_ids_list = []
  140. if same[-1].get("claim_ids"):
  141. claim_ids_list = same[-1].get("claim_ids")
  142. claim_ids_editor = ui.tags_editor(win, claim_ids_list)
  143. term_box.pack_start(claim_ids_editor, 0,0,0)
  144. ## TAGS ##
  145. collapsable = Gtk.Expander(label=" Tags: ")
  146. menu_box.pack_start(collapsable, 0,0,5)
  147. term_box = Gtk.VBox()
  148. collapsable.add(term_box)
  149. term_box.pack_start(Gtk.Label(" Any of: "), 0,0,5)
  150. any_tags_list = []
  151. if same[-1].get("any_tags"):
  152. any_tags_list = same[-1].get("any_tags")
  153. tags_editor = ui.tags_editor(win, any_tags_list)
  154. term_box.pack_start(tags_editor, 0,0,0)
  155. term_box.pack_start(Gtk.Label(" All of: "), 0,0,5)
  156. all_tags_list = []
  157. if same[-1].get("all_tags"):
  158. all_tags_list = same[-1].get("all_tags")
  159. tags_editor = ui.tags_editor(win, all_tags_list)
  160. term_box.pack_start(tags_editor, 0,0,0)
  161. term_box.pack_start(Gtk.Label(" Excluding: "), 0,0,5)
  162. not_tags_list = []
  163. if same[-1].get("not_tags"):
  164. not_tags_list = same[-1].get("not_tags")
  165. tags_editor = ui.tags_editor(win, not_tags_list)
  166. term_box.pack_start(tags_editor, 0,0,0)
  167. ## CHANNELS ##
  168. collapsable = Gtk.Expander(label=" Channels: ")
  169. menu_box.pack_start(collapsable, 0,0,5)
  170. term_box = Gtk.VBox()
  171. collapsable.add(term_box)
  172. hbox = Gtk.HBox()
  173. term_box.pack_start(hbox, 0,0,5)
  174. hbox.pack_start(Gtk.Label(" Channel Name: "), 0,0,0)
  175. channel_entry = Gtk.Entry()
  176. if same[-1].get("channel"):
  177. channel_entry.set_text(same[-1]["channel"])
  178. hbox.pack_start(channel_entry, 1,1,0)
  179. term_box.pack_start(Gtk.Label(" Anything from (channel's claim IDs): "), 0,0,5)
  180. any_channels_list = []
  181. if same[2]:
  182. any_channels_list = same[2]
  183. elif same[-1].get("channel_ids"):
  184. any_channels_list = same[-1]["channel_ids"]
  185. tags_editor = ui.tags_editor(win, any_channels_list)
  186. term_box.pack_start(tags_editor, 0,0,0)
  187. term_box.pack_start(Gtk.Label(" Excluding (channel's claim IDs): "), 0,0,5)
  188. not_channels_list = []
  189. if same[-1].get("not_channel_ids"):
  190. not_channels_list = same[-1]["not_channel_ids"]
  191. tags_editor = ui.tags_editor(win, not_channels_list)
  192. term_box.pack_start(tags_editor, 0,0,0)
  193. ## ORDER BY ##
  194. collapsable = Gtk.Expander(label=" Order: ")
  195. menu_box.pack_start(collapsable, 0,0,5)
  196. term_box = Gtk.VBox()
  197. collapsable.add(term_box)
  198. order_by_rules = {
  199. "Name":"name",
  200. "Release Time":"release_time",
  201. "Trending":"trending_mixed",
  202. "Trending (Global)":"trending_global",
  203. "Trending (Group)":"trending_group",
  204. "Trending (Local)":"trending_local",
  205. "Bid":"amount",
  206. "Support":"support_amount",
  207. "Support plus Bid":"effective_amount",
  208. "Block Height":"height",
  209. "Start Block":"activation_height"
  210. }
  211. order_by = Gtk.ComboBoxText()
  212. was_set = False
  213. for n, i in enumerate(order_by_rules):
  214. order_by.append_text(i)
  215. if order_by_rules[i] in str(same[-1].get("order_by")):
  216. order_by.set_active(n)
  217. was_set = True
  218. if not was_set:
  219. order_by.set_active(1)
  220. term_box.pack_start(order_by, 0,0,5)
  221. hbox = Gtk.HBox()
  222. term_box.pack_start(hbox, 0,0,5)
  223. hbox.pack_start(Gtk.Label(" Reverse: "), 0,0,0)
  224. order_reverse = Gtk.Switch()
  225. if "^" in str(same[-1].get("order_by")):
  226. order_reverse.set_active(True)
  227. hbox.pack_end(order_reverse, 0,0,0)
  228. ## ORDER BY ##
  229. collapsable = Gtk.Expander(label=" Type: ")
  230. menu_box.pack_start(collapsable, 0,0,5)
  231. term_box = Gtk.VBox()
  232. collapsable.add(term_box)
  233. claim_types = {
  234. "Every Claim Type":"",
  235. "Channel":"channel",
  236. "File":"stream",
  237. "Re-Post":"repost",
  238. "Collection":"collection"
  239. }
  240. claim_type = Gtk.ComboBoxText()
  241. was_set = False
  242. for n, i in enumerate(claim_types):
  243. claim_type.append_text(i)
  244. if claim_types[i] in str(same[-1].get("claim_type")):
  245. claim_type.set_active(n)
  246. was_set = True
  247. if not was_set:
  248. claim_type.set_active(0)
  249. term_box.pack_start(claim_type, 0,0,5)
  250. ## FILETYPES ##
  251. term_box.pack_start(Gtk.Label(" Filetypes: "), 0,0,5)
  252. stream_types = []
  253. if same[-1].get("stream_types"):
  254. stream_types = same[-1]["stream_types"]
  255. stream_types_editor = ui.tags_editor(win, stream_types)
  256. stream_types_editor.set_tooltip_text("Types of a file. For example: video, audio, image, document, binary.")
  257. term_box.pack_start(stream_types_editor, 0,0,5)
  258. ## MIME-TYPES ##
  259. term_box.pack_start(Gtk.Label(" Mime-Types: "), 0,0,5)
  260. media_types = []
  261. if same[-1].get("media_types"):
  262. media_types = same[-1]["media_types"]
  263. media_types_editor = ui.tags_editor(win, media_types)
  264. media_types_editor.set_tooltip_text("Precise Formats. For example: image/png, audio/ogg, application/x-ext-blend")
  265. term_box.pack_start(media_types_editor, 0,0,5)
  266. ####### !!! RE _ SEARCH !!! #######
  267. def do_re_search(w):
  268. request = {}
  269. ## TEXT ##
  270. if lbry_name_entry.get_text():
  271. request["name"] = lbry_name_entry.get_text()
  272. if text_entry.get_text():
  273. request["text"] = text_entry.get_text()
  274. ## CLAIM ID ##
  275. if claim_id_entry.get_text():
  276. request["claim_id"] = claim_id_entry.get_text()
  277. if claim_ids_list:
  278. request["claim_ids"] = claim_ids_list
  279. ## TAGS ##
  280. if any_tags_list:
  281. request["any_tags"] = any_tags_list
  282. if all_tags_list:
  283. request["all_tags"] = all_tags_list
  284. if any_tags_list:
  285. request["not_tags"] = not_tags_list
  286. ## CHANNELS ##
  287. if channel_entry.get_text():
  288. request["channel"] = channel_entry.get_text()
  289. if any_channels_list:
  290. request["channel_ids"] = any_channels_list
  291. if not_channels_list:
  292. request["not_channel_ids"] = not_channels_list
  293. ## Order by ##
  294. prefix = ""
  295. if order_reverse.get_active():
  296. prefix = "^"
  297. request["order_by"] = prefix+list(order_by_rules.values())[order_by.get_active()]
  298. ## CLAIM TYPE ##
  299. if list(claim_types.values())[claim_type.get_active()]:
  300. request["claim_type"] = list(claim_types.values())[claim_type.get_active()]
  301. # Some logic related to what the user will percieve
  302. if request["channel_ids"] and not request.get("claim_ids") and request["claim_type"] == "channel":
  303. request["claim_ids"] = request["channel_ids"]
  304. request["channel_ids"] = []
  305. ## STREAM TYPE ##
  306. if stream_types:
  307. request["stream_types"] = stream_types
  308. ## MIME TYPES ##
  309. if media_types:
  310. request["media_types"] = media_types
  311. box = overlay.get_parent()
  312. overlay.destroy()
  313. resolve = ui.load(win, find, render, win, "", [], 1, request)
  314. box.pack_start(resolve, 1,1,0)
  315. box.show_all()
  316. re_search = Gtk.Button()
  317. re_search.connect("clicked", do_re_search)
  318. re_search.set_relief(Gtk.ReliefStyle.NONE)
  319. b = Gtk.HBox()
  320. b.pack_start(ui.icon(win, "system-search"),0,0,0)
  321. b.pack_start(Gtk.Label(" Re-Search "),0,0,0)
  322. re_search.add(b)
  323. menu_box.pack_end(re_search, 0,0,0)
  324. settings_scroll.show_all()
  325. b = Gtk.MenuButton(popover=settings_menu)
  326. b.add(ui.icon(win, "preferences-system"))
  327. b.set_halign(Gtk.Align.END)
  328. b.set_valign(Gtk.Align.END)
  329. overlay.add_overlay(b)
  330. return overlay
  331. def lastthing(win, same, flowbox):
  332. # THis is a hack. I use a spinner to get a draw event
  333. # when the user scrolls far enough, it will execute it
  334. # to load more stuff
  335. spinner_more = ui.icon(win, "loading", "gif")
  336. flowbox.add(spinner_more)
  337. def draw_event(w, e):
  338. print("EVENT MOTHERFUCKER")
  339. w.destroy()
  340. same.append(flowbox)
  341. print("SAME:", same)
  342. load_thread = threading.Thread(target=find, args=[*same])
  343. load_thread.setDaemon(True)
  344. load_thread.start()
  345. spinner_more.connect("draw", draw_event)