Pberndt V4

Direkt zum Inhalt springen


Quellcode wetter.py

Beschreibung

Ein kleines Script zum Anzeigen des wetteronline.de Wetters als Desktopwidget

Sourcecode

#!/bin/env python
# vim:fileencoding=utf-8:ft=python
#
import gtk, cairo, urllib, re, gobject, sys, os

# Hier das Wetter entsprechend der URL auf wetteronline.de eintragen
CITY="Niedersachsen/Lueneburg"

# Load images ########################################################
class AppURLopener(urllib.FancyURLopener):
    version = "Mozilla/5.0"
urllib._urlopener = AppURLopener()
def loadImage(url):
    data = urllib.urlopen(url).read()
    loader = gtk.gdk.PixbufLoader()
    loader.write(data)
    loader.close()
    image = gtk.Image()
    image.set_from_pixbuf(loader.get_pixbuf())
    return image

# Load day ###########################################################
def load():
    try:
        url = "http://www.wetteronline.de/%s.htm" % CITY
        data = urllib.urlopen(url).read()
        url = "http://www.wetteronline.de" + re.search("\/cgi-bin[^\"]+", data).group(0)
        url = url.replace("region", "citybild")
        data = urllib.urlopen(url).read()
        data = re.search("<table[^>]+BGCOLOR.+?>(?s).+?<\/table>", data).group(0)
    except:
        print "Failed to load data from wetteronline"
        print " - Are you online?"
        print " - Maybe wetteronline changed their page design :/"
        sys.exit(1)

    table = gtk.Table()
    c = 0
    for day in re.findall("(?i)<b>([a-z]{2}, [0-9]+\.[0-9]+\.)<\/b>", data):
        table.attach(gtk.Label(day), c, c + 1, 0, 1)
        c += 1
    c = 0
    temps = re.findall("([0-9]+)&deg;C", data)
    for temp in (0, 1, 2):
        table.attach(gtk.Label(temps[temp] + "-" + temps[temp + 3] + u"°C"), c, c + 1, 1, 2)
        c += 1
    c = 0
    for image in re.findall("\/daten\/symbole\/vorhersagen\/standard\/[^\"]+", data)[:9]:
        url = "http://www.wetteronline.de" + image
        table.attach(loadImage(url), c % 3, (c % 3) + 1, c / 3 + 2, c / 3 + 3)
        c += 1
    return table

def reload():
    global wnd
    data = windowCreate()
    apply(data.move, wnd.get_position())
    wnd.destroy()
    data.show_all()
    wnd = data
    return True

# Create window #####################################################
def windowCreate():
    wnd = gtk.Window()
    wnd.set_decorated(False)
    wnd.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_SPLASHSCREEN)
    wnd.set_keep_below(True)
    wnd.move(wnd.get_screen().get_width() - 180, 10)
    wnd.stick()
    wnd.connect('delete-event', gtk.main_quit)
    try:
        wnd.set_app_paintable(True)
        wnd.set_colormap(wnd.get_screen().get_rgba_colormap())
        def expose(widget, event):
            context = widget.window.cairo_create()
            context.set_source_rgba(1.0, 1.0, 1.0, 0.0)
            context.set_operator(cairo.OPERATOR_SOURCE)
            context.paint()
            return False
        wnd.connect("expose-event", expose)
    except:
        print "Can't make window transparent"
    wnd.add(load())
    return wnd

if __name__ == "__main__":
    if os.fork() != 0:
        sys.exit(0)
    wnd = windowCreate()
    wnd.show_all()
    gobject.timeout_add(3600000, reload)
    gtk.main()

Download

Dateiname
wetter.py
Größe
2.69kb