import cairo
import datetime
import shutil
+import sys
supports_alpha = False
+# LOGGING!
+# sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
+# sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
+# print "Start logging!"
+
# constants. dbfile is the location of the csv
# comiccache is the location of the images
-APP_VERSION = "0.3.1"
+APP_VERSION = "0.3.2"
basedbdir = "/opt/comic-widget/db/"
imagedir = "/opt/comic-widget/images/"
"dilbert":{"name":"Dilbert","link":"http://dilbert.com/","start":"2009-01-01","dbfile":dbdir + "comicdb.dilbert.csv"},
"cyanide":{"name":"Cyanide&Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
}
-
+defaults = {}
previous = False
next = False
+
# handling of the comics
class ComicDb():
def __init__(self, comic):
def get_comic(self):
print str(self.currentcomic)
if self.currentcomic < 0:
- self.refresh()
self.currentcomic = 0
+ if len(self.db) > 0:
+ fetchid = self.db[self.currentcomic]['id']
+ else:
+ fetchid = comics[self.comic]['start']
+ self.fetch_newer(self.comic, fetchid)
+ self.refresh()
if len(self.db) < (self.currentcomic + 1):
- self.currentcomic -= 1
+ self.currentcomic = len(self.db) - 1
+ if len(self.db) > 0:
+ fetchid = self.db[self.currentcomic]['id']
+ else:
+ fetchid = comics[self.comic]['start']
+ self.fetch_earlier(self.comic, fetchid)
self.refresh()
self.currentcomic = len(self.db) - 1
dbf.close()
def refresh(self):
- if len(self.db) < 1:
- self.currentcomic = -1
- self.fetch_latest_std(self.comic, self.start)
- elif self.currentcomic == 0 or self.currentcomic < 0:
- self.fetch_latest_std(self.comic, self.db[0]['id'])
- elif self.currentcomic == (len(self.db) - 1):
- self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
+ # if len(self.db) < 1:
+ # self.currentcomic = -1
+ # self.fetch_latest_std(self.comic, self.start)
+ # elif self.currentcomic == 0 or self.currentcomic < 0:
+ # self.fetch_latest_std(self.comic, self.db[0]['id'])
+ # elif self.currentcomic == (len(self.db) - 1):
+ # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
dbf = open(self.dbfile, 'r')
dbr = csv.DictReader(dbf)
self.db = []
self.db.insert(0,row)
dbf.close()
+
+# fetch earlier
def fetch_earlier(self, comic, earliest):
- if len(earliest) == 10:
+ print "fetch before, " + comic + " earliest"
+ if comic == "cyanide":
+ comicid = self.get_prev_id(comic,earliest)
+ print "got " + comicid + " as the one before current..."
+ elif len(earliest) == 10:
# date id.
dt = string.split(earliest, "-")
d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
comicid = int(earliest) - 1
irow = self.get_irow(comic, comicid)
- if irow:
+ if irow and irow[0] == "skip":
+ print "problem with this one, fetching " + str(irow[2]) + " instead."
+ irow = self.get_irow(comic, irow[2])
+
+ if irow and len(irow) > 3:
print "got irow: "
print irow
- print "\ninserting first...\n"
- self.insert_row_first(irow)
+ print "\ninserting...\n"
+ self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
else:
print "No comic found at " + comicid
+
+
+
+
+ def fetch_newer(self, comic, newest):
+ if comic == "cyanide":
+ comicid = self.get_next_id(comic,newest)
+ elif len(newest) == 10:
+ # date id.
+ dt = string.split(newest, "-")
+ d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+ newest = d + datetime.timedelta( 1 )
+ comicid = newest.isoformat()
+ else:
+ comicid = int(newest) + 1
+
+ irow = self.get_irow(comic, comicid)
+ if irow and irow[0] == "skip":
+ print "problem with this one, fetching " + str(irow[1]) + " instead."
+ irow = self.get_irow(comic, irow[1])
+
+ if irow and len(irow) > 3:
+ print "got irow: "
+ print irow
+ print "\ninserting...\n"
+ self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
+ else:
+ print "No comic found at " + str(comicid)
+
+
+
+
def fetch_latest_std(self, comic, latest):
print "fetching new after " + str(comic) + " " + str(latest)
next = False
if (hcode != 200):
return False
else:
- print "Cyanide & Happiness is unreliable, so we need to track next and prev"
+ # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
s = f.read()
f.close()
# title:
print "no 'next' found"
return False
else:
- return splt[0]
+ print "got next: " + splt[0]
+ return splt[0]
+ def get_prev_id(self, comic, number):
+ if comic == 'cyanide':
+ link = "http://www.explosm.net/comics/" + str(number) + "/"
+ print "link: " + link
+ try:
+ f = urllib2.urlopen(link)
+ hcode = f.code
+ except:
+ hcode = 404
+ print "got hcode = " + str(hcode) + "\n"
+ if (hcode != 200):
+ return False
+ else:
+ print "Cyanide & Happiness is unreliable, so we need to track next and prev"
+ s = f.read()
+ f.close()
+ # title:
+ splt = string.split(s, 'First</a> | <a href="/comics/', 1)
+ if len(splt) < 2:
+ print "no 'prev' found"
+ return False
+ splt = string.split(splt[1], '/">< Previous', 1)
+ if len(splt) < 2 or len(splt[0]) > 5:
+ print "no 'next' found"
+ return False
+ else:
+ print "got previous: " + splt[0]
+ return splt[0]
if len(url) < 49:
print "Fake 404! Break break break!"
return False
-
+
elif comic == 'cyanide':
s = f.read()
f.close()
splt2 = string.rsplit(url, "/", 1)
filename = splt2[1]
+ if filename == self.db[0]['filename']:
+ print "already exists! Break break break!"
+ return False
irow = [comic,number,link,url,filename,title]
return irow
# ------------UI
class ComicHomePlugin(hildondesktop.HomePluginItem):
- __gtype_name__ = 'ComicHomePlugin'
+ # __gtype_name__ = 'ComicHomePlugin'
def __init__(self):
hildondesktop.HomePluginItem.__init__(self)
global supports_alpha
elif func == 'switch':
self.active_comics = self.get_active_comics()
+ print "closing log"
+ #sys.stdout.close()
+ #sys.stdout = sys.__stdout__
+ #sys.stderr.close()
+ #sys.stderr = sys.__stderr__
print "active comics: " + str(self.active_comics)
self.keypointer = (self.keypointer + 1) % len(self.active_comics)
self.imgvpos = 0
# ************************* OPTION DIALOGS ********************************
def show_options(self, widget):
+ print "loading options dialog"
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)
comics_button.set_alignment(0,0,0,0)
comics_button.connect("clicked", self.show_comics)
- dialog.vbox.pack_start(about_button, True, True, 0)
+ print "adding buttons to dialog, starting with comics"
dialog.vbox.pack_start(comics_button, True, True, 0)
-
+ print "adding about button"
+ dialog.vbox.pack_start(about_button, True, True, 0)
+ print "show!"
dialog.show_all()
dialog.run()
dialog.destroy()
def show_about(self, widget):
+ print "in about dialog"
dialog = gtk.AboutDialog()
dialog.set_title("About")
dialog.set_name("Comic widget")
dialog.destroy()
def show_comics(self, widget):
+ print "in comics dialog! yay!"
dialog = gtk.Dialog("Configure Search Engines", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
comiclist = comics.keys()
comiclist.sort()
comicid = splt[0]
if os.path.isfile(activecomics) == True:
print "removing " + comicid + " from " + str(self.active_comics)
- del self.active_comics[self.active_comics.index(comicid)]
+ try:
+ del self.active_comics[self.active_comics.index(comicid)]
+ except:
+ pass
dbf = open(activecomics, 'w')
dbw = csv.writer(dbf)
dbw.writerow(self.active_comics)