SHARE
TWEET

netglance2.py for Python3

Halbheld Nov 18th, 2019 (edited) 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## netglance2.py
  2. ## for Python3. Python2 version at https://pastebin.com/uxiK6Z99
  3. ## November 2019 by Marco Nassenstein https://nassenstein.com
  4. import gi
  5. gi.require_version('Gtk', '3.0')
  6. from gi.repository import Gtk, Gio, GdkPixbuf
  7. from threading import Timer,Thread,Event
  8. import urllib.request
  9. #import urllib2     # Python2
  10. import argparse
  11. from datetime import datetime
  12.  
  13. ## Kommandozeilenparameter
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument("-u", "--url", help="URL to an online JPG or PNG file", default="http://www.haemmern.de/live/bild.jpg")
  16. parser.add_argument("-iw", "--width", help="Image width", default="640")
  17. parser.add_argument("-ih", "--height", help="Image height, 0 to keep aspect ratio", default="0")
  18. parser.add_argument("-x", "--posx", help="Horizontal position of the image on desktop in pixel", default="20")
  19. parser.add_argument("-y", "--posy", help="Vertical position of the image on desktop in pixel", default="20")
  20. parser.add_argument("-t", "--time", help="Refresh duration in seconds, 0 for no refresh", default="60")
  21. args = parser.parse_args()
  22.  
  23. url = args.url
  24. dimx = int(args.width)
  25. dimy = int(args.height)
  26. if dimy == 0:
  27.     # Aspect Ratio soll eingehalten werden
  28.     ratio = True
  29.     dimy = dimx
  30. else:
  31.     ratio = False
  32. posx = int(args.posx)
  33. posy = int(args.posy)
  34. seconds = int(args.time)
  35.  
  36. # URL Host als Titel des Fensters
  37. titel = url.split("//")[-1].split("/")[0]
  38. titel = titel.replace("www.","")
  39. savefile = titel
  40. # Refresh dahinter angeben
  41. if seconds != 0:
  42.     titel = titel+" ("+str(seconds)+"s)"
  43.  
  44. ##### THREADING Subroutinen
  45. class perpetualTimer():
  46.  
  47.     def __init__(self,t,hFunction):
  48.         self.t=t
  49.         self.hFunction = hFunction
  50.         self.thread = Timer(self.t,self.handle_function)
  51.  
  52.     def handle_function(self):
  53.         self.hFunction()
  54.         self.thread = Timer(self.t,self.handle_function)
  55.         self.thread.start()
  56.  
  57.     def start(self):
  58.         self.thread.start()
  59.  
  60.     def cancel(self):
  61.         self.thread.cancel()
  62. ##### THREADING
  63.  
  64.  
  65. class Handler:
  66.  
  67.         def on_Fenster_destroy(self, *args):
  68.             if seconds != 0:
  69.                 t.cancel()
  70.             Gtk.main_quit()
  71.  
  72.  
  73.         def on_Eventbox_button_press_event(self,widget,event):
  74.  
  75.             if event.button == 3:       ## Rechte Maustaste, Datei-Speichern-Dialog
  76.                 print ("Save Image...")
  77.  
  78.                 dialog = Gtk.FileChooserDialog("Save Image",
  79.                         None,
  80.                         Gtk.FileChooserAction.SAVE,
  81.                         (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  82.                         Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
  83.                 dialog.set_default_size(-1,-1)
  84.                 #dialog.resize(500,400)
  85.                 dialog.set_transient_for(x.builder.get_object("Fenster"))
  86.                 zeit = datetime.now().strftime('%Y-%m-%d %H:%M')
  87.                 savfile = savefile + " " + zeit
  88.                 dialog.set_current_name(savfile+".jpg")
  89.  
  90.                 response = dialog.run()
  91.  
  92.                 if response == Gtk.ResponseType.OK:
  93.                     bilddatei = dialog.get_filename()
  94.  
  95.                     if not bilddatei.endswith(".jpg") and not bilddatei.endswith(".png"):
  96.                         bilddatei += ".jpg"
  97.  
  98.                     bild = x.builder.get_object("Bild").get_pixbuf()
  99.  
  100.                     if bilddatei.endswith(".jpg"):
  101.                         bild.savev(bilddatei, "jpeg", ["quality"], ["100"])
  102.                     elif bilddatei.endswith(".png"):
  103.                         bild.savev(bilddatei, "png", "", "")
  104.  
  105.                     print("...saved to "+bilddatei)
  106.  
  107.                 elif response == Gtk.ResponseType.CANCEL:
  108.                     print("...cancel")
  109.  
  110.                 dialog.destroy()
  111.  
  112.             else:       ## Irgend eine andere Taste: Manueller Refresh!
  113.                 print("Manual"),
  114.                 lauf()
  115.  
  116.  
  117. class Netglance:
  118.     def __init__(self):
  119.         self.builder=Gtk.Builder()
  120.         #self.builder.add_from_file("/home/nassenstein/Python/Netglance2.glade")
  121.         self.builder.add_from_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- Generated with glade 3.22.1 -->\n<interface>\n  <requires lib=\"gtk+\" version=\"3.20\"/>\n  <object class=\"GtkWindow\" id=\"Fenster\">\n    <property name=\"can_focus\">False</property>\n    <property name=\"title\" translatable=\"yes\">Netglance</property>\n    <property name=\"resizable\">False</property>\n    <property name=\"icon_name\">image-x-generic</property>\n    <signal name=\"destroy\" handler=\"on_Fenster_destroy\" swapped=\"no\"/>\n    <child type=\"titlebar\">\n      <placeholder/>\n    </child>\n    <child>\n      <object class=\"GtkBox\" id=\"Kiste\">\n        <property name=\"visible\">True</property>\n        <property name=\"can_focus\">False</property>\n        <property name=\"orientation\">vertical</property>\n        <child>\n          <object class=\"GtkEventBox\" id=\"Eventbox\">\n            <property name=\"visible\">True</property>\n            <property name=\"can_focus\">False</property>\n            <signal name=\"button-press-event\" handler=\"on_Eventbox_button_press_event\" swapped=\"no\"/>\n            <child>\n              <object class=\"GtkImage\" id=\"Bild\">\n                <property name=\"visible\">True</property>\n                <property name=\"can_focus\">False</property>\n                <property name=\"stock\">gtk-missing-image</property>\n              </object>\n            </child>\n          </object>\n          <packing>\n            <property name=\"expand\">True</property>\n            <property name=\"fill\">True</property>\n            <property name=\"position\">0</property>\n          </packing>\n        </child>\n      </object>\n    </child>\n  </object>\n</interface>\n")
  122.  
  123.         self.builder.connect_signals(Handler())
  124.  
  125.         window=self.builder.get_object("Fenster")
  126.  
  127.         # Fenster positionieren
  128.         window.move(posx,posy)
  129.  
  130.         window.set_title(titel)
  131.  
  132.         window.show_all()
  133.  
  134.         print("Startup "+url+" Refresh after "+str(seconds)+" seconds")
  135.  
  136.         #response = urllib2.urlopen(url)                # Python2
  137.         response = urllib.request.urlopen(url)
  138.         input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None)
  139.         pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(input_stream, dimx, dimy, ratio, None)
  140.  
  141.         self.builder.get_object("Bild").set_from_pixbuf(pixbuf)
  142.  
  143.  
  144.  
  145.     def main(self):
  146.         Gtk.main()
  147.  
  148. x=Netglance()
  149.  
  150. def lauf():
  151.         print("Refresh")
  152.  
  153.         response = urllib.request.urlopen(url)
  154.         #response = urllib2.urlopen(url)        # Python2
  155.         input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None)
  156.         pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(input_stream, dimx, dimy, ratio, None)
  157.  
  158.         x.builder.get_object("Bild").set_from_pixbuf(pixbuf)
  159.  
  160. if seconds != 0:
  161.     t=perpetualTimer(seconds,lauf)
  162.     t.start()
  163.  
  164. x.main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top