moved fetch function
[comic-widget] / comic-widget.py
index 8b23f5a..f53beea 100644 (file)
@@ -11,10 +11,12 @@ import osso
 # constants. dbfile is the location of the csv
 # comiccache is the location of the images
 
-comiccache = "/home/user/MyDocs/comics/"
-comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":"/home/user/.comicdb.xkcd.csv"},
-               "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":"/home/user/.comicdb.sinfest.csv"}}
 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"}}
+
 # handling of the comics
 class ComicDb():
        def __init__(self, comic):
@@ -117,6 +119,16 @@ class ComicDb():
 
        def fetch_earlier(self, comic, earliest):
                number = int(earliest) - 1
+               irow = self.get_irow(comic, number)
+               if irow:
+                       print "got irow: "
+                       print irow
+                       print "\ninserting first...\n"
+                       self.insert_row_first(irow)
+               else:
+                       print "No comic found at " + number
+
+       def get_irow(self, comic, number):
                if number < 0:
                        return False
                print "number is now: " + str(number) + "\n"
@@ -126,6 +138,7 @@ class ComicDb():
                        link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
                else:
                        return False
+
                print "link: " + link + "\n"
                try:
                        f = urllib2.urlopen(link)
@@ -136,18 +149,20 @@ class ComicDb():
                if (hcode != 200):
                        return False
                else:
-                       # Build db entry.
-                       # they look like: comic,id,link,url,filename,title
-                       # We already have comic, number/id and link
-                       
+                        # Build db entry.
+                        # they look like: comic,id,link,url,filename,title
+                        # We already have comic, number/id and link
+
                        if comic == 'xkcd':
                                s = f.read()
                                f.close()
+                                # This should be done with regex but...
                                splt = string.split(s, "<h3>Image URL (for hotlinking/embedding): ", 1)
                                splt2 = string.split(splt[1], "</h3>", 1)
                                url = splt2[0]
                                splt = string.split(splt[0], "<h1>", 1)
                                splt = string.split(splt[1], "</h1>", 1)
+
                                title = splt[0]
 
                        elif comic == 'sinfest':
@@ -163,74 +178,27 @@ class ComicDb():
                                splt = string.split(splt[1], '" border="0" />', 1)
                                title = splt[0]
                        splt2 = string.rsplit(url, "/", 1)
-                       filename = splt2[1]                             
+                       filename = splt2[1]
                        irow = [comic,number,link,url,filename,title]
-                       print "got irow: "
-                       print irow
-                       print "\ninserting first...\n"
-                       self.insert_row_first(irow)
+                       return irow
+                        print "got irow: "
 
+        def fetch_latest_std(self, comic, latest):
 
-       def fetch_latest_std(self, comic, latest):
-               
-               print "fetching new after " + str(comic) + " " + str(latest) + "\n"
-               hcode = 200
-               number = int(latest) + 1
-               while hcode == 200:
-                       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)
-                       else:
-                               break
-                       print "link: " + link + "\n"
-                       try:
-                               f = urllib2.urlopen(link)
-                               hcode = f.code
-                       except:
-                               hcode = 404
-                       print "got hcode = " + str(hcode) + "\n"
-                       if (hcode != 200):
-                               break
-                       else:
-                               # Build db entry.
-                               # they look like: comic,id,link,url,filename,title
-                               # We already have comic, number/id and link
-                               
-                               if comic == 'xkcd':
-                                       s = f.read()
-                                       f.close()
-                                       splt = string.split(s, "<h3>Image URL (for hotlinking/embedding): ", 1)
-                                       splt2 = string.split(splt[1], "</h3>", 1)
-                                       url = splt2[0]
-                                       splt = string.split(splt[0], "<h1>", 1)
-                                       splt = string.split(splt[1], "</h1>", 1)
-                                       title = splt[0]
-
-                               elif comic == 'sinfest':
-                                       s = f.read()
-                                       f.close()
-                                       splt = string.split(s, 'height="107"', 1)
-                                       splt = string.split(splt[1], '<img src="', 1)
-                                       splt = string.split(splt[1], '" alt="', 1)
-                                       url = splt[0]
-                                       if len(url) < 49:
-                                               print "Fake 404! Break break break!"
-                                               break
-                                       splt = string.split(splt[1], '" border="0" />', 1)
-                                       title = splt[0]
-
-                               splt2 = string.rsplit(url, "/", 1)
-                               filename = splt2[1]                                     
-                               irow = [comic,number,link,url,filename,title]
+                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
 
-                       number += 1
+# ------------
 
 class ComicHomePlugin(hildondesktop.HomePluginItem):
        def __init__(self):