def create_background(self): file = os.path.join(self.savepath, 'bg_%d.png' % (self.bg_alpha)) print 'background file: %s' % file if connection_manager.Cache.has_cache(file): print 'use cached background' return file w = self.w * self.cols h = self.h * self.rows r, g, b, NA = pal[0] a = self.bg_alpha rgba = (r, g, b, a) img = ImageRGB(w, h, True, rgba) import png bg = image.BLACK bd = 8 if os.path.isdir(os.path.dirname(file)) == False: os.makedirs(os.path.dirname(file)) fh = open(file, 'wb') save_img = png.Writer(w, h, alpha=True, bitdepth=bd, background=bg) save_img.write_array(fh, img.array) fh.close() del img return file
def get_page(self, page=100, sub=1): if (page < 100 or page > 999) or (sub < 1 or sub > 99): self.status = 901 return self.status = 900 self.page = page self.sub = sub savefile = self.get_savefile() if connection_manager.Cache.has_cache(savefile, 10) and self.cache: return savefile url = self.get_url() data = self.conn.get_data(url) self.parse(data) if len(self.parser.table) == 0: self.status = 904 return ch = self.channel = self.parser.metadata['channel'] or 'NRK1' sps = self.subs = self.parser.metadata['subpages'] or 1 sp = self.sub = self.parser.metadata['subpage'] or 1 dt = self.date = self.parser.metadata['date'] pg = self.page = self.parser.metadata['page'] subp = '(%02d/%02d' % (sp, sps) if self.parser.metadata['substat'] == 'more': subp += '+)' else: subp += ') ' cnt = self.cols - (len(ch) + len(dt) + 5) - (len(subp) + 4) head = ' %s %s%s%s %s ' % (ch, dt, ' ' * cnt, str(pg), subp) cell = Cell() cell.fcolor = 0 cell.bcolor = 7 cell.add(head) w = self.w * self.cols h = self.h * self.rows self.img = ImageRGB(w, h, True, pal[0]) self.paintHexFonts(0, cell.read(), 1) for row in self.parser.table: self.paintHexFonts(row.id + 1, row.read()) return self.save()