Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:CommonsSlide/script

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
#
# Slideshow new images in commons.wikimedia.com
# It checks new images every five minute, and update
# Written by Ryu, Cheol <ryuch@yahoo.com> 
 
import sys, urllib, gc, urlparse, string
import gtk, gtk.gdk, gobject
import wikipedia
 
class AppURLopener(urllib.FancyURLopener):
    version = "WikimediaCommonsSlide/0.1"
 
class SimpleSlideShow:
    def __init__ (self):
        self.is_fullscreen = False
        self.w = gtk.Window()
        self.vb = gtk.VBox()
        self.i = gtk.Image()
        self.b = gtk.Button('Fullscreen')
        self.label = gtk.Label()
        self.label.set_line_wrap(True)
        self.b.connect('clicked', self.toggle_fullscreen_cb)
        self.bb=gtk.HButtonBox()
        self.bb.add(self.b)
        self.qb=gtk.Button(stock=gtk.STOCK_QUIT)
        self.qb.connect('clicked',self.quit)
        self.bb.add(self.qb)
        self.vb.pack_start(self.i,expand=True,fill=True)
        self.vb.pack_start(self.label, expand=False, fill=True)
        self.vb.pack_start(self.bb,expand=False,fill=False)
        self.w.connect('delete-event', self.quit)
        self.w.connect('key-press-event', self.key_process)
        self.wikisite = wikipedia.getSite() # get wiki site
        self.image_url = 'initial'
        self.w.add(self.vb)
        self.w.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
        self.set_image_cb() # set our first image
        self.w.show_all()
 
    def key_process(self, widget, event):
        #print event.type, gtk.gdk.KEY_PRESS
        if event.type == gtk.gdk.KEY_PRESS and event.keyval == gtk.keysyms.Escape:
            self.quit()
            return True
        return False
 
    def run (self):
        """Run our slideshow"""
        # Run the main gtk process
        gtk.main()
 
    def quit (self, *args):
        self.w.hide()
        gtk.main_quit()
 
    def set_image_cb (self):
        """Get the lastest image."""
        try :
            # get the last page
            pagelist = self.wikisite.newimages(1)
            iPage, idate, iuser, icomment = 0, 0, 0, 0
            for atuple in pagelist:
                iPage, idate, iuser, icomment = atuple
                # print "[set image cb]", iPage.fileUrl(), idate, iuser, icomment
            if self.image_url != iPage.fileUrl() : # new image came in
                urllib.urlretrieve(iPage.fileUrl(), "tmpimage")
                url_parts = urlparse.urlparse(iPage.fileUrl())
                fname = string.split(url_parts.path,"/")
                self.pb = gtk.gdk.pixbuf_new_from_file("tmpimage")
                self.pb = self.scale_image_to_window(self.pb)
                self.i.set_from_pixbuf(self.pb)
                self.image_url = iPage.fileUrl()
                self.label.set_text(urllib.unquote_plus(fname[-1]))
        except :
            print "Unexpected error:", sys.exc_info()[0]
 
        # garbage collection
        gc.collect()
        gobject.timeout_add(5000, self.set_image_cb)        
        return False
 
    def scale_image_to_window (self,pb):
        """Make pixbuf fit our window"""
        ww,wh = self.w.get_size() #window size
        iw,ih=pb.get_width()+30,pb.get_height()+50 #image size
        wratio = float(iw)/ww
        hratio = float(ih)/wh
        if wratio > hratio: ratio = wratio
        else: ratio = hratio
        scaled = pb.scale_simple(pb.get_width()/ratio,
                                 pb.get_height()/ratio,
                                 gtk.gdk.INTERP_BILINEAR)
        return scaled
 
    def toggle_fullscreen_cb (self,*args):
        if self.is_fullscreen:
            self.w.unfullscreen()
            self.w.set_size_request(*self.is_fullscreen)
            self.is_fullscreen=False
        else:
            self.is_fullscreen=self.w.get_size() #store non-full size here
            self.w.fullscreen()
        # resize our image...
        pb = self.scale_image_to_window(self.pb)
        self.i.set_from_pixbuf(pb)
 
if __name__ == '__main__':    
    urllib._urlopener = AppURLopener()   
    sss = SimpleSlideShow()
    sss.run()
Personal tools
Share