Hi,

I'm ready with my plugin and want to share it with you. I'm not a
python programmer, so it might not be well written (exception handling
for example). It's a plugin that grabs Dilbert strips from the web and
stores them in a directory. Three menu items appears in image section
if it is activated.

Place these lines in local_conf.py:
DILBERT_DIR='/home/freevo/media/images/dilbert/'
plugin.activate('image.dilbert')

/Bjorn


H�strusk och gr� moln - k�p en resa till solen p� Yahoo! Resor p� adressen 
http://se.docs.yahoo.com/travel/index.html
import plugin


import os
import config
import urllib
import time
import string
from gui.PopupBox import PopupBox
from menu import MenuItem

class PluginInterface(plugin.MainMenuPlugin):
        """
        Grab Dilbert strips from www.comics.com and store in DILBERT_DIR

        Example for local_conf.py:
        DILBERT_DIR='/home/freevo/media/images/dilbert/'
        plugin.activate('image.dilbert')
        """
        def items(self, parent):
                items = []
                items.append(MenuItem('Grab todays Dilbert strip', self.get_strip1))
                items.append(MenuItem('Grab Dilbert strips for last 7 days', 
self.get_strip7))
                items.append(MenuItem('Grab Dilbert strips for last 30 days', 
self.get_strip30))

                return items

        def get_strip1(self, arg=None, menuw=None):
                self.get_strip(1)

        def get_strip7(self, arg=None, menuw=None):
                self.get_strip(7)

        def get_strip30(self, arg=None, menuw=None):
                self.get_strip(30)

        def get_strip(self, arg, menuw=None):
            #d = 30      # Number of days to fetch. Only exist for 1 mounth.
            # 25 september, strips exist from 25 august (i.e. 32 strips).
            d = arg

            # Show a popup-box
            box = PopupBox(text='Fetching Dilbert strip...')
            box.show()

            i = 0
            while i<d:
                d_sec = i*60*60*24 # i = 0, 1, ... d days in seconds.
                today = time.strftime('%Y%m%d',time.gmtime(time.time()-d_sec))

                # Determine image type, jpg on sundays and gif other days.
                if time.strftime('%a',time.gmtime(time.time()-d_sec))=='Sun':
                    file_type = '.jpg'
                else:
                    file_type = '.gif'

                # The name to todays homepage
                urlname = 
'http://www.comics.com/comics/dilbert/archive/dilbert-'+today+'.html'
                targetfile = config.DILBERT_DIR + '/dilbert-'+today+'.html'

                try:
                    # Save the html-file
                    urllib.urlretrieve(urlname, targetfile)
                except:
                    print 'Could not open the Dilbert URL.'
                    print urlname

                # Define the sub-string to search for.
                search_substr = '<IMG SRC="/comics/dilbert/archive/images/'

                try:
                    fd = open(targetfile)
                    # Parse the html-file and search for Dilbert strip image.
                    while (1):
                        try:
                            file_line = fd.readline()
                        except (EOFError):
                            break
                        # Search for the sub-string before the unknown image filename.
                        ret = string.find(file_line, search_substr)
                        # Search for end of filename (rfind = last occurance in string)
                        ret2 = string.rfind(file_line, file_type)
                        if  ret != -1:
                            image_url = 
'http://www.comics.com/comics/dilbert/archive/images/'+file_line[ret + 
len(search_substr): ret2+4]
                            print image_url
                            break
                        else:
                            pass

                    fd.close()
                except:
                    print 'Error open file'

                targetstrip = config.DILBERT_DIR + '/dilbert-'+today+file_type
                try:
                    urllib.urlretrieve(image_url, targetstrip)
                    print 'Fetched the Dilbert Strip succesfully.'
                except:
                    print 'Could not get Dilbert strip'

                i = i+1
                os.remove(targetfile)
            box.destroy()

Reply via email to