16 socket.setdefaulttimeout(30)
18 supports_alpha = False
21 # sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
22 # sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
23 # print "Start logging!"
25 # constants. dbfile is the location of the csv
26 # comiccache is the location of the images
27 APP_VERSION = "0.4.1-1"
29 basedbdir = "/opt/comic-widget/db/"
30 imagedir = "/opt/comic-widget/images/"
31 dbdir = "/home/user/.comic-widget/"
32 activecomics = dbdir + "activecomics.cfg"
33 comiccache = "/home/user/MyDocs/.comics/"
34 defaultcomics = ['xkcd','wulff','sinfest']
36 "9_chickweed_lanecomicscom":{"name":"9 Chickweed Lane","link":"http://comics.com/9_chickweed_lane/","start":"2010-02-01","dbfile":dbdir + "comicdb.9chickweedlane.csv"},
37 "agnescomicscom":{"name":"Agnes","link":"http://comics.com/agnes/","start":"2010-02-01","dbfile":dbdir + "comicdb.agnes.csv"},
38 "andy_cappcomicscom":{"name":"Andy Capp","link":"http://comics.com/andy_capp/","start":"2010-02-01","dbfile":dbdir + "comicdb.andycapp.csv"},
39 "alley_oopcomicscom":{"name":"Alley Oop","link":"http://comics.com/alley_oop/","start":"2010-02-01","dbfile":dbdir + "comicdb.alleyoop.csv"},
40 "arlonjaniscomicscom":{"name":"Arlo and Janis","link":'http://comics.com/arlo&janis/',"start":"2010-02-01","dbfile":dbdir + "comicdb.arlonjanis.csv"},
41 "bccomicscom":{"name":"B.C.","link":"http://comics.com/bc/","start":"2010-02-01","dbfile":dbdir + "comicdb.bc.csv"},
42 "ballard_streetcomicscom":{"name":"Ballard Street","link":"http://comics.com/ballard_street/","start":"2010-02-01","dbfile":dbdir + "comicdb.ballardstreet.csv"},
43 "babyblues":{"name":"Baby Blues","link":"http://www.babyblues.com/","start":"01/19/2010","dbfile":dbdir + "comicdb.babyblues.csv"},
44 "bencomicscom":{"name":"Ben","link":'http://comics.com/ben/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ben.csv"},
45 "bettycomicscom":{"name":"Betty","link":"http://comics.com/betty/","start":"2010-02-01","dbfile":dbdir + "comicdb.betty.csv"},
46 "big_natecomicscom":{"name":"Big Nate","link":"http://comics.com/big_nate/","start":"2010-02-01","dbfile":dbdir + "comicdb.bignate.csv"},
47 "brevitycomicscom":{"name":"Brevity","link":"http://comics.com/brevity/","start":"2010-02-01","dbfile":dbdir + "comicdb.brevity.csv"},
48 "candorvillecomicscom":{"name":"Candorville","link":"http://comics.com/candorville/","start":"2010-02-01","dbfile":dbdir + "comicdb.candorville.csv"},
49 "cheap_thrillscomicscom":{"name":"Cheap Thrills","link":"http://comics.com/cheap_thrills/","start":"2010-02-01","dbfile":dbdir + "comicdb.cheapthrills.csv"},
50 "committedcomicscom":{"name":"Committed","link":"http://comics.com/committed/","start":"2010-02-01","dbfile":dbdir + "comicdb.committed.csv"},
51 "cownboycomicscom":{"name":"Cow and Boy","link":'http://comics.com/cow&boy/',"start":"2010-02-01","dbfile":dbdir + "comicdb.cownboy.csv"},
52 "cyanide":{"name":"Cyanide and Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
53 "daddyshomecomicscom":{"name":"Daddy's Home","link":"http://comics.com/daddys_home/","start":"2010-02-01","dbfile":dbdir + "comicdb.daddyshome.csv"},
54 "dilbert":{"name":"Dilbert","link":"http://dilbert.com/","start":"2010-01-01","dbfile":dbdir + "comicdb.dilbert.csv"},
55 "dog_eat_dougcomicscom":{"name":"Dog eat Doug","link":"http://comics.com/dog_eat_doug/","start":"2010-02-01","dbfile":dbdir + "comicdb.dogeatdoug.csv"},
56 "drabblecomicscom":{"name":"Drabble","link":"http://comics.com/drabble/","start":"2010-02-01","dbfile":dbdir + "comicdb.drabble.csv"},
57 "f_minuscomicscom":{"name":"F Minus","link":"http://comics.com/f_minus/","start":"2010-02-01","dbfile":dbdir + "comicdb.f_minus.csv"},
58 "family_treecomicscom":{"name":"Family Tree","link":"http://comics.com/family_tree/","start":"2010-02-01","dbfile":dbdir + "comicdb.familytree.csv"},
59 "farcuscomicscom":{"name":"Farcus","link":'http://comics.com/farcus/',"start":"2010-02-01","dbfile":dbdir + "comicdb.farcus.csv"},
60 "fat_catscomicscom":{"name":"Fat Cats","link":'http://comics.com/fat_cats_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.fatcats.csv"},
61 "ferdnandcomicscom":{"name":"Ferd'nand","link":'http://comics.com/ferdnand/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ferdnand.csv"},
62 "flight_deckcomicscom":{"name":"Flight_Deck","link":'http://comics.com/flight_deck/',"start":"2010-02-01","dbfile":dbdir + "comicdb.flightdeck.csv"},
63 "flonfriendscomicscom":{"name":"Flo and Friends","link":'http://comics.com/flo&friends/',"start":"2010-02-01","dbfile":dbdir + "comicdb.flonfriends.csv"},
64 "fort_knoxcomicscom":{"name":"Fort Knox","link":'http://comics.com/fort_knox/',"start":"2010-02-01","dbfile":dbdir + "comicdb.fortknox.csv"},
65 "franknernestcomicscom":{"name":"Frank and Ernest","link":'http://comics.com/frank&ernest/',"start":"2010-02-01","dbfile":dbdir + "comicdb.franknernest.csv"},
66 "frazzcomicscom":{"name":"Frazz","link":"http://comics.com/frazz/","start":"2010-02-01","dbfile":dbdir + "comicdb.frazz.csv"},
67 "free_rangecomicscom":{"name":"Free Range","link":'http://comics.com/free_range/',"start":"2010-02-01","dbfile":dbdir + "comicdb.freerange.csv"},
68 "geechcomicscom":{"name":"Geech","link":'http://comics.com/geech_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.geech.csv"},
69 "getfuzzycomicscom":{"name":"Get Fuzzy","link":"http://comics.com/get_fuzzy/","start":"2010-02-01","dbfile":dbdir + "comicdb.getfuzzy.csv"},
70 "girlsnsportscomicscom":{"name":"Girls and Sports","link":'http://comics.com/girls&sports/',"start":"2010-02-01","dbfile":dbdir + "comicdb.gitlsnsports.csv"},
71 "graffiticomicscom":{"name":"Graffiti","link":'http://comics.com/graffiti/',"start":"2010-02-01","dbfile":dbdir + "comicdb.graffiti.csv"},
72 "grand_avenuecomicscom":{"name":"Grand Avenue","link":'http://comics.com/grand_avenue/',"start":"2010-02-01","dbfile":dbdir + "comicdb.grandavenue.csv"},
73 "heathcliffcomicscom":{"name":"Heathcliff","link":'http://comics.com/heathcliff/',"start":"2010-02-01","dbfile":dbdir + "comicdb.heathcliff.csv"},
74 "herb_and_jamaalcomicscom":{"name":"Herb and Jamaal","link":'http://comics.com/herb_and_jamaal/',"start":"2010-02-01","dbfile":dbdir + "comicdb.herbandjamaal.csv"},
75 "hermancomicscom":{"name":"Herman","link":'http://comics.com/herman/',"start":"2010-02-01","dbfile":dbdir + "comicdb.herman.csv"},
76 "home_and_awaycomicscom":{"name":"Home and Away","link":'http://comics.com/home_and_away/',"start":"2010-02-01","dbfile":dbdir + "comicdb.homeandaway.csv"},
77 "its_all_about_youcomicscom":{"name":"It's All About You","link":'http://comics.com/its_all_about_you/',"start":"2010-02-01","dbfile":dbdir + "comicdb.itsallaboutyou.csv"},
78 "janes_worldcomicscom":{"name":"Jane's World","link":'http://comics.com/janes_world/',"start":"2010-02-01","dbfile":dbdir + "comicdb.janesworld.csv"},
79 "jump_startcomicscom":{"name":"Jump Start","link":'http://comics.com/jump_start/',"start":"2010-02-01","dbfile":dbdir + "comicdb.jumpstart.csv"},
80 "kit_n_carlylecomicscom":{"name":"Kit 'n' Carlyle","link":'http://comics.com/kit_n_carlyle/',"start":"2010-02-01","dbfile":dbdir + "comicdb.kitncarlyle.csv"},
81 "lil_abnercomicscom":{"name":"Li'l Abner","link":'http://comics.com/lil_abner_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.lilabner.csv"},
82 "liberty_meadowscomicscom":{"name":"Liberty Meadows","link":'http://comics.com/liberty_meadows/',"start":"2010-02-01","dbfile":dbdir + "comicdb.libertymeadows.csv"},
83 "little_dog_lostcomicscom":{"name":"Little Dog Lost","link":'http://comics.com/little_dog_lost/',"start":"2010-02-01","dbfile":dbdir + "comicdb.littledoglost.csv"},
84 "lolacomicscom":{"name":"Lola","link":'http://comics.com/lola/',"start":"2010-02-01","dbfile":dbdir + "comicdb.lola.csv"},
85 "luanncomicscom":{"name":"Luann","link":'http://comics.com/luann/',"start":"2010-02-01","dbfile":dbdir + "comicdb.luann.csv"},
86 "marmadukecomicscom":{"name":"Marmaduke","link":'http://comics.com/marmaduke/',"start":"2010-02-01","dbfile":dbdir + "comicdb.marmaduke.csv"},
87 "megcomicscom":{"name":"Meg","link":'http://comics.com/meg_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.meg.csv"},
88 "minimum_securitycomicscom":{"name":"Minimum Security","link":'http://comics.com/minimum_security/',"start":"2010-02-01","dbfile":dbdir + "comicdb.minimumsecurity.csv"},
89 "moderately_confusedcomicscom":{"name":"Moderately Confused","link":'http://comics.com/moderately_confused/',"start":"2010-02-01","dbfile":dbdir + "comicdb.moderatelyconfused.csv"},
90 "mommacomicscom":{"name":"Momma","link":'http://comics.com/momma/',"start":"2010-02-01","dbfile":dbdir + "comicdb.momma.csv"},
91 "motleycomicscom":{"name":"Motley","link":'http://comics.com/motley_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.motley.csv"},
92 "nancycomicscom":{"name":"Nancy","link":'http://comics.com/nancy/',"start":"2010-02-01","dbfile":dbdir + "comicdb.nancy.csv"},
93 "natural_selectioncomicscom":{"name":"Natural Selection","link":'http://comics.com/natural_selection/',"start":"2010-02-01","dbfile":dbdir + "comicdb.naturalselection.csv"},
94 "nest_headscomicscom":{"name":"Nest Heads","link":'http://comics.com/nest_heads/',"start":"2010-02-01","dbfile":dbdir + "comicdb.nestheads.csv"},
95 "off_the_markcomicscom":{"name":"Off the mark","link":"http://comics.com/off_the_mark/","start":"2010-02-01","dbfile":dbdir + "comicdb.offthemark.csv"},
96 "on_a_claire_daycomicscom":{"name":"On A Claire Day","link":'http://comics.com/on_a_claire_day/',"start":"2010-02-01","dbfile":dbdir + "comicdb.onaclaireday.csv"},
97 "one_big_happycomicscom":{"name":"One Big Happy","link":'http://comics.com/one_big_happy_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.onebighappy.csv"},
98 "over_the_hedgecomicscom":{"name":"Over the Hedge","link":'http://comics.com/over_the_hedge/',"start":"2010-02-01","dbfile":dbdir + "comicdb.overthehedge.csv"},
99 "pc_and_pixelcomicscom":{"name":"PC and Pixel","link":'http://comics.com/pc_and_pixel/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pcandpixel.csv"},
100 "peanutscomicscom":{"name":"Peanuts","link":"http://comics.com/peanuts/","start":"2010-02-01","dbfile":dbdir + "comicdb.peanuts.csv"},
101 "pearls_before_swinecomicscom":{"name":"Pearls Before Swine","link":'http://comics.com/pearls_before_swine/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pearlsbeforeswine.csv"},
102 "phd":{"name":"PHD Comics","link":"http://www.phdcomics.com/","start":1240,"dbfile":dbdir + "comicdb.phd.csv"},
103 "picklescomicscom":{"name":"Pickles","link":'http://comics.com/pickles/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pickles.csv"},
104 "prickly_citycomicscom":{"name":"Prickly City","link":'http://comics.com/prickly_city/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pricklycity.csv"},
105 "raising_duncancomicscom":{"name":"Raising Duncan","link":'http://comics.com/raising_duncan_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.raisingduncan.csv"},
106 "reality_checkcomicscom":{"name":"Reality Check","link":'http://comics.com/reality_check/',"start":"2010-02-01","dbfile":dbdir + "comicdb.realitycheck.csv"},
107 "rednrovercomicscom":{"name":"Red and Rover","link":'http://comics.com/red&rover/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rednrover.csv"},
108 "rip_haywirecomicscom":{"name":"Rip Haywire","link":'http://comics.com/rip_haywire/',"start":"2010-02-01","dbfile":dbdir + "comicdb.riphaywire.csv"},
109 "ripleys_believe_it_or_notcomicscom":{"name":"Ripley's Believe it or not","link":'http://comics.com/ripleys_believe_it_or_not/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ripleysbelieveitornot.csv"},
110 "rose_is_rosecomicscom":{"name":"Rose Is Rose","link":'http://comics.com/rose_is_rose/',"start":"2010-02-01","dbfile":dbdir + "comicdb.roseisrose.csv"},
111 "rubescomicscom":{"name":"Rubes","link":'http://comics.com/rubes/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rubes.csv"},
112 "rudy_parkcomicscom":{"name":"Rudy Park","link":'http://comics.com/rudy_park/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rudypark.csv"},
113 "scary_garycomicscom":{"name":"Scary Gary","link":'http://comics.com/scary_gary/',"start":"2010-02-01","dbfile":dbdir + "comicdb.scarygary.csv"},
114 "shirley_and_soncomicscom":{"name":"Shirley and Son","link":'http://comics.com/shirley_and_son_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.shirleyandson.csv"},
115 "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + "comicdb.sinfest.csv"},
116 "soup_to_nutzcomicscom":{"name":"Soup to Nutz","link":'http://comics.com/soup_to_nutz/',"start":"2010-02-01","dbfile":dbdir + "comicdb.souptonutz.csv"},
117 "speed_bumpcomicscom":{"name":"Speed Bump","link":'http://comics.com/speed_bump/',"start":"2010-02-01","dbfile":dbdir + "comicdb.speedbump.csv"},
118 "spot_the_frogcomicscom":{"name":"Spot the Frog","link":'http://comics.com/spot_the_frog/',"start":"2010-02-01","dbfile":dbdir + "comicdb.spotthefrog.csv"},
119 "state_of_the_unioncomicscom":{"name":"State of the Union","link":'http://comics.com/state_of_the_union/',"start":"2010-02-01","dbfile":dbdir + "comicdb.stateoftheunion.csv"},
120 "strange_brewcomicscom":{"name":"Strange Brew","link":'http://comics.com/strange_brew/',"start":"2010-02-01","dbfile":dbdir + "comicdb.strangebrew.csv"},
121 "tarzancomicscom":{"name":"Tarzan","link":'http://comics.com/tarzan_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.tarzan.csv"},
122 "thats_lifecomicscom":{"name":"That's Life","link":'http://comics.com/thats_life/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thatslife.csv"},
123 "the_barncomicscom":{"name":"The Barn","link":'http://comics.com/the_barn/',"start":"2010-02-01","dbfile":dbdir + "comicdb.the_barn.csv"},
124 "the_born_losercomicscom":{"name":"The Born Loser","link":'http://comics.com/the_born_loser/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thebornloser.csv"},
125 "the_bucketscomicscom":{"name":"The Buckets","link":'http://comics.com/the_buckets/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thebuckets.csv"},
126 "the_dinette_setcomicscom":{"name":"The Dinette Set","link":'http://comics.com/the_dinette_set/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thedinetteset.csv"},
127 "the_grizzwellscomicscom":{"name":"The Grizzwells","link":'http://comics.com/the_grizzwells/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thegrizzwells.csv"},
128 "the_humble_stumblecomicscom":{"name":"The Humble Stumble","link":'http://comics.com/the_humble_stumble/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thehumblestumble.csv"},
129 "the_knight_lifecomicscom":{"name":"The Knight Life","link":'http://comics.com/the_knight_life/',"start":"2010-02-01","dbfile":dbdir + "comicdb.theknightlife.csv"},
130 "the_meaning_of_lilacomicscom":{"name":"The Meaning of Lila","link":'http://comics.com/the_meaning_of_lila/',"start":"2010-02-01","dbfile":dbdir + "comicdb.themeaningoflila.csv"},
131 "the_other_coastcomicscom":{"name":"The Other Coast","link":'http://comics.com/the_other_coast/',"start":"2010-02-01","dbfile":dbdir + "comicdb.theothercoast.csv"},
132 "the_sunshine_clubcomicscom":{"name":"the Sunshine Club","link":'http://comics.com/the_sunshine_club/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thesunshineclub.csv"},
133 "unstrange_phenomenacomicscom":{"name":"Unstrange Phenomena","link":'http://comics.com/unstrange_phenomena/',"start":"2010-02-01","dbfile":dbdir + "comicdb.unstrangephenomena.csv"},
134 "watch_your_headcomicscom":{"name":"Watch Your Head","link":'http://comics.com/watch_your_head/',"start":"2010-02-01","dbfile":dbdir + "comicdb.watchyourhead.csv"},
135 "wizard_of_idcomicscom":{"name":"Wizard of Id","link":'http://comics.com/wizard_of_id/',"start":"2010-02-01","dbfile":dbdir + "comicdb.wizardofid.csv"},
136 "working_dazecomicscom":{"name":"Working Daze","link":'http://comics.com/working_daze/',"start":"2010-02-01","dbfile":dbdir + "comicdb.workingdaze.csv"},
137 "working_it_outcomicscom":{"name":"Working It Out","link":'http://comics.com/working_it_out/',"start":"2010-02-01","dbfile":dbdir + "comicdb.workingitout.csv"},
138 "wulff":{"name":"Wulffmorgenthaler","link":"http://wulffmorgenthaler.com/","start":"edd3411b-96ca-4d93-bd5f-0cf1deb67c8a","dbfile":dbdir + "comicdb.wulff.csv"},
139 "xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + "comicdb.xkcd.csv"},
140 "zack_hillcomicscom":{"name":"Zack Hill","link":'http://comics.com/zack_hill/',"start":"2010-02-01","dbfile":dbdir + "comicdb.zackhill.csv"},
145 defaults = {'width':480,'height':230}
146 #defaults = {'width':480,'height':240}
147 sizefile = "/home/user/.comic-widget/size.cfg"
152 # handling of the comics
154 def __init__(self, comic):
156 self.start = comics[self.comic]["start"]
157 self.dbfile = comics[self.comic]["dbfile"]
159 #if db file exist, read it
160 # if os.path.isfile(self.dbfile) == True:
161 # dbf = open(self.dbfile, 'r')
163 dbf = self.get_dbfile()
164 dbr = csv.DictReader(dbf)
167 self.db.insert(0,row)
169 if len(self.db) == 0:
171 self.currentcomic = 0
175 def get_dbfile(self):
176 #if db file exist, read it
177 print "fetching " + self.dbfile
178 if os.path.isfile(self.dbfile) == True:
179 print "found " + self.dbfile
180 dbf = open(self.dbfile, 'r')
184 print "file " + self.dbfile + " not found"
185 if not os.path.exists(dbdir):
186 print "path not found: " + dbdir
190 print "comic db creation failed on mkdir"
193 shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
195 print "comic db creation failed on copy"
196 print "creating new..."
197 dbf = open(self.dbfile, 'w')
198 dbf.write('comic,id,link,url,filename,title\n')
201 if os.path.isfile(self.dbfile) == True:
202 dbf = open(self.dbfile, 'r')
205 print "comic db creation failed after copy"
208 def get_stored(self):
210 idf = open(self.dbfile + ".last", 'r')
212 print "got stored index :" + str(retval)
218 def store_last(self, sid = 0):
220 idf = open(self.dbfile + ".last", 'w')
224 idf.write(str(self.currentcomic))
232 print str(self.currentcomic)
233 if self.currentcomic < 0:
234 self.currentcomic = 0
236 fetchid = self.db[self.currentcomic]['id']
238 fetchid = comics[self.comic]['start']
239 self.fetch_newer(self.comic, fetchid)
241 if len(self.db) < (self.currentcomic + 1):
243 self.currentcomic = len(self.db) - 1
245 fetchid = self.db[self.currentcomic]['id']
247 fetchid = comics[self.comic]['start']
248 self.fetch_earlier(self.comic, fetchid)
250 self.currentcomic = len(self.db) - 1
252 dbrow = self.db[self.currentcomic]
253 filename = comiccache + self.comic + "/" + dbrow['filename']
254 print filename + "\n"
256 if os.path.isfile(filename):
257 print "found, returning " + filename + "\n"
261 print filename + " not found\nretrieving " + dbrow['url'] + "\n"
262 if not os.path.exists(comiccache + self.comic + "/"):
263 print "create dir " + comiccache + self.comic + "/"
265 os.makedirs(comiccache + self.comic + "/")
268 print "comic db creation failed on mkdir"
270 f = open(filename, "wb")
271 print "writing to " + filename
272 req = urllib2.Request(dbrow['url'])
273 req.add_header('Referer', dbrow['link'])
274 tmpimg = urllib2.urlopen(req)
275 f.write(tmpimg.read())
279 # urllib.urlretrieve(dbrow['url'], filename)
281 if os.path.isfile(filename):
286 def get_current_row(self):
287 return self.db[self.currentcomic]
290 print str(self.currentcomic)
291 if self.currentcomic < 0:
293 self.currentcomic = 0
294 if len(self.db) < (self.currentcomic + 1):
295 self.currentcomic -= 1
296 return self.db[self.currentcomic]['link']
300 def insert_row(self, irow):
301 dbf = open(self.dbfile, 'a')
302 dbw = csv.writer(dbf)
305 stored = self.get_stored()
307 self.store_last(stored + 1)
309 def insert_row_first(self, irow):
310 dbf = open(self.dbfile, 'w')
311 dbf.write('comic,id,link,url,filename,title\n')
312 dbw = csv.writer(dbf)
315 dbf = open(self.dbfile, 'a')
316 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
324 # if len(self.db) < 1:
325 # self.currentcomic = -1
326 # self.fetch_latest_std(self.comic, self.start)
327 # elif self.currentcomic == 0 or self.currentcomic < 0:
328 # self.fetch_latest_std(self.comic, self.db[0]['id'])
329 # elif self.currentcomic == (len(self.db) - 1):
330 # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
331 dbf = open(self.dbfile, 'r')
332 dbr = csv.DictReader(dbf)
335 self.db.insert(0,row)
337 if len(self.db) == 0:
338 self.fetch_earlier(self.comic, self.start)
339 dbf = open(self.dbfile, 'r')
340 dbr = csv.DictReader(dbf)
343 self.db.insert(0,row)
348 def fetch_earlier(self, comic, earliest):
349 print "fetch before, " + comic + " earliest"
351 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
352 print "getting get_prev_id..."
353 comicid = self.get_prev_id(comic,earliest)
355 print "already at first comic"
357 print "got " + comicid + " as the one before current..."
358 elif len(str(earliest)) == 10:
360 dt = string.split(earliest, "-")
361 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
362 earlier = d - datetime.timedelta( 1 )
363 comicid = earlier.isoformat()
365 comicid = int(earliest) - 1
367 irow = self.get_irow(comic, comicid)
368 if irow and irow[0] == "skip":
369 print "problem with this one, fetching " + str(irow[2]) + " instead."
370 irow = self.get_irow(comic, irow[2])
372 if irow and len(irow) > 3:
375 print "\ninserting...\n"
376 self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
378 print "No comic found at " + comicid
384 def fetch_newer(self, comic, newest):
385 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
386 comicid = self.get_next_id(comic,newest)
388 print "already at last comic"
390 elif len(newest) == 10:
392 dt = string.split(newest, "-")
393 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
394 newest = d + datetime.timedelta( 1 )
395 comicid = newest.isoformat()
397 comicid = int(newest) + 1
399 irow = self.get_irow(comic, comicid)
400 if irow and irow[0] == "skip":
401 print "problem with this one, fetching " + str(irow[1]) + " instead."
402 irow = self.get_irow(comic, irow[1])
404 if irow and len(irow) > 3:
407 print "\ninserting...\n"
408 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
410 print "No comic found at " + str(comicid)
415 def fetch_latest_std(self, comic, latest):
416 print "fetching new after " + str(comic) + " " + str(latest)
419 if comic == 'cyanide':
420 next = self.get_next_id(comic, latest)
426 if len(str(latest)) == 10:
429 dt = string.split(latest, "-")
430 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
431 newer = d + datetime.timedelta( 1 )
432 comicid = newer.isoformat()
434 comicid = int(latest) + 1
437 lasturl = self.db[0]['url']
441 irow = self.get_irow(comic, comicid)
443 print "got irow: " + str(irow)
444 if str(irow[0]) == 'skip':
445 print "skipping this one..."
448 if irow[3] == lasturl:
449 print "Looping the same, break break break!"
453 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
459 dt = string.split(comicid, "-")
460 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
461 newer = d + datetime.timedelta( 1 )
462 comicid = newer.isoformat()
473 def get_next_id(self, comic, number):
474 if comic == 'babyblues':
475 link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
476 print "link: " + link
478 f = urllib2.urlopen(link)
482 print "got hcode = " + str(hcode) + "\n"
486 print "checking next Babyblues date.."
490 splt = string.split(s, 'nextStripLink', 1)
492 print "no 'prev' found"
495 next = splt[1][48:58]
496 print "got next: " + next
500 if comic == 'cyanide':
501 link = "http://www.explosm.net/comics/" + str(number) + "/"
502 print "link: " + link
504 f = urllib2.urlopen(link)
508 print "got hcode = " + str(hcode) + "\n"
512 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
516 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
518 print "no 'next' found"
520 splt = string.split(splt[1], '/">Next >', 1)
522 print "no 'next' found"
525 print "got next: " + splt[0]
528 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
529 print "link: " + link
531 f = urllib2.urlopen(link)
535 print "got hcode = " + str(hcode) + "\n"
542 splt = string.split(s, '<a href="/default.aspx?id=', 1)
543 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
544 print "no previous found"
545 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
546 print "no next found!"
549 splt = string.split(splt[1], '<a href="/default.aspx?id=', 1)
551 print "no next found! At newest comic!"
553 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
554 print "no next found!"
558 elif comic[-9:] == "comicscom":
559 link = comics[comic]['link'] + str(number) + "/"
560 print "link: " + link
562 f = urllib2.urlopen(link)
566 print "got hcode = " + str(hcode) + "\n"
572 return self.parse_comics_com(comic, s, 'next')
579 def get_prev_id(self, comic, number):
580 if comic == 'babyblues':
581 link = 'http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=' + str(number)
582 print "link: " + link
584 f = urllib2.urlopen(link)
588 print "got hcode = " + str(hcode) + "\n"
592 print "checking prev Babyblues date.."
596 splt = string.split(s, 'prevStripLink', 1)
598 print "no 'prev' found"
602 prev = splt[1][48:58]
603 print "got previous: " + prev
606 if comic == 'cyanide':
607 link = "http://www.explosm.net/comics/" + str(number) + "/"
608 print "link: " + link
610 f = urllib2.urlopen(link)
614 print "got hcode = " + str(hcode) + "\n"
618 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
622 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
624 print "no 'prev' found"
626 splt = string.split(splt[1], '/">< Previous', 1)
627 if len(splt) < 2 or len(splt[0]) > 5:
628 print "no 'next' found"
631 print "got previous: " + splt[0]
633 elif comic == 'wulff':
634 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
635 print "link: " + link
637 f = urllib2.urlopen(link)
641 print "got hcode = " + str(hcode) + "\n"
648 splt = string.split(s, '<a href="/default.aspx?id=', 1)
649 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
652 print "no previous found"
656 elif comic[-9:] == "comicscom":
657 print "fetch prev, still in function..."
658 link = comics[comic]['link'] + str(number) + "/"
659 print "link: " + link
661 f = urllib2.urlopen(link)
665 print "got hcode = " + str(hcode) + "\n"
671 return self.parse_comics_com(comic, s, 'prev')
676 def get_irow(self, comic, number):
678 if len(str(number)) == 10:
683 print "number is now: " + str(number) + "\n"
685 link = "http://xkcd.org/" + str(number) + "/"
686 elif comic == 'sinfest':
687 link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
688 elif comic == 'dilbert':
689 link = "http://dilbert.com/" + str(number) + "/"
691 link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
692 elif comic == 'cyanide':
693 link = "http://www.explosm.net/comics/" + str(number) + "/"
694 elif comic == 'wulff':
695 link = "http://wulffmorgenthaler.com/default.aspx?id=" + str(number)
696 elif comic == 'babyblues':
697 link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
698 elif comic[-9:] == 'comicscom':
699 link = comics[comic]['link'] + str(number) + "/"
703 print "link: " + link
705 f = urllib2.urlopen(link)
709 print "got hcode = " + str(hcode) + "\n"
714 # they look like: comic,id,link,url,filename,title
715 # We already have comic, number/id and link
720 # This should be done with regex but...
721 splt = string.split(s, 'png" title="', 1)
722 splt = string.split(splt[1], '" alt="', 1)
724 splt = string.split(splt[1], "<h3>Image URL (for hotlinking/embedding): ", 1)
725 splt = string.split(splt[1], "</h3>", 1)
729 elif comic == 'sinfest':
732 splt = string.split(s, 'height="107"', 1)
733 splt = string.split(splt[1], '<img src="', 1)
734 splt = string.split(splt[1], '" alt="', 1)
737 print "Fake 404! Break break break!"
739 splt = string.split(splt[1], '" border="0" />', 1)
742 elif comic == 'dilbert':
745 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
746 splt = string.split(splt[1], '" />', 1)
747 url = "http://dilbert.com" + splt[0]
749 print "Fake 404! Break break break!"
757 splt = string.split(s, '<title>PHD Comics: ', 1)
758 splt = string.split(splt[1], '</title>', 1)
760 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
761 splt = string.split(splt[1], ' align=top>', 1)
764 print "Fake 404! Break break break!"
767 elif comic == 'cyanide':
771 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
776 splt = string.split(splt[1], '/">< ', 1)
778 print "got prev: " + prev
779 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
784 splt = string.split(splt[1], '/">Next ></a>', 1)
785 if len(splt[0]) > 10:
789 print "got next: " + next
791 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
795 splt = string.split(splt[1], ' <b>by <a href="', 1)
798 print "title wrong..."
800 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
802 print "a video? Try skipping"
803 return ['skip',next,prev]
805 splt = string.split(splt[1], '">', 1)
806 url = "http://www.explosm" + splt[0]
807 splt2 = string.rsplit(url, "/", 1)
809 irow = [comic,number,link,url,filename,title,next,prev]
813 elif comic == 'babyblues':
814 # babyblues is type .gif
817 splt = string.split(s, 'http://est.rbma.com/content/Baby_Blues?date=', 1)
820 datesplt = string.split(number, "/")
821 flncheck = datesplt[2] + datesplt[0] + datesplt[1]
822 if not flnm == flncheck:
823 print "incorrect filename, end of line. Break break break!"
825 url = 'http://est.rbma.com/content/Baby_Blues?date=' + flnm
826 title = number + " (2 weeks delay)"
827 filename = flnm + ".gif"
828 irow = [comic,number,link,url,filename,title]
832 elif comic == 'wulff':
836 splt = string.split(s, '<img src="pics/dates/month/', 1)
837 if not splt[1][1:2] == ".":
840 title = "0" + splt[1][:1]
841 splt = string.split(splt[1], '<img src="pics/dates/day/', 1)
842 if not splt[1][1:2] == ".":
843 title = title + "-" + splt[1][:2]
845 title = title + "-0" + splt[1][:1]
846 splt = string.split(splt[1], '<img src="pics/dates/year/', 1)
847 title = splt[1][:4] + "-" + title
849 splt = string.split(splt[1], 'id="ctl00_content_Strip1_imgStrip" class="strip" src="striphandler.ashx?stripid=', 1)
850 number = splt[1][:36]
851 url = "http://wulffmorgenthaler.com/striphandler.ashx?stripid=" + number
852 filename = number + ".gif"
853 irow = [comic,number,link,url,filename,title]
855 elif comic[-9:] == 'comicscom':
856 url = self.parse_comics_com(comic, f.read(), 'url')
860 splt2 = string.rsplit(url, "/", 1)
863 if filename == self.db[0]['filename']:
864 print "already exists! Break break break!"
868 irow = [comic,number,link,url,filename,title]
872 def parse_comics_com(self, comicname, source, get_what):
873 # rel="{StripID:309486, ComicID:36, Type:'Comic', DateStrip:'2010-02-06', URL_Comic: 'girls&sports', Link_Previous: '/girls&sports/2010-02-05/', Link_Next: '/girls&sports/2010-02-07/'}"
874 if get_what == "url":
875 splt = string.split(source, 'StripID:', 1)
876 splt = string.split(splt[1], ', ComicID:', 1)
877 url = "http://c0389161.cdn.cloudfiles.rackspacecloud.com/dyn/str_strip/" + splt[0] + ".full.gif"
879 elif get_what == "next":
880 splt = string.split(source, "Link_Next: '", 1)
881 splt = string.split(splt[1], "'", 1)
882 splt = string.split(splt[0], "/")
889 elif get_what == "prev":
890 print "getting comics com prev: " + comicname
891 splt = string.split(source, "Link_Previous: '", 1)
892 splt = string.split(splt[1], "'", 1)
893 splt = string.split(splt[0], "/")
897 if len(splt[2]) == 10:
906 class ComicHomePlugin(hildondesktop.HomePluginItem):
907 # __gtype_name__ = 'ComicHomePlugin'
909 hildondesktop.HomePluginItem.__init__(self)
910 global supports_alpha
911 self.set_settings(True)
912 self.active_comics = self.get_active_comics()
913 self.sizes = self.get_size_settings()
914 self.new_width = self.sizes['width']
915 self.new_height = self.sizes['height']
916 self.connect("show-settings", self.show_options)
917 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
918 self.osso_rpc = osso.Rpc(self.osso_c)
920 if len(self.active_comics) < 1:
921 self.active_comics = ['xkcd']
923 self.db = ComicDb(self.active_comics[self.keypointer])
924 self.comicname = comics[self.active_comics[self.keypointer]]['name']
925 self.set_name = "comicwidget"
926 screen = self.get_screen()
927 colormap = screen.get_rgba_colormap()
929 colormap = screen.get_rgb_colormap()
930 supports_alpha = False
932 supports_alpha = True
933 self.set_colormap(colormap)
934 self.set_app_paintable(True)
936 self.set_size_request(self.sizes['width'], self.sizes['height'])
938 self.vbox = gtk.VBox()
939 self.hbox = gtk.HBox()
940 self.label = gtk.Label()
941 self.label.set_alignment(xalign = 0, yalign = 0.5)
942 self.label.set_use_markup(True)
943 self.label.set_markup(self.get_markup())
946 self.e_goweb = gtk.EventBox()
947 self.e_goweb.set_name('goweb')
948 self.e_goweb.set_size_request(self.sizes['width'] - 250, 48)
949 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
950 # it will change to be a cairo thing when I have the time.
951 self.e_open = gtk.EventBox()
952 self.e_open.set_name('open')
953 self.e_switch = gtk.EventBox()
954 self.e_switch.set_name('switch')
955 self.e_switch.set_size_request(48, 48)
956 self.e_next = gtk.EventBox()
957 self.e_next.set_name('next')
958 self.e_next.set_size_request(48, 48)
959 self.e_prev = gtk.EventBox()
960 self.e_prev.set_name('prev')
961 self.e_prev.set_size_request(48, 48)
962 self.e_up = gtk.EventBox()
963 self.e_up.set_name('up')
964 self.e_up.set_size_request(48, 48)
965 self.e_down = gtk.EventBox()
966 self.e_down.set_name('down')
967 self.e_down.set_size_request(48, 48)
968 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
969 self.comic_image.show()
972 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
974 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
975 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
976 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
977 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
978 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
979 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
980 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
981 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
982 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
983 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
988 self.e_goweb.add(self.label)
989 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
990 self.e_goweb.connect("button-release-event", self.view_comic)
992 self.e_open.add(self.comic_image)
994 # self.e_switch.add(self.switch)
995 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
996 self.e_switch.connect("button-press-event", self.button_press)
997 self.e_switch.connect("button-release-event", self.button_release)
998 self.e_switch.connect("leave-notify-event", self.button_release)
1001 # self.e_next.add(self.next)
1002 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
1003 self.e_next.connect("button-press-event", self.button_press)
1004 self.e_next.connect("button-release-event", self.button_release)
1005 self.e_next.connect("leave-notify-event", self.button_release)
1007 #self.e_prev.add(self.prev)
1008 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
1009 self.e_prev.connect("button-press-event", self.button_press)
1010 self.e_prev.connect("button-release-event", self.button_release)
1011 self.e_prev.connect("leave-notify-event", self.button_release)
1013 #self.e_up.add(self.up)
1014 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
1015 self.e_up.connect("button-press-event", self.button_press)
1016 self.e_up.connect("button-release-event", self.button_release)
1017 self.e_up.connect("leave-notify-event", self.button_release)
1020 #self.e_down.add(self.down)
1021 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
1022 self.e_down.connect("button-press-event", self.button_press)
1023 self.e_down.connect("button-release-event", self.button_release)
1024 self.e_down.connect("leave-notify-event", self.button_release)
1026 self.vbox.pack_start(self.e_open,False,False,0)
1027 self.vbox.pack_end(self.hbox,False,False,0)
1029 self.hbox.pack_start(self.e_switch,False,False,0)
1030 self.hbox.pack_start(self.e_goweb,False,False,5)
1031 self.hbox.pack_end(self.e_next,False,False,0)
1032 self.hbox.pack_end(self.e_prev,False,False,0)
1033 self.hbox.pack_end(self.e_down,False,False,0)
1034 self.hbox.pack_end(self.e_up,False,False,0)
1035 self.vbox.show_all()
1040 # **************** Cairo draw functions ***************
1042 def realize(self, widget):
1043 global supports_alpha
1045 # To check if the display supports alpha channels, get the colormap
1046 screen = self.get_screen()
1047 colormap = screen.get_rgba_colormap()
1048 if colormap == None:
1049 print 'Your screen does not support alpha channels!'
1050 colormap = screen.get_rgb_colormap()
1051 supports_alpha = False
1053 # print 'Your screen supports alpha channels!'
1054 supports_alpha = True
1056 # Now we have a colormap appropriate for the screen, use it
1057 self.set_colormap(colormap)
1061 def do_expose_event(widget, event):
1062 cr = widget.window.cairo_create()
1063 widget.draw(widget.e_up, 0)
1064 widget.draw(widget.e_down, 0)
1065 widget.draw(widget.e_prev,0)
1066 widget.draw(widget.e_next, 0)
1067 widget.draw(widget.e_switch, 0)
1068 # widget.writeit(widget.e_goweb, "asdf")
1069 cr.set_operator(cairo.OPERATOR_OVER)
1070 region = gtk.gdk.region_rectangle(event.area)
1072 bg_color=gtk.gdk.color_parse('#000000')
1074 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
1076 cr.set_source_rgb (0.0, 0.0, 0.0)
1081 # Function that draws a button preloaded in the images dictionary.
1082 def draw(self, target, postfix):
1083 cr_e = target.window.cairo_create()
1084 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
1085 cr_e.set_operator(cairo.OPERATOR_SOURCE)
1087 cr_e.set_operator(cairo.OPERATOR_OVER)
1088 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
1091 # ********** Event handlers *************
1094 # Most events happen on release, so I moved all of them into the same event handler.
1095 def button_release(self, widget, event):
1096 if event.type == gtk.gdk.BUTTON_RELEASE:
1097 func = widget.get_name()
1098 print "pressed: " + func
1100 self.e_open.remove(self.comic_image)
1101 self.imgvpos = self.imgvpos - ((self.sizes['height'] - 48) / 2.1)
1102 if self.imgvpos < 0:
1104 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1105 self.e_open.add(self.comic_image)
1106 self.e_open.show_all()
1108 elif func == 'down':
1109 self.e_open.remove(self.comic_image)
1110 self.imgvpos = self.imgvpos + ((self.sizes['height'] - 48) / 2.1)
1111 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1112 self.e_open.add(self.comic_image)
1113 self.e_open.show_all()
1115 elif func == 'next':
1116 self.db.currentcomic -= 1
1118 self.e_open.remove(self.comic_image)
1119 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1120 self.e_open.add(self.comic_image)
1121 self.e_open.show_all()
1122 self.e_goweb.remove(self.label)
1123 titl = str(self.db.db[self.db.currentcomic]['title'])
1125 titl = titl[:19] + "..."
1126 self.label.set_markup(self.get_markup())
1127 self.e_goweb.add(self.label)
1128 self.e_goweb.show_all()
1130 elif func == 'prev':
1131 self.db.currentcomic += 1
1133 self.e_open.remove(self.comic_image)
1134 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1135 self.e_open.add(self.comic_image)
1136 self.e_open.show_all()
1137 self.e_goweb.remove(self.label)
1138 titl = str(self.db.db[self.db.currentcomic]['title'])
1140 titl = titl[:19] + "..."
1141 self.label.set_markup(self.get_markup())
1142 self.e_goweb.add(self.label)
1143 self.e_goweb.show_all()
1145 elif func == 'switch':
1146 # Check if store needed..
1147 if not self.db.currentcomic == 0 and not self.db.currentcomic == (len(self.db.db) - 1):
1148 print "Storing location " + str(self.db.currentcomic)
1149 if self.db.store_last():
1150 print "stored successfully"
1152 print "store failed"
1154 self.active_comics = self.get_active_comics()
1155 # print "closing log"
1157 #sys.stdout = sys.__stdout__
1159 #sys.stderr = sys.__stderr__
1160 print "active comics: " + str(self.active_comics)
1161 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
1164 print "switching to " + self.active_comics[self.keypointer]
1165 self.comicname = comics[self.active_comics[self.keypointer]]['name']
1166 self.db = ComicDb(self.active_comics[self.keypointer])
1167 self.e_open.remove(self.comic_image)
1168 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1169 self.e_goweb.remove(self.label)
1170 titl = str(self.db.db[self.db.currentcomic]['title'])
1172 titl = titl[:19] + "..."
1173 self.label.set_markup(self.get_markup())
1174 self.e_goweb.add(self.label)
1175 self.e_goweb.show_all()
1176 self.e_open.add(self.comic_image)
1177 self.e_open.show_all()
1181 self.draw(widget, "0")
1183 # this one only switches to pressed button image
1184 def button_press(self, widget, event):
1185 if not event.type == gtk.gdk.BUTTON_PRESS:
1187 self.draw(widget, "1")
1189 # this one cancels a button press when pointer is moved out from it after press.
1190 def button_out(self, widget, event):
1191 if not event.type == gtk.gdk.BUTTON_PRESS:
1193 self.draw(widget, "0")
1195 # no fancy cairo stuff here. simply launches url in browser.
1196 def view_comic(self, widget, event):
1197 if not event.type == gtk.gdk.BUTTON_RELEASE:
1199 link = self.db.get_link()
1200 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
1204 # this function modifies the image to fit the widget and then returns it.
1206 def get_resized_pixmap(self, filename, vpos):
1207 print "vpos: " + str(vpos)
1209 width = int(self.sizes['width'])
1210 height = int(self.sizes['height'])
1211 picwidth = width - 2
1212 maxheight = height - 48
1214 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
1215 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
1216 parent_buf.fill(0xffffffff)
1217 retimg = gtk.Image()
1218 # get correct width/height
1220 new_width = picwidth
1221 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
1222 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
1223 print "scaled height: " + str(scaled_buf.get_height())
1224 if scaled_buf.get_height() > maxheight:
1225 if scaled_buf.get_height() - (vpos + maxheight) < 0:
1226 vpos = scaled_buf.get_height() - maxheight
1227 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
1228 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
1231 tmpy = maxheight - scaled_buf.get_height()
1235 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
1237 # test = parent_buf.render_pixmap_and_mask(0)
1238 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
1239 retimg.set_from_pixbuf(parent_buf)
1240 print retimg.get_pixel_size()
1241 print "vpos after fixing image: " + str(self.imgvpos)
1244 def screen_changed(self, widget, old_screen=None):
1245 global supports_alpha
1247 # To check if the display supports alpha channels, get the colormap
1248 screen = self.get_screen()
1249 colormap = screen.get_rgba_colormap()
1250 if colormap == None:
1251 print 'Your screen does not support alpha channels!'
1252 colormap = screen.get_rgb_colormap()
1253 supports_alpha = False
1255 # print 'Your screen supports alpha channels!'
1256 supports_alpha = True
1258 # Now we have a colormap appropriate for the screen, use it
1259 self.set_colormap(colormap)
1264 def get_active_comics(self):
1267 if os.path.isfile(activecomics) == True:
1268 print "found " + activecomics
1269 dbf = open(activecomics, 'r')
1270 dbr = csv.reader(dbf)
1279 print "file " + activecomics + " not found"
1280 if not os.path.exists(dbdir):
1281 print "path not found: " + dbdir
1285 print "settings dir creation failed. Exiting."
1288 # dbf = file(activecomics, 'w')
1290 dbf = open(activecomics, 'w')
1292 print "error storing settings"
1294 dbw = csv.writer(dbf)
1295 dbw.writerow(defaultcomics)
1301 def get_markup(self):
1302 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1303 return '<span size="12000" face="arial"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1305 return '<span size="12000" face="arial"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1308 #check if settings file exists
1310 #if not, write defaults
1312 def goto_comic(self, index):
1313 self.db.currentcomic = index
1314 self.e_open.remove(self.comic_image)
1315 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1316 self.e_open.add(self.comic_image)
1317 self.e_open.show_all()
1318 self.e_goweb.remove(self.label)
1319 titl = str(self.db.db[self.db.currentcomic]['title'])
1321 titl = titl[:19] + "..."
1322 self.label.set_markup(self.get_markup())
1323 self.e_goweb.add(self.label)
1324 self.e_goweb.show_all()
1326 # ************************* OPTION DIALOGS ********************************
1328 def show_options(self, widget):
1329 print "loading options dialog"
1330 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1332 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1333 about_button.set_text("About", "See Author, Copyright and License information")
1334 about_button.set_alignment(0,0,0,0)
1335 about_button.connect("clicked", self.show_about)
1337 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1338 comics_button.set_text("Comics", "Choose which comics to view")
1339 comics_button.set_alignment(0,0,0,0)
1340 comics_button.connect("clicked", self.show_comics)
1342 navfirst_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1343 navfirst_button.set_text("Go to earliest", "Jump to the earliest cached comic")
1344 navfirst_button.set_alignment(0,0,0,0)
1345 navfirst_button.connect("clicked", self.show_nav, "oldest")
1347 navlast_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1348 navlast_button.set_text("Go to latest", "Jump to the latest cached comic")
1349 navlast_button.set_alignment(0,0,0,0)
1350 navlast_button.connect("clicked", self.show_nav, "newest")
1352 navstored_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1353 navstored_button.set_text("Go to last visited", "Jump to where you were before you switched comics")
1354 navstored_button.set_alignment(0,0,0,0)
1355 navstored_button.connect("clicked", self.show_nav, "stored")
1357 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1358 # size_button.set_text("Change size", "Change the size of the widget")
1359 # size_button.set_alignment(0,0,0,0)
1360 # size_button.connect("clicked", self.show_size)
1364 print "adding buttons to dialog, starting with comics"
1365 dialog.vbox.pack_start(navstored_button, True, True, 0)
1366 dialog.vbox.pack_start(navfirst_button, True, True, 0)
1367 dialog.vbox.pack_start(navlast_button, True, True, 0)
1368 dialog.vbox.pack_start(comics_button, True, True, 0)
1369 print "adding about button"
1370 dialog.vbox.pack_start(about_button, True, True, 0)
1371 # dialog.vbox.pack_start(size_button, True, True, 0)
1377 def show_nav(self, widget, goto):
1378 if goto == "stored":
1379 print "look for stored file"
1380 self.goto_comic(self.db.get_stored())
1381 elif goto == "newest":
1383 elif goto == "oldest":
1384 self.goto_comic(len(self.db.db) - 1)
1385 def show_about(self, widget):
1386 print "in about dialog"
1387 dialog = gtk.AboutDialog()
1388 dialog.set_title("About")
1389 dialog.set_name("Comic widget")
1390 dialog.set_version(APP_VERSION)
1391 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1392 dialog.set_authors(["Marcus Wikstrom <mece@ovi.com>\nLogo by Martin Wikstrom\n\nSpecial thanks to all the great python developers on t.m.o who share their code, and the helpful wizards on #maemo."])
1393 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1394 dialog.set_comments("Silly rabbit, tricks are for kids.")
1395 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/>.""")
1396 dialog.set_wrap_license(True)
1401 def show_comics(self, widget, data = None):
1402 print "in comics dialog! yay!"
1403 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1404 comiclist = comics.keys()
1408 self.liststore = gtk.ListStore(str,str,bool)
1409 for comicid in comiclist:
1411 for acomic in self.active_comics:
1412 if acomic == comicid:
1416 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1418 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1419 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1420 self.treeview.set_model(self.liststore)
1421 self.treeview.set_reorderable(True)
1422 tvcolumn = gtk.TreeViewColumn('Column 0')
1423 self.treeview.append_column(tvcolumn)
1424 cell = gtk.CellRendererText()
1425 tvcolumn.pack_start(cell, True)
1426 tvcolumn.add_attribute(cell, 'text', 0)
1428 ts = hildon.PannableArea()
1430 ts.set_size_request(-1, 280)
1431 ts.add(self.treeview)
1432 selection = self.treeview.get_selection()
1434 dialog.vbox.pack_start(ts,True,True,0)
1436 selection.connect("changed", self.pick_comic, comiclist)
1440 def pick_comic(self, widget, data):
1441 selected = widget.get_selected()
1442 if selected == None:
1445 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1446 if selected[0].get_value(selected[1],2):
1447 self.remove_comic(selected[0].get_value(selected[1],1))
1448 splt = selected[0].get_value(selected[1],0)[1:]
1449 selected[0].set_value(selected[1],0," " + splt)
1450 selected[0].set_value(selected[1],2,False)
1452 self.add_comic(selected[0].get_value(selected[1],1))
1453 splt = selected[0].get_value(selected[1],0)[1:]
1454 selected[0].set_value(selected[1],0,"*" + splt)
1455 selected[0].set_value(selected[1],2,True)
1458 def add_comic(self, comicid):
1459 if os.path.isfile(activecomics) == True:
1460 print "added " + comicid + " to " + str(self.active_comics)
1462 found = self.active_comics.index(comicid)
1464 self.active_comics.append(comicid)
1465 dbf = open(activecomics, 'w')
1466 dbw = csv.writer(dbf)
1467 dbw.writerow(self.active_comics)
1470 def remove_comic(self, comicid):
1471 if os.path.isfile(activecomics) == True:
1472 print "removing " + comicid + " from " + str(self.active_comics)
1474 del self.active_comics[self.active_comics.index(comicid)]
1477 dbf = open(activecomics, 'w')
1478 dbw = csv.writer(dbf)
1479 dbw.writerow(self.active_comics)
1483 def get_size_settings(self):
1484 # defaults = {'width':480,'height':230}
1485 # options: w 798 700 600 500 480 400
1486 # options: h 420 360 310 270 240 200
1487 print "checking to see if config file is available"
1488 if os.path.isfile(sizefile) == True:
1489 print "found " + sizefile
1490 dbf = open(sizefile, 'r')
1491 dbr = csv.reader(dbf)
1496 print "empty config file"
1500 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1501 if sizes['width'] > 798:
1502 sizes['width'] = 798
1503 if sizes['width'] < 400:
1504 sizes['width'] = 400
1505 if sizes['height'] > 420:
1506 sizes['height'] = 420
1507 if sizes['height'] < 200:
1508 sizes['height'] = 200
1515 print "no config file"
1518 hd_plugin_type = ComicHomePlugin
1521 if __name__ == "__main__":
1523 gobject.type_register(hd_plugin_type)
1524 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")