images.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # GPL3 or any later version
  2. import os
  3. import json
  4. import time
  5. import urllib.request
  6. import urllib.parse
  7. import gi
  8. import threading
  9. gi.require_version('Gtk', '3.0')
  10. from gi.repository import Gtk
  11. from gi.repository import Gdk
  12. from gi.repository import GLib
  13. from modules import wallet
  14. from modules import ui
  15. def search_searx(search):
  16. # This function searches in searx for images
  17. search = urllib.parse.quote_plus(search)
  18. url = "https://searx.thegpm.org/?q="+search+"&categories=images&format=json"
  19. req = urllib.request.Request(url, data=None)
  20. f = urllib.request.urlopen(req)
  21. data = json.loads(f.read().decode('utf-8'))
  22. ret = []
  23. for i in data.get("results",[]):
  24. if i.get("thumbnail_src"):
  25. ret.append(i.get("thumbnail_src"))
  26. return ret
  27. def select_image(win, search):
  28. # Selector window
  29. # This will insures that we are not getting by mistake
  30. # the image from the previous search into here.
  31. try:
  32. del win.return_search
  33. except:
  34. pass
  35. folder = wallet.save_folder()+"images/"
  36. try:
  37. os.makedirs(folder)
  38. except:
  39. pass
  40. dialogWindow = Gtk.Dialog("Select Image",
  41. buttons=(),
  42. )
  43. dialogWindow.set_size_request(400, 400)
  44. box = dialogWindow.get_content_area()
  45. scrl = Gtk.ScrolledWindow()
  46. scrl.set_vexpand(0)
  47. box.pack_start(scrl, 1,1,1)
  48. pack = Gtk.VBox()
  49. scrl.add(pack)
  50. def draw(links):
  51. flowbox = Gtk.FlowBox()
  52. flowbox.set_valign(Gtk.Align.START)
  53. flowbox.set_max_children_per_line(30)
  54. flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
  55. for i in links:
  56. def on_button(w, i, save_as):
  57. dialogWindow.destroy()
  58. win.return_search = save_as
  59. save_as = ui.image_save_name(i,folder)
  60. button = Gtk.Button()
  61. button.set_relief(Gtk.ReliefStyle.NONE)
  62. button.connect("clicked", on_button, i, save_as)
  63. image = ui.load(win, ui.net_image_calculation, ui.net_image_render, i, 100 , save_as, False)
  64. button.add(image)
  65. flowbox.add(button)
  66. flowbox.show_all()
  67. return flowbox
  68. widget = ui.load(win, search_searx, draw, search)
  69. pack.pack_start(widget, 1,1,1)
  70. box.show_all()
  71. response = dialogWindow.run()
  72. dialogWindow.destroy()
  73. def clear():
  74. # This function runs in the end of the runtime to clean images
  75. # that are not needed.
  76. imgs = [] # List of images that we should not touch
  77. # Checking every listed image in items.
  78. folder = wallet.save_folder()+"items/"
  79. for i in list(os.walk(folder))[0][2]:
  80. try:
  81. with open(folder+i) as json_file:
  82. idata = json.load(json_file)
  83. im = idata.get("image", "")
  84. if im:
  85. imgs.append(im)
  86. except:
  87. pass
  88. # Checking evert listed image in merchants.
  89. folder = wallet.save_folder()+"merchants/"
  90. for i in list(os.walk(folder))[0][2]:
  91. try:
  92. with open(folder+i) as json_file:
  93. idata = json.load(json_file)
  94. im = idata.get("image", "")
  95. if im:
  96. imgs.append(im)
  97. except:
  98. pas
  99. # If not listed deleting.
  100. folder = wallet.save_folder()+"images/"
  101. for i in list(os.walk(folder))[0][2]:
  102. img = folder+i
  103. if not img in imgs:
  104. try:
  105. os.remove(img)
  106. except:
  107. pass