IconManager.py :  » RSS » PenguinTV » PenguinTV-4.1.0 » penguintv » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » RSS » PenguinTV 
PenguinTV » PenguinTV 4.1.0 » penguintv » IconManager.py
import os, os.path
import glob
import urllib
import logging

ICONSIZE = 48, 48

try:
  import Image
  HAS_PIL = True
except:
  HAS_PIL = False

class IconManager:

  """A small class that handles favicons for feeds"""

  def __init__(self, home):
    self._home = home
    try:
      os.stat(os.path.join(self._home, 'icons'))
    except:
      os.mkdir(os.path.join(self._home, 'icons'))

  def icon_exists(self, feed_id):
    filename = os.path.join(self._home, 'icons', str(feed_id) + '.*')
    result = glob.glob(filename)
    result = [r for r in result if r[-4:].upper() != "NONE"]
    return result > 0  
    
  def get_icon(self, feed_id):
    filename = os.path.join(self._home, 'icons', str(feed_id) + '.*')
    result = glob.glob(filename)
    if len(result) == 0:
      return None
    return result[0]
  
  def get_icon_pixbuf(self, feed_id, max_width=None, max_height=None, min_width=None, min_height=None):
    import gtk
    if min_width is None:
      min_width = 8
    if min_height is None:
      min_height = 8
    filename = self.get_icon(feed_id)
    if filename is None:
      p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,True,8, min_width, min_height)
      p.fill(0xffffff00)
      return p
  
    try:
      p = gtk.gdk.pixbuf_new_from_file(filename)
    except:
      p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,True,8, min_width, min_height)
      p.fill(0xffffff00)
      return p
    height = p.get_height()
    width = p.get_width()
    if max_height is not None:
      if height > max_height:
        height = max_height
        width = p.get_width() * height / p.get_height()
    if max_width is not None:
      if width > max_width:
        width = max_width
        height = p.get_height() * width / p.get_width()
    if min_height is not None:
      if height < min_height:
        height = min_height
        width = p.get_width() * height / p.get_height()
    
    if min_width is not None:
      if width < min_width:
        width = min_width
        height = p.get_height() * width / p.get_width()
    
    if height != p.get_height() or width != p.get_width():
      del p
      p = gtk.gdk.pixbuf_new_from_file_at_size(filename, width, height)
    return p
    
  def download_icon(self, feed_id, feedparser_data):
    url_list = []
    try: url_list.append(feedparser_data['feed']['image']['href'])
    except: pass
    
    try: url_list.append(feedparser_data['feed']['link'] + '/favicon.ico')
    except: pass
    
    found=False
    for url in url_list:
      try:
        filename = os.path.join(self._home, 'icons', str(feed_id) + '.' + url.split('.')[-1])
        urllib.urlretrieve(url, filename)
        found = True
        break
      except:
        pass
  
    if found:
      if HAS_PIL:
        try:
          im = Image.open(filename)
          if im.size[0] > ICONSIZE[0] or im.size[1] > ICONSIZE[1]:
            im.thumbnail(ICONSIZE, Image.ANTIALIAS)
            im.save(filename+".thumb", "PNG")
            os.remove(filename)
            newname = ".".join(filename.split('.')[:-1]) + ".png"
            os.rename(filename+".thumb", newname)
        except Exception, e:
          logging.warning("Feed %i: Couldn't resize feed icon: %s" % (feed_id, str(e)))
      else:
        logging.warning("Don't have Python Imaging, can't resize icons")
      return url
      
    f = open(os.path.join(self._home, 'icons', str(feed_id)+'.none'), 'w')
    f.write("")
    f.close()
    return None
      
  def remove_icon(self, feed_id):
    filename = os.path.join(self._home, 'icons', str(feed_id) + '.*')
    result = glob.glob(filename)
    for r in result:
      print "deleting icon:",r
      os.remove(r)
      
  def is_icon_up_to_date(self, feed_id, old_href, feedparser_data):
    url_list = []
    try: url_list.append(feedparser_data['feed']['image']['href'])
    except: pass
    
    try: url_list.append(feedparser_data['feed']['link'] + '/favicon.ico')
    except: pass
    
    if len(url_list) > 0:
      if old_href in url_list:
        filename = os.path.join(self._home, 'icons', str(feed_id) + '.*')
        result = glob.glob(filename)
        if len(result) == 0: #whoops, there's no file there anymore
          return False
        return True
    return False
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.