Advertisement
Halbheld

netglance2.py

Nov 12th, 2019 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.12 KB | None | 0 0
  1. ## netglance2.py
  2. ## for Python2. Python3 version at https://pastebin.com/s91KQJfh
  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 urllib2
  9. import argparse
  10. from datetime import datetime
  11.  
  12. ## Kommandozeilenparameter
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument("-u", "--url", help="URL to an online JPG or PNG file", default="http://www.haemmern.de/live/bild.jpg")
  15. parser.add_argument("-iw", "--width", help="Image width", default="640")
  16. parser.add_argument("-ih", "--height", help="Image height, 0 to keep aspect ratio", default="0")
  17. parser.add_argument("-x", "--posx", help="Horizontal position of the image on desktop in pixel", default="20")
  18. parser.add_argument("-y", "--posy", help="Vertical position of the image on desktop in pixel", default="20")
  19. parser.add_argument("-t", "--time", help="Refresh duration in seconds, 0 for no refresh", default="60")
  20. args = parser.parse_args()
  21.  
  22. url = args.url
  23. dimx = int(args.width)
  24. dimy = int(args.height)
  25. if dimy == 0:
  26.     # Aspect Ratio soll eingehalten werden
  27.     ratio = True
  28.     dimy = dimx
  29. else:
  30.     ratio = False
  31. posx = int(args.posx)
  32. posy = int(args.posy)
  33. seconds = int(args.time)
  34.  
  35. # URL Host als Titel des Fensters
  36. titel = url.split("//")[-1].split("/")[0]
  37. titel = titel.replace("www.","")
  38. savefile = titel
  39. # Refresh dahinter angeben
  40. if seconds != 0:
  41.     titel = titel+" ("+str(seconds)+"s)"
  42.  
  43. ##### THREADING Subroutinen
  44. class perpetualTimer():
  45.  
  46.     def __init__(self,t,hFunction):
  47.         self.t=t
  48.         self.hFunction = hFunction
  49.         self.thread = Timer(self.t,self.handle_function)
  50.  
  51.     def handle_function(self):
  52.         self.hFunction()
  53.         self.thread = Timer(self.t,self.handle_function)
  54.         self.thread.start()
  55.  
  56.     def start(self):
  57.         self.thread.start()
  58.  
  59.     def cancel(self):
  60.         self.thread.cancel()
  61. ##### THREADING
  62.  
  63.  
  64. class Handler:
  65.  
  66.         def on_Fenster_destroy(self, *args):
  67.             if seconds != 0:
  68.                 t.cancel()
  69.             Gtk.main_quit()
  70.  
  71.  
  72.         def on_Eventbox_button_press_event(self,widget,event):
  73.            
  74.             if event.button == 3:       ## Rechte Maustaste, Datei-Speichern-Dialog
  75.                 print "Save Image..."
  76.                
  77.                 dialog = Gtk.FileChooserDialog("Save Image",
  78.                         None,
  79.                         Gtk.FileChooserAction.SAVE,
  80.                         (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  81.                         Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
  82.                 dialog.set_default_size(640,480)
  83.                 dialog.set_transient_for(x.builder.get_object("Fenster"))
  84.                 zeit = datetime.now().strftime('%Y-%m-%d %H:%M')
  85.                 savfile = savefile + " " + zeit
  86.                 dialog.set_current_name(savfile+".jpg")
  87.  
  88.                 response = dialog.run()
  89.  
  90.                 if response == Gtk.ResponseType.OK:
  91.                     bilddatei = dialog.get_filename()
  92.                    
  93.                     if not bilddatei.endswith(".jpg") and not bilddatei.endswith(".png"):
  94.                         bilddatei += ".jpg"
  95.                        
  96.                     bild = x.builder.get_object("Bild").get_pixbuf()
  97.                    
  98.                     if bilddatei.endswith(".jpg"):
  99.                         bild.savev(bilddatei, "jpeg", ["quality"], ["100"])
  100.                     elif bilddatei.endswith(".png"):
  101.                         bild.savev(bilddatei, "png", "", "")
  102.                        
  103.                     print("...saved to "+bilddatei)
  104.  
  105.                 elif response == Gtk.ResponseType.CANCEL:
  106.                     print("...cancel")
  107.  
  108.                 dialog.destroy()               
  109.                
  110.             else:       ## Irgend eine andere Taste: Manueller Refresh!
  111.                 print("Manual"),
  112.                 lauf()
  113.  
  114.  
  115. class Netglance:
  116.     def __init__(self):
  117.         self.builder=Gtk.Builder()
  118.         #self.builder.add_from_file("/home/nassenstein/Python/Netglance2.glade")
  119.         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")
  120.  
  121.         self.builder.connect_signals(Handler())
  122.  
  123.         window=self.builder.get_object("Fenster")
  124.  
  125.         # Fenster positionieren
  126.         window.move(posx,posy)
  127.  
  128.         window.set_title(titel)
  129.  
  130.         window.show_all()
  131.  
  132.         print("Startup "+url+" Refresh after "+str(seconds)+" seconds")
  133.        
  134.         response = urllib2.urlopen(url)
  135.         input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None)  
  136.         pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(input_stream, dimx, dimy, ratio, None)
  137.  
  138.         self.builder.get_object("Bild").set_from_pixbuf(pixbuf)
  139.  
  140.  
  141.  
  142.     def main(self):
  143.         Gtk.main()
  144.  
  145. x=Netglance()
  146.  
  147. def lauf():
  148.         print("Refresh")
  149.        
  150.         response = urllib2.urlopen(url)
  151.         input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None)  
  152.         pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(input_stream, dimx, dimy, ratio, None)  
  153.        
  154.         x.builder.get_object("Bild").set_from_pixbuf(pixbuf)
  155.  
  156. if seconds != 0:
  157.     t=perpetualTimer(seconds,lauf)
  158.     t.start()
  159.  
  160. x.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement