bugfixes
[comic-widget] / src / usr / lib / hildon-desktop / comic-widget.py
index 46b9824..0965909 100644 (file)
@@ -9,6 +9,7 @@ import os
 import osso
 import cairo
 import datetime
+import shutil
 
 supports_alpha = False
 
@@ -16,8 +17,9 @@ supports_alpha = False
 # comiccache is the location of the images
 APP_VERSION = "0.2.5"
 
+basedbdir = "/opt/comic-widget/db/"
 imagedir = "/opt/comic-widget/images/"
-dbdir = "/home/user/MyDocs/.comics/db/"
+dbdir = "/home/user/.comic-widget/"
 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"},
@@ -26,28 +28,15 @@ comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":d
 # handling of the comics
 class ComicDb():
        def __init__(self, comic):
-               #if no db, start from this comic id.
                self.comic = comic
                self.start = comics[self.comic]["start"]
                self.dbfile = comics[self.comic]["dbfile"]
 
                #if db file exist, read it
-               if os.path.isfile(self.dbfile) == True:
-                       dbf = open(self.dbfile, 'r')
+               # if os.path.isfile(self.dbfile) == True:
+               #       dbf = open(self.dbfile, 'r')
                # if not, create it.
-               else:
-                       dbf = open(self.dbfile, 'w')
-                       dbf.write('comic,id,link,url,filename,title\n')
-                       dbf.close()
-                       dbf = open(self.dbfile, 'r')
-                       self.fetch_latest_std(self.comic, self.start)
-
-               if not os.path.exists(comiccache + self.comic + "/"):
-                       try:
-                               os.makedirs(comiccache + self.comic + "/")
-                       except:
-                               print "comic cache creation failed"
-                               exit()
+               dbf = self.get_dbfile()
                dbr = csv.DictReader(dbf)
                self.db = []
                for row in dbr:
@@ -55,6 +44,38 @@ class ComicDb():
                dbf.close()
                self.currentcomic = 0           
 
+
+
+       def get_dbfile(self):
+               #if db file exist, read it
+               print "fetching " + self.dbfile
+               if os.path.isfile(self.dbfile) == True:
+                       print "found " + self.dbfile
+                       dbf = open(self.dbfile, 'r')
+                       return dbf
+               # if not, copy it.
+               else:
+                       print "file " + self.dbfile + " not found"
+                       if not os.path.exists(dbdir + self.comic + "/"):
+                               print "path not found: " + dbdir + self.comic + "/"
+                               try:
+                                       os.makedirs(dbdir + self.comic + "/")
+                               except:
+                                       print "comic db creation failed on mkdir"
+                                       exit()
+                       try:
+                               shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
+                       except:
+                               print "comic db creation failed on copy"
+                               exit()
+                       if os.path.isfile(self.dbfile) == True:
+                               dbf = open(self.dbfile, 'r')
+                               return dbf
+                       else:
+                               print "comic db creation failed after copy"
+                               exit()
+
+
        def get_comic(self):
                print str(self.currentcomic)
                if self.currentcomic < 0:
@@ -75,6 +96,13 @@ class ComicDb():
                else:
                        #fetch file
                        print filename + " not found\nretrieving " + dbrow['url'] + "\n"
+                       if not os.path.exists(comiccache + self.comic + "/"):
+                               print "create dir " + comiccache + self.comic + "/"
+                               try:
+                                       os.makedirs(comiccache + self.comic + "/")
+                                       print "created dir"
+                               except:
+                                       print "comic db creation failed on mkdir"
                        urllib.urlretrieve(dbrow['url'], filename)
                        print "success\n"
                        return filename