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
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"
209 print str(self.currentcomic)
210 if self.currentcomic < 0:
211 self.currentcomic = 0
213 fetchid = self.db[self.currentcomic]['id']
215 fetchid = comics[self.comic]['start']
216 self.fetch_newer(self.comic, fetchid)
218 if len(self.db) < (self.currentcomic + 1):
220 self.currentcomic = len(self.db) - 1
222 fetchid = self.db[self.currentcomic]['id']
224 fetchid = comics[self.comic]['start']
225 self.fetch_earlier(self.comic, fetchid)
227 self.currentcomic = len(self.db) - 1
229 dbrow = self.db[self.currentcomic]
230 filename = comiccache + self.comic + "/" + dbrow['filename']
231 print filename + "\n"
233 if os.path.isfile(filename):
234 print "found, returning " + filename + "\n"
238 print filename + " not found\nretrieving " + dbrow['url'] + "\n"
239 if not os.path.exists(comiccache + self.comic + "/"):
240 print "create dir " + comiccache + self.comic + "/"
242 os.makedirs(comiccache + self.comic + "/")
245 print "comic db creation failed on mkdir"
247 f = open(filename, "wb")
248 print "writing to " + filename
249 req = urllib2.Request(dbrow['url'])
250 req.add_header('Referer', dbrow['link'])
251 tmpimg = urllib2.urlopen(req)
252 f.write(tmpimg.read())
256 # urllib.urlretrieve(dbrow['url'], filename)
258 if os.path.isfile(filename):
263 def get_current_row(self):
264 return self.db[self.currentcomic]
267 print str(self.currentcomic)
268 if self.currentcomic < 0:
270 self.currentcomic = 0
271 if len(self.db) < (self.currentcomic + 1):
272 self.currentcomic -= 1
273 return self.db[self.currentcomic]['link']
277 def insert_row(self, irow):
278 dbf = open(self.dbfile, 'a')
279 dbw = csv.writer(dbf)
283 def insert_row_first(self, irow):
284 dbf = open(self.dbfile, 'w')
285 dbf.write('comic,id,link,url,filename,title\n')
286 dbw = csv.writer(dbf)
289 dbf = open(self.dbfile, 'a')
290 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
298 # if len(self.db) < 1:
299 # self.currentcomic = -1
300 # self.fetch_latest_std(self.comic, self.start)
301 # elif self.currentcomic == 0 or self.currentcomic < 0:
302 # self.fetch_latest_std(self.comic, self.db[0]['id'])
303 # elif self.currentcomic == (len(self.db) - 1):
304 # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
305 dbf = open(self.dbfile, 'r')
306 dbr = csv.DictReader(dbf)
309 self.db.insert(0,row)
311 if len(self.db) == 0:
312 self.fetch_earlier(self.comic, self.start)
313 dbf = open(self.dbfile, 'r')
314 dbr = csv.DictReader(dbf)
317 self.db.insert(0,row)
322 def fetch_earlier(self, comic, earliest):
323 print "fetch before, " + comic + " earliest"
325 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
326 print "getting get_prev_id..."
327 comicid = self.get_prev_id(comic,earliest)
329 print "already at first comic"
331 print "got " + comicid + " as the one before current..."
332 elif len(str(earliest)) == 10:
334 dt = string.split(earliest, "-")
335 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
336 earlier = d - datetime.timedelta( 1 )
337 comicid = earlier.isoformat()
339 comicid = int(earliest) - 1
341 irow = self.get_irow(comic, comicid)
342 if irow and irow[0] == "skip":
343 print "problem with this one, fetching " + str(irow[2]) + " instead."
344 irow = self.get_irow(comic, irow[2])
346 if irow and len(irow) > 3:
349 print "\ninserting...\n"
350 self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
352 print "No comic found at " + comicid
358 def fetch_newer(self, comic, newest):
359 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
360 comicid = self.get_next_id(comic,newest)
362 print "already at last comic"
364 elif len(newest) == 10:
366 dt = string.split(newest, "-")
367 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
368 newest = d + datetime.timedelta( 1 )
369 comicid = newest.isoformat()
371 comicid = int(newest) + 1
373 irow = self.get_irow(comic, comicid)
374 if irow and irow[0] == "skip":
375 print "problem with this one, fetching " + str(irow[1]) + " instead."
376 irow = self.get_irow(comic, irow[1])
378 if irow and len(irow) > 3:
381 print "\ninserting...\n"
382 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
384 print "No comic found at " + str(comicid)
389 def fetch_latest_std(self, comic, latest):
390 print "fetching new after " + str(comic) + " " + str(latest)
393 if comic == 'cyanide':
394 next = self.get_next_id(comic, latest)
400 if len(str(latest)) == 10:
403 dt = string.split(latest, "-")
404 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
405 newer = d + datetime.timedelta( 1 )
406 comicid = newer.isoformat()
408 comicid = int(latest) + 1
411 lasturl = self.db[0]['url']
415 irow = self.get_irow(comic, comicid)
417 print "got irow: " + str(irow)
418 if str(irow[0]) == 'skip':
419 print "skipping this one..."
422 if irow[3] == lasturl:
423 print "Looping the same, break break break!"
427 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
433 dt = string.split(comicid, "-")
434 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
435 newer = d + datetime.timedelta( 1 )
436 comicid = newer.isoformat()
447 def get_next_id(self, comic, number):
448 if comic == 'babyblues':
449 link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
450 print "link: " + link
452 f = urllib2.urlopen(link)
456 print "got hcode = " + str(hcode) + "\n"
460 print "checking next Babyblues date.."
464 splt = string.split(s, 'nextStripLink', 1)
466 print "no 'prev' found"
469 next = splt[1][48:58]
470 print "got next: " + next
474 if comic == 'cyanide':
475 link = "http://www.explosm.net/comics/" + str(number) + "/"
476 print "link: " + link
478 f = urllib2.urlopen(link)
482 print "got hcode = " + str(hcode) + "\n"
486 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
490 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
492 print "no 'next' found"
494 splt = string.split(splt[1], '/">Next >', 1)
496 print "no 'next' found"
499 print "got next: " + splt[0]
502 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
503 print "link: " + link
505 f = urllib2.urlopen(link)
509 print "got hcode = " + str(hcode) + "\n"
516 splt = string.split(s, '<a href="/default.aspx?id=', 1)
517 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
518 print "no previous found"
519 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
520 print "no next found!"
523 splt = string.split(splt[1], '<a href="/default.aspx?id=', 1)
525 print "no next found! At newest comic!"
527 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
528 print "no next found!"
532 elif comic[-9:] == "comicscom":
533 link = comics[comic]['link'] + str(number) + "/"
534 print "link: " + link
536 f = urllib2.urlopen(link)
540 print "got hcode = " + str(hcode) + "\n"
546 return self.parse_comics_com(comic, s, 'next')
553 def get_prev_id(self, comic, number):
554 if comic == 'babyblues':
555 link = 'http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=' + str(number)
556 print "link: " + link
558 f = urllib2.urlopen(link)
562 print "got hcode = " + str(hcode) + "\n"
566 print "checking prev Babyblues date.."
570 splt = string.split(s, 'prevStripLink', 1)
572 print "no 'prev' found"
576 prev = splt[1][48:58]
577 print "got previous: " + prev
580 if comic == 'cyanide':
581 link = "http://www.explosm.net/comics/" + str(number) + "/"
582 print "link: " + link
584 f = urllib2.urlopen(link)
588 print "got hcode = " + str(hcode) + "\n"
592 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
596 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
598 print "no 'prev' found"
600 splt = string.split(splt[1], '/">< Previous', 1)
601 if len(splt) < 2 or len(splt[0]) > 5:
602 print "no 'next' found"
605 print "got previous: " + splt[0]
607 elif comic == 'wulff':
608 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
609 print "link: " + link
611 f = urllib2.urlopen(link)
615 print "got hcode = " + str(hcode) + "\n"
622 splt = string.split(s, '<a href="/default.aspx?id=', 1)
623 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
626 print "no previous found"
630 elif comic[-9:] == "comicscom":
631 print "fetch prev, still in function..."
632 link = comics[comic]['link'] + str(number) + "/"
633 print "link: " + link
635 f = urllib2.urlopen(link)
639 print "got hcode = " + str(hcode) + "\n"
645 return self.parse_comics_com(comic, s, 'prev')
650 def get_irow(self, comic, number):
652 if len(str(number)) == 10:
657 print "number is now: " + str(number) + "\n"
659 link = "http://xkcd.org/" + str(number) + "/"
660 elif comic == 'sinfest':
661 link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
662 elif comic == 'dilbert':
663 link = "http://dilbert.com/" + str(number) + "/"
665 link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
666 elif comic == 'cyanide':
667 link = "http://www.explosm.net/comics/" + str(number) + "/"
668 elif comic == 'wulff':
669 link = "http://wulffmorgenthaler.com/default.aspx?id=" + str(number)
670 elif comic == 'babyblues':
671 link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
672 elif comic[-9:] == 'comicscom':
673 link = comics[comic]['link'] + str(number) + "/"
677 print "link: " + link
679 f = urllib2.urlopen(link)
683 print "got hcode = " + str(hcode) + "\n"
688 # they look like: comic,id,link,url,filename,title
689 # We already have comic, number/id and link
694 # This should be done with regex but...
695 splt = string.split(s, 'png" title="', 1)
696 splt = string.split(splt[1], '" alt="', 1)
698 splt = string.split(splt[1], "<h3>Image URL (for hotlinking/embedding): ", 1)
699 splt = string.split(splt[1], "</h3>", 1)
703 elif comic == 'sinfest':
706 splt = string.split(s, 'height="107"', 1)
707 splt = string.split(splt[1], '<img src="', 1)
708 splt = string.split(splt[1], '" alt="', 1)
711 print "Fake 404! Break break break!"
713 splt = string.split(splt[1], '" border="0" />', 1)
716 elif comic == 'dilbert':
719 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
720 splt = string.split(splt[1], '" />', 1)
721 url = "http://dilbert.com" + splt[0]
723 print "Fake 404! Break break break!"
731 splt = string.split(s, '<title>PHD Comics: ', 1)
732 splt = string.split(splt[1], '</title>', 1)
734 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
735 splt = string.split(splt[1], ' align=top>', 1)
738 print "Fake 404! Break break break!"
741 elif comic == 'cyanide':
745 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
750 splt = string.split(splt[1], '/">< ', 1)
752 print "got prev: " + prev
753 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
758 splt = string.split(splt[1], '/">Next ></a>', 1)
759 if len(splt[0]) > 10:
763 print "got next: " + next
765 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
769 splt = string.split(splt[1], ' <b>by <a href="', 1)
772 print "title wrong..."
774 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
776 print "a video? Try skipping"
777 return ['skip',next,prev]
779 splt = string.split(splt[1], '">', 1)
780 url = "http://www.explosm" + splt[0]
781 splt2 = string.rsplit(url, "/", 1)
783 irow = [comic,number,link,url,filename,title,next,prev]
787 elif comic == 'babyblues':
788 # babyblues is type .gif
791 splt = string.split(s, 'http://est.rbma.com/content/Baby_Blues?date=', 1)
794 datesplt = string.split(number, "/")
795 flncheck = datesplt[2] + datesplt[0] + datesplt[1]
796 if not flnm == flncheck:
797 print "incorrect filename, end of line. Break break break!"
799 url = 'http://est.rbma.com/content/Baby_Blues?date=' + flnm
800 title = number + " (2 weeks delay)"
801 filename = flnm + ".gif"
802 irow = [comic,number,link,url,filename,title]
806 elif comic == 'wulff':
810 splt = string.split(s, '<img src="pics/dates/month/', 1)
811 if not splt[1][1:2] == ".":
814 title = "0" + splt[1][:1]
815 splt = string.split(splt[1], '<img src="pics/dates/day/', 1)
816 if not splt[1][1:2] == ".":
817 title = title + "-" + splt[1][:2]
819 title = title + "-0" + splt[1][:1]
820 splt = string.split(splt[1], '<img src="pics/dates/year/', 1)
821 title = splt[1][:4] + "-" + title
823 splt = string.split(splt[1], 'id="ctl00_content_Strip1_imgStrip" class="strip" src="striphandler.ashx?stripid=', 1)
824 number = splt[1][:36]
825 url = "http://wulffmorgenthaler.com/striphandler.ashx?stripid=" + number
826 filename = number + ".gif"
827 irow = [comic,number,link,url,filename,title]
829 elif comic[-9:] == 'comicscom':
830 url = self.parse_comics_com(comic, f.read(), 'url')
834 splt2 = string.rsplit(url, "/", 1)
837 if filename == self.db[0]['filename']:
838 print "already exists! Break break break!"
842 irow = [comic,number,link,url,filename,title]
846 def parse_comics_com(self, comicname, source, get_what):
847 # 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/'}"
848 if get_what == "url":
849 splt = string.split(source, 'StripID:', 1)
850 splt = string.split(splt[1], ', ComicID:', 1)
851 url = "http://c0389161.cdn.cloudfiles.rackspacecloud.com/dyn/str_strip/" + splt[0] + ".full.gif"
853 elif get_what == "next":
854 splt = string.split(source, "Link_Next: '", 1)
855 splt = string.split(splt[1], "'", 1)
856 splt = string.split(splt[0], "/")
863 elif get_what == "prev":
864 print "getting comics com prev: " + comicname
865 splt = string.split(source, "Link_Previous: '", 1)
866 splt = string.split(splt[1], "'", 1)
867 splt = string.split(splt[0], "/")
871 if len(splt[2]) == 10:
880 class ComicHomePlugin(hildondesktop.HomePluginItem):
881 # __gtype_name__ = 'ComicHomePlugin'
883 hildondesktop.HomePluginItem.__init__(self)
884 global supports_alpha
885 self.set_settings(True)
886 self.active_comics = self.get_active_comics()
887 self.sizes = self.get_size_settings()
888 self.new_width = self.sizes['width']
889 self.new_height = self.sizes['height']
890 self.connect("show-settings", self.show_options)
891 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
892 self.osso_rpc = osso.Rpc(self.osso_c)
894 if len(self.active_comics) < 1:
895 self.active_comics = ['xkcd']
897 self.db = ComicDb(self.active_comics[self.keypointer])
898 self.comicname = comics[self.active_comics[self.keypointer]]['name']
899 self.set_name = "comicwidget"
900 screen = self.get_screen()
901 colormap = screen.get_rgba_colormap()
903 colormap = screen.get_rgb_colormap()
904 supports_alpha = False
906 supports_alpha = True
907 self.set_colormap(colormap)
908 self.set_app_paintable(True)
910 self.set_size_request(self.sizes['width'], self.sizes['height'])
912 self.vbox = gtk.VBox()
913 self.hbox = gtk.HBox()
914 self.label = gtk.Label()
915 self.label.set_alignment(xalign = 0, yalign = 0.5)
916 self.label.set_use_markup(True)
917 self.label.set_markup(self.get_markup())
920 self.e_goweb = gtk.EventBox()
921 self.e_goweb.set_name('goweb')
922 self.e_goweb.set_size_request(self.sizes['width'] - 250, 48)
923 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
924 # it will change to be a cairo thing when I have the time.
925 self.e_open = gtk.EventBox()
926 self.e_open.set_name('open')
927 self.e_switch = gtk.EventBox()
928 self.e_switch.set_name('switch')
929 self.e_switch.set_size_request(48, 48)
930 self.e_next = gtk.EventBox()
931 self.e_next.set_name('next')
932 self.e_next.set_size_request(48, 48)
933 self.e_prev = gtk.EventBox()
934 self.e_prev.set_name('prev')
935 self.e_prev.set_size_request(48, 48)
936 self.e_up = gtk.EventBox()
937 self.e_up.set_name('up')
938 self.e_up.set_size_request(48, 48)
939 self.e_down = gtk.EventBox()
940 self.e_down.set_name('down')
941 self.e_down.set_size_request(48, 48)
942 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
943 self.comic_image.show()
946 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
948 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
949 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
950 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
951 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
952 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
953 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
954 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
955 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
956 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
957 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
962 self.e_goweb.add(self.label)
963 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
964 self.e_goweb.connect("button-release-event", self.view_comic)
966 self.e_open.add(self.comic_image)
968 # self.e_switch.add(self.switch)
969 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
970 self.e_switch.connect("button-press-event", self.button_press)
971 self.e_switch.connect("button-release-event", self.button_release)
972 self.e_switch.connect("leave-notify-event", self.button_release)
975 # self.e_next.add(self.next)
976 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
977 self.e_next.connect("button-press-event", self.button_press)
978 self.e_next.connect("button-release-event", self.button_release)
979 self.e_next.connect("leave-notify-event", self.button_release)
981 #self.e_prev.add(self.prev)
982 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
983 self.e_prev.connect("button-press-event", self.button_press)
984 self.e_prev.connect("button-release-event", self.button_release)
985 self.e_prev.connect("leave-notify-event", self.button_release)
987 #self.e_up.add(self.up)
988 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
989 self.e_up.connect("button-press-event", self.button_press)
990 self.e_up.connect("button-release-event", self.button_release)
991 self.e_up.connect("leave-notify-event", self.button_release)
994 #self.e_down.add(self.down)
995 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
996 self.e_down.connect("button-press-event", self.button_press)
997 self.e_down.connect("button-release-event", self.button_release)
998 self.e_down.connect("leave-notify-event", self.button_release)
1000 self.vbox.pack_start(self.e_open,False,False,0)
1001 self.vbox.pack_end(self.hbox,False,False,0)
1003 self.hbox.pack_start(self.e_switch,False,False,0)
1004 self.hbox.pack_start(self.e_goweb,False,False,5)
1005 self.hbox.pack_end(self.e_next,False,False,0)
1006 self.hbox.pack_end(self.e_prev,False,False,0)
1007 self.hbox.pack_end(self.e_down,False,False,0)
1008 self.hbox.pack_end(self.e_up,False,False,0)
1009 self.vbox.show_all()
1014 # **************** Cairo draw functions ***************
1016 def realize(self, widget):
1017 global supports_alpha
1019 # To check if the display supports alpha channels, get the colormap
1020 screen = self.get_screen()
1021 colormap = screen.get_rgba_colormap()
1022 if colormap == None:
1023 print 'Your screen does not support alpha channels!'
1024 colormap = screen.get_rgb_colormap()
1025 supports_alpha = False
1027 # print 'Your screen supports alpha channels!'
1028 supports_alpha = True
1030 # Now we have a colormap appropriate for the screen, use it
1031 self.set_colormap(colormap)
1035 def do_expose_event(widget, event):
1036 cr = widget.window.cairo_create()
1037 widget.draw(widget.e_up, 0)
1038 widget.draw(widget.e_down, 0)
1039 widget.draw(widget.e_prev,0)
1040 widget.draw(widget.e_next, 0)
1041 widget.draw(widget.e_switch, 0)
1042 # widget.writeit(widget.e_goweb, "asdf")
1043 cr.set_operator(cairo.OPERATOR_OVER)
1044 region = gtk.gdk.region_rectangle(event.area)
1046 bg_color=gtk.gdk.color_parse('#000000')
1048 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
1050 cr.set_source_rgb (0.0, 0.0, 0.0)
1055 # Function that draws a button preloaded in the images dictionary.
1056 def draw(self, target, postfix):
1057 cr_e = target.window.cairo_create()
1058 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
1059 cr_e.set_operator(cairo.OPERATOR_SOURCE)
1061 cr_e.set_operator(cairo.OPERATOR_OVER)
1062 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
1065 # ********** Event handlers *************
1068 # Most events happen on release, so I moved all of them into the same event handler.
1069 def button_release(self, widget, event):
1070 if event.type == gtk.gdk.BUTTON_RELEASE:
1071 func = widget.get_name()
1072 print "pressed: " + func
1074 self.e_open.remove(self.comic_image)
1075 self.imgvpos = self.imgvpos - ((self.sizes['height'] - 48) / 2.1)
1076 if self.imgvpos < 0:
1078 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1079 self.e_open.add(self.comic_image)
1080 self.e_open.show_all()
1082 elif func == 'down':
1083 self.e_open.remove(self.comic_image)
1084 self.imgvpos = self.imgvpos + ((self.sizes['height'] - 48) / 2.1)
1085 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1086 self.e_open.add(self.comic_image)
1087 self.e_open.show_all()
1089 elif func == 'next':
1090 self.db.currentcomic -= 1
1092 self.e_open.remove(self.comic_image)
1093 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1094 self.e_open.add(self.comic_image)
1095 self.e_open.show_all()
1096 self.e_goweb.remove(self.label)
1097 titl = str(self.db.db[self.db.currentcomic]['title'])
1099 titl = titl[:19] + "..."
1100 self.label.set_markup(self.get_markup())
1101 self.e_goweb.add(self.label)
1102 self.e_goweb.show_all()
1104 elif func == 'prev':
1105 self.db.currentcomic += 1
1107 self.e_open.remove(self.comic_image)
1108 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1109 self.e_open.add(self.comic_image)
1110 self.e_open.show_all()
1111 self.e_goweb.remove(self.label)
1112 titl = str(self.db.db[self.db.currentcomic]['title'])
1114 titl = titl[:19] + "..."
1115 self.label.set_markup(self.get_markup())
1116 self.e_goweb.add(self.label)
1117 self.e_goweb.show_all()
1119 elif func == 'switch':
1120 self.active_comics = self.get_active_comics()
1123 #sys.stdout = sys.__stdout__
1125 #sys.stderr = sys.__stderr__
1126 print "active comics: " + str(self.active_comics)
1127 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
1130 print "switching to " + self.active_comics[self.keypointer]
1131 self.comicname = comics[self.active_comics[self.keypointer]]['name']
1132 self.db = ComicDb(self.active_comics[self.keypointer])
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_goweb.remove(self.label)
1136 titl = str(self.db.db[self.db.currentcomic]['title'])
1138 titl = titl[:19] + "..."
1139 self.label.set_markup(self.get_markup())
1140 self.e_goweb.add(self.label)
1141 self.e_goweb.show_all()
1142 self.e_open.add(self.comic_image)
1143 self.e_open.show_all()
1147 self.draw(widget, "0")
1149 # this one only switches to pressed button image
1150 def button_press(self, widget, event):
1151 if not event.type == gtk.gdk.BUTTON_PRESS:
1153 self.draw(widget, "1")
1155 # this one cancels a button press when pointer is moved out from it after press.
1156 def button_out(self, widget, event):
1157 if not event.type == gtk.gdk.BUTTON_PRESS:
1159 self.draw(widget, "0")
1161 # no fancy cairo stuff here. simply launches url in browser.
1162 def view_comic(self, widget, event):
1163 if not event.type == gtk.gdk.BUTTON_RELEASE:
1165 link = self.db.get_link()
1166 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
1170 # this function modifies the image to fit the widget and then returns it.
1172 def get_resized_pixmap(self, filename, vpos):
1173 print "vpos: " + str(vpos)
1175 width = int(self.sizes['width'])
1176 height = int(self.sizes['height'])
1177 picwidth = width - 2
1178 maxheight = height - 48
1180 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
1181 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
1182 parent_buf.fill(0xffffffff)
1183 retimg = gtk.Image()
1184 # get correct width/height
1186 new_width = picwidth
1187 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
1188 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
1189 print "scaled height: " + str(scaled_buf.get_height())
1190 if scaled_buf.get_height() > maxheight:
1191 if scaled_buf.get_height() - (vpos + maxheight) < 0:
1192 vpos = scaled_buf.get_height() - maxheight
1193 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
1194 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
1197 tmpy = maxheight - scaled_buf.get_height()
1201 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
1203 # test = parent_buf.render_pixmap_and_mask(0)
1204 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
1205 retimg.set_from_pixbuf(parent_buf)
1206 print retimg.get_pixel_size()
1207 print "vpos after fixing image: " + str(self.imgvpos)
1210 def screen_changed(self, widget, old_screen=None):
1211 global supports_alpha
1213 # To check if the display supports alpha channels, get the colormap
1214 screen = self.get_screen()
1215 colormap = screen.get_rgba_colormap()
1216 if colormap == None:
1217 print 'Your screen does not support alpha channels!'
1218 colormap = screen.get_rgb_colormap()
1219 supports_alpha = False
1221 # print 'Your screen supports alpha channels!'
1222 supports_alpha = True
1224 # Now we have a colormap appropriate for the screen, use it
1225 self.set_colormap(colormap)
1230 def get_active_comics(self):
1233 if os.path.isfile(activecomics) == True:
1234 print "found " + activecomics
1235 dbf = open(activecomics, 'r')
1236 dbr = csv.reader(dbf)
1245 print "file " + activecomics + " not found"
1246 if not os.path.exists(dbdir):
1247 print "path not found: " + dbdir
1251 print "settings dir creation failed. Exiting."
1254 # dbf = file(activecomics, 'w')
1256 dbf = open(activecomics, 'w')
1258 print "error storing settings"
1260 dbw = csv.writer(dbf)
1261 dbw.writerow(defaultcomics)
1267 def get_markup(self):
1268 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1269 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>'
1271 return '<span size="12000" face="arial"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1274 #check if settings file exists
1276 #if not, write defaults
1278 # ************************* OPTION DIALOGS ********************************
1280 def show_options(self, widget):
1281 print "loading options dialog"
1282 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1284 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1285 about_button.set_text("About", "See Author, Copyright and License information")
1286 about_button.set_alignment(0,0,0,0)
1287 about_button.connect("clicked", self.show_about)
1289 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1290 comics_button.set_text("Comics", "Choose which comics to view")
1291 comics_button.set_alignment(0,0,0,0)
1292 comics_button.connect("clicked", self.show_comics)
1294 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1295 # size_button.set_text("Change size", "Change the size of the widget")
1296 # size_button.set_alignment(0,0,0,0)
1297 # size_button.connect("clicked", self.show_size)
1299 print "adding buttons to dialog, starting with comics"
1300 dialog.vbox.pack_start(comics_button, True, True, 0)
1301 print "adding about button"
1302 dialog.vbox.pack_start(about_button, True, True, 0)
1303 # dialog.vbox.pack_start(size_button, True, True, 0)
1309 def show_about(self, widget):
1310 print "in about dialog"
1311 dialog = gtk.AboutDialog()
1312 dialog.set_title("About")
1313 dialog.set_name("Comic widget")
1314 dialog.set_version(APP_VERSION)
1315 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1316 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."])
1317 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1318 dialog.set_comments("Silly rabbit, tricks are for kids.")
1319 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/>.""")
1320 dialog.set_wrap_license(True)
1325 def show_comics(self, widget, data = None):
1326 print "in comics dialog! yay!"
1327 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1328 comiclist = comics.keys()
1332 self.liststore = gtk.ListStore(str,str,bool)
1333 for comicid in comiclist:
1335 for acomic in self.active_comics:
1336 if acomic == comicid:
1340 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1342 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1343 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1344 self.treeview.set_model(self.liststore)
1345 self.treeview.set_reorderable(True)
1346 tvcolumn = gtk.TreeViewColumn('Column 0')
1347 self.treeview.append_column(tvcolumn)
1348 cell = gtk.CellRendererText()
1349 tvcolumn.pack_start(cell, True)
1350 tvcolumn.add_attribute(cell, 'text', 0)
1352 ts = hildon.PannableArea()
1354 ts.set_size_request(-1, 280)
1355 ts.add(self.treeview)
1356 selection = self.treeview.get_selection()
1358 dialog.vbox.pack_start(ts,True,True,0)
1360 selection.connect("changed", self.pick_comic, comiclist)
1364 def pick_comic(self, widget, data):
1365 selected = widget.get_selected()
1366 if selected == None:
1369 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1370 if selected[0].get_value(selected[1],2):
1371 self.remove_comic(selected[0].get_value(selected[1],1))
1372 splt = selected[0].get_value(selected[1],0)[1:]
1373 selected[0].set_value(selected[1],0," " + splt)
1374 selected[0].set_value(selected[1],2,False)
1376 self.add_comic(selected[0].get_value(selected[1],1))
1377 splt = selected[0].get_value(selected[1],0)[1:]
1378 selected[0].set_value(selected[1],0,"*" + splt)
1379 selected[0].set_value(selected[1],2,True)
1382 def add_comic(self, comicid):
1383 if os.path.isfile(activecomics) == True:
1384 print "added " + comicid + " to " + str(self.active_comics)
1386 found = self.active_comics.index(comicid)
1388 self.active_comics.append(comicid)
1389 dbf = open(activecomics, 'w')
1390 dbw = csv.writer(dbf)
1391 dbw.writerow(self.active_comics)
1394 def remove_comic(self, comicid):
1395 if os.path.isfile(activecomics) == True:
1396 print "removing " + comicid + " from " + str(self.active_comics)
1398 del self.active_comics[self.active_comics.index(comicid)]
1401 dbf = open(activecomics, 'w')
1402 dbw = csv.writer(dbf)
1403 dbw.writerow(self.active_comics)
1407 def get_size_settings(self):
1408 # defaults = {'width':480,'height':230}
1409 # options: w 798 700 600 500 480 400
1410 # options: h 420 360 310 270 240 200
1411 print "checking to see if config file is available"
1412 if os.path.isfile(sizefile) == True:
1413 print "found " + sizefile
1414 dbf = open(sizefile, 'r')
1415 dbr = csv.reader(dbf)
1420 print "empty config file"
1424 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1425 if sizes['width'] > 798:
1426 sizes['width'] = 798
1427 if sizes['width'] < 400:
1428 sizes['width'] = 400
1429 if sizes['height'] > 420:
1430 sizes['height'] = 420
1431 if sizes['height'] < 200:
1432 sizes['height'] = 200
1439 print "no config file"
1442 hd_plugin_type = ComicHomePlugin
1445 if __name__ == "__main__":
1447 gobject.type_register(hd_plugin_type)
1448 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")