added Dilbert!
[comic-widget] / comic-widget.py
index 311db0e..c3b33c5 100644 (file)
@@ -9,15 +9,19 @@ import os
 import osso
 import cairo
 import pango
+import datetime
+
 
 # constants. dbfile is the location of the csv
 # comiccache is the location of the images
+APP_VERSION = "0.1"
 
 imagedir = "/home/user/comic-widget/images/"
 dbdir = "/home/user/comic-widget/db/"
 comiccache = "/home/user/MyDocs/comics/"
 comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + ".comicdb.xkcd.csv"},
-               "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + ".comicdb.sinfest.csv"}}
+               "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + ".comicdb.sinfest.csv"},
+               "dilbert":{"name":"Dilbert","link":"http://dilbert.com.com/","start":"2009-01-01","dbfile":dbdir + ".comicdb.dilbert.csv"}}
 
 # handling of the comics
 class ComicDb():
@@ -45,7 +49,7 @@ class ComicDb():
                for row in dbr:
                        self.db.insert(0,row)
                dbf.close()
-               self.currentcomic = 0
+               self.currentcomic = 0           
 
        def get_comic(self):
                print str(self.currentcomic)
@@ -120,28 +124,73 @@ class ComicDb():
                dbf.close()
 
        def fetch_earlier(self, comic, earliest):
-               number = int(earliest) - 1
-               irow = self.get_irow(comic, number)
+               if len(earliest) == 10:
+                       # date id.
+                       dt = string.split(earliest, "-")
+                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                       earlier = d - datetime.timedelta( 1 )
+                       comicid = earlier.isoformat()
+               else:
+                       comicid = int(earliest) - 1
+
+               irow = self.get_irow(comic, comicid)
                if irow:
                        print "got irow: "
                        print irow
                        print "\ninserting first...\n"
                        self.insert_row_first(irow)
                else:
-                       print "No comic found at " + number
+                       print "No comic found at " + comicid
+
+       def fetch_latest_std(self, comic, latest):
+               print "fetching new after " + str(comic) + " " + str(latest)
+               dateid = False
+               if len(latest) == 10:
+                       # date id.
+                       dateid = True
+                       dt = string.split(latest, "-")
+                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                       newer = d + datetime.timedelta( 1 )
+                       comicid = newer.isoformat()
+               else:
+                       comicid = int(latest) + 1
+
+
+               while True:
+                       irow = self.get_irow(comic, comicid)
+                       if irow:
+                               print "got irow: " + str(irow)
+                               print "inserting..."
+                               self.insert_row(irow)
+                               if dateid:
+                                       dt = string.split(comicid, "-")
+                                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                                       newer = d + datetime.timedelta( 1 )
+                                       comicid = newer.isoformat()
+                               else:
+                                       comicid += 1
+                       else:
+                               break
+
 
        def get_irow(self, comic, number):
-               if number < 0:
-                       return False
-               print "number is now: " + str(number) + "\n"
+               dateid = False
+               if len(str(number)) == 10:
+                       dateid = True
+               else:
+                       if number < 0:
+                               return False
+                       print "number is now: " + str(number) + "\n"
                if comic == 'xkcd':
                        link = "http://xkcd.org/" + str(number) + "/"
                elif comic == 'sinfest':
                        link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
+               elif comic == 'dilbert':
+                       link = "http://dilbert.com/" + str(number) + "/"
                else:
                        return False
 
-               print "link: " + link + "\n"
+               print "link: " + link
                try:
                        f = urllib2.urlopen(link)
                        hcode = f.code
@@ -179,32 +228,31 @@ class ComicDb():
                                        return False
                                splt = string.split(splt[1], '" border="0" />', 1)
                                title = splt[0]
+
+                       elif comic == 'dilbert':
+                               s = f.read()
+                               f.close()
+                               splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
+                               splt = string.split(splt[1], '" />', 1)
+                               url = "http://dilbert.com" + splt[0]
+                               if len(url) < 50:
+                                       print "Fake 404! Break break break!"
+                                       return False
+                               title = ""
+
                        splt2 = string.rsplit(url, "/", 1)
                        filename = splt2[1]
                        irow = [comic,number,link,url,filename,title]
                        return irow
-                        print "got irow: "
 
-        def fetch_latest_std(self, comic, latest):
 
-                print "fetching new after " + str(comic) + " " + str(latest) + "\n"
-                number = int(latest) + 1
-                while True:
-                       irow = self.get_irow(comic, number)
-                       if irow:
-                               print "got irow: "
-                               print irow
-                               print "\ninserting...\n"
-                               self.insert_row(irow)
-                               number += 1
-                       else:
-                               break
-
-# ------------
+# ------------UI
 
 class ComicHomePlugin(hildondesktop.HomePluginItem):
        def __init__(self):
                hildondesktop.HomePluginItem.__init__(self)
+               self.set_settings(True)
+               self.connect("show-settings", self.show_options)
                self.osso_c = osso.Context("comic-widget", "0.0.1", False)
                self.osso_rpc = osso.Rpc(self.osso_c)
                self.keys = comics.keys()
@@ -488,6 +536,37 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                return retimg
 
 
+# ************************* OPTIONS ********************************
+
+       def show_options(self, widget):
+               dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
+
+               about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               about_button.set_text("About", "See Author, Copyright and License information")
+               about_button.set_alignment(0,0,0,0)
+               about_button.connect("clicked", self.show_about)
+
+               dialog.vbox.pack_start(about_button, True, True, 0)
+
+               dialog.show_all()
+               dialog.run()
+               dialog.destroy()
+
+       def show_about(self, widget):
+               dialog = gtk.AboutDialog()
+               dialog.set_title("About")
+               dialog.set_name("Comic widget")
+               dialog.set_version(APP_VERSION)
+               dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
+               dialog.set_authors(["Marcus Wikstrom <mece@ovi.com>\n\nSpecial thanks to Benoit Hervier, Daniel Would and Brent Chiodo\nfor some nice code to sample."])
+               # dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/usr/share/touchsearch/icon.png"))
+               # dialog.set_comments("Thanks to Benoit Hervier, Daniel Would and Brent Chiodo for some nice code to sample.")
+               dialog.set_license("""This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License. <http://www.gnu.org/licenses/>.""")
+               dialog.set_wrap_license(True)
+               dialog.show_all()
+               dialog.run()
+               dialog.destroy()
+
 hd_plugin_type = ComicHomePlugin