Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
class TTV:
    
    w    =   8; h    = 14
    cols =  39; rows = 24
    sub  =  1
    subs =   1
    bg_alpha = 200
    
    header = 'NRK Tekst tv laster..'
    
    
    def __init__(self, cache=False, page=100):
        self.conn = connection_manager.DataHandle()
        self.savepath = os.path.join(os.getcwd(), 'ttv_images')
        self.status = 900
        self.cache = cache
        self.page = page
        
        
    def get_url(self):
        tpl = ( 'http://www2.nrk.no/teksttv/'
                'index.asp?channel=1&page=%d&subpage=%d' )
                
        url = tpl % ( self.page, self.sub )
        if DEBUG: 
            Log.out('nrk ttv url: %s' % url)
        return url
        
        
    def get_savefile(self):
        return os.path.join(
                    self.savepath, 
                    'ttv_p%dsp_%d.png' % ( self.page, self.sub )
                )
        
        
    def page_up(self):
        return self.get_page(self.page + 1)
        
        
    def page_down(self):
        return self.get_page(self.page - 1)
        
        
    def next_page(self):
        return self.get_subpage(self.sub + 1)
    
    
    def has_next(self):
        if self.sub < self.subs: 
            return True
        
        
    def prev_page(self):
        return self.get_subpage(self.sub - 1)
    
    
    def has_prev(self):
        if self.sub > 1: 
            return True
        
        
    def get_subpage(self, subpage):
        return self.get_page(self.page, subpage)
        
        
    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()
        
        
    def parse(self, data):
        self.parser = TTVParser()
        self.parser.parse(data)
   
   
    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 paintHexFonts(self, y, fonts, offset=2):
        
        r = self.rows - offset
        
        #Set characters width and height and set x/y-position
        w = self.w
        h = self.h
        y = y * h + h
        x = 0
           
        for f in fonts:
            
            #Unpack fonts values
            fnt, fgc, bgc, chr = f
            fgc, bgc = pal[fgc], pal[bgc]
            
            #Don't plot background if its 
            #"transparent" (black) to increase speed
            if bgc == (0, 0, 0): 
                bgc = None
            
            #Set local loop references and plot background
            lx = x; ly = y
            if bgc:
                for k in range( w * h ):
                    if k % w == 0:
                        ly -= 1
                        lx  = x
                    self.img.plot(lx, ly, bgc)
                    lx -= 1
            
            #Reset local loop references and plot text
            lx = x
            ly = y
            for j in range( len(fnt) ):
                if j % w == 0:
                    ly -= 1
                    lx  = x
                if int(fnt[j]):
                    self.img.plot( lx, ly, fgc )
                lx -= 1
            
            #Set x-axis reference for next character
            x += w
            

        
    def save(self):   
        import png
        
        w = self.img.wd 
        h = self.img.ht
        bg = image.BLACK 
        bd = 8
        file = self.get_savefile()
        
        if self.status > 900:
            return
            
        if os.path.isdir(os.path.dirname(file)) == False:
            os.makedirs(os.path.dirname(file))
        
        print 'Save image to file %s' % file
        
        fh  = open(file, 'wb')
        img = png.Writer(w, h, alpha=True, bitdepth=bd, background=bg)
        img.write_array(fh, self.img.array)
        fh.close()
        
        del self.img
        
        return file
Exemplo n.º 5
0
class TTV:

    w = 8
    h = 14
    cols = 39
    rows = 24
    sub = 1
    subs = 1
    bg_alpha = 200

    header = 'NRK Tekst tv laster..'

    def __init__(self, cache=False, page=100):
        self.conn = connection_manager.DataHandle()
        self.savepath = os.path.join(os.getcwd(), 'ttv_images')
        self.status = 900
        self.cache = cache
        self.page = page

    def get_url(self):
        tpl = ('http://www2.nrk.no/teksttv/'
               'index.asp?channel=1&page=%d&subpage=%d')

        url = tpl % (self.page, self.sub)
        if DEBUG:
            Log.out('nrk ttv url: %s' % url)
        return url

    def get_savefile(self):
        return os.path.join(self.savepath,
                            'ttv_p%dsp_%d.png' % (self.page, self.sub))

    def page_up(self):
        return self.get_page(self.page + 1)

    def page_down(self):
        return self.get_page(self.page - 1)

    def next_page(self):
        return self.get_subpage(self.sub + 1)

    def has_next(self):
        if self.sub < self.subs:
            return True

    def prev_page(self):
        return self.get_subpage(self.sub - 1)

    def has_prev(self):
        if self.sub > 1:
            return True

    def get_subpage(self, subpage):
        return self.get_page(self.page, subpage)

    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()

    def parse(self, data):
        self.parser = TTVParser()
        self.parser.parse(data)

    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 paintHexFonts(self, y, fonts, offset=2):

        r = self.rows - offset

        #Set characters width and height and set x/y-position
        w = self.w
        h = self.h
        y = y * h + h
        x = 0

        for f in fonts:

            #Unpack fonts values
            fnt, fgc, bgc, chr = f
            fgc, bgc = pal[fgc], pal[bgc]

            #Don't plot background if its
            #"transparent" (black) to increase speed
            if bgc == (0, 0, 0):
                bgc = None

            #Set local loop references and plot background
            lx = x
            ly = y
            if bgc:
                for k in range(w * h):
                    if k % w == 0:
                        ly -= 1
                        lx = x
                    self.img.plot(lx, ly, bgc)
                    lx -= 1

            #Reset local loop references and plot text
            lx = x
            ly = y
            for j in range(len(fnt)):
                if j % w == 0:
                    ly -= 1
                    lx = x
                if int(fnt[j]):
                    self.img.plot(lx, ly, fgc)
                lx -= 1

            #Set x-axis reference for next character
            x += w

    def save(self):
        import png

        w = self.img.wd
        h = self.img.ht
        bg = image.BLACK
        bd = 8
        file = self.get_savefile()

        if self.status > 900:
            return

        if os.path.isdir(os.path.dirname(file)) == False:
            os.makedirs(os.path.dirname(file))

        print 'Save image to file %s' % file

        fh = open(file, 'wb')
        img = png.Writer(w, h, alpha=True, bitdepth=bd, background=bg)
        img.write_array(fh, self.img.array)
        fh.close()

        del self.img

        return file