home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / standardmedia_ke.recipe < prev    next >
Text File  |  2011-09-09  |  6KB  |  150 lines

  1. __license__   = 'GPL v3'
  2. __copyright__ = '2010, Hans Donner <hans.donner at pobox.com>'
  3. '''
  4. www.standardmedia.co.ke
  5. '''
  6.  
  7. import os
  8. from calibre import strftime, __appname__, __version__
  9.  
  10. from calibre.web.feeds.news import BasicNewsRecipe
  11. from calibre.constants import preferred_encoding
  12. from calibre.utils.magick import Image
  13.  
  14. class StandardMediaKeRecipe(BasicNewsRecipe):
  15.  
  16.     __author__ = 'Hans Donner'
  17.  
  18.     title = u'The Standard'
  19.     description = 'News from Kenia'
  20.     language = 'en'
  21.     country = 'KE'
  22.     publication_type = 'newspaper'
  23.     publisher = 'standardmedia.co.ke'
  24.     category = 'news, politics, Kenia'
  25.  
  26.     cover_img_url = 'http://www.standardmedia.co.ke/images/easLogoOther.gif'
  27.     masthead_url = cover_img_url
  28.  
  29.     max_articles_per_feed = 200
  30.     oldest_article = 3
  31.  
  32.     use_embedded_content = False
  33.     remove_empty_feeds = True
  34.  
  35.     no_stylesheets = False
  36.  
  37.     feeds = [(u'Headlines', u'http://www.standardmedia.co.ke/rss/headlines.php'),
  38.              (u'Business', u'http://www.standardmedia.co.ke/rss/business.php'),
  39.              (u'Politics', u'http://www.standardmedia.co.ke/rss/politics.php'),
  40.              (u'Editorial', u'http://www.standardmedia.co.ke/rss/editorial.php'),
  41.              (u'Columnists', u'http://www.standardmedia.co.ke/rss/columnists.php'),
  42.              (u'Sports', u'http://www.standardmedia.co.ke/rss/sports.php'),
  43.              (u'Entertainment', u'http://www.standardmedia.co.ke/rss/entertain.php')]
  44.  
  45.     conversion_options = {
  46.                           'comment'   : description
  47.                         , 'tags'      : category
  48.                         , 'publisher' : publisher
  49.                         , 'language'  : language
  50.                         }
  51.  
  52.  
  53.     def print_version(self, url):
  54.         import re
  55.         p = re.compile('http://www.standardmedia.co.ke/.*InsidePage.php')
  56.         return p.sub('http://www.standardmedia.co.ke/print.php', url)
  57.  
  58.     def preprocess_html(self, soup):
  59.         return self.adeify_images(soup)
  60.  
  61.     def get_cover_img_url(self):
  62.         return getattr(self, 'cover_img_url', None)
  63.  
  64.     def _download_cover_img(self):
  65.         # hack to reuse download_cover
  66.         old_cu = None
  67.         try:
  68.             old_cu = self.get_cover_ur()
  69.         except:
  70.             pass
  71.         new_cu = self.get_cover_img_url()
  72.         self.cover_url = new_cu
  73.         self._download_cover()
  74.  
  75.         outfile = os.path.join(self.output_dir, 'cover_img.jpg')
  76.         self.prepare_masthead_image(self.cover_path, outfile)
  77.  
  78.         self.cover_url = old_cu
  79.         self.cover_img_path = outfile
  80.  
  81.     def download_cover_img(self):
  82.         try:
  83.             self._download_cover_img()
  84.             self.report_progress(1, _('Downloaded cover to %s') % self.cover_img_path)
  85.         except:
  86.             self.log.exception('Failed to download cover img')
  87.             self.cover_img_path = None
  88.  
  89.     def prepare_cover_image(self, path_to_image, out_path):
  90.         img = Image()
  91.         img.open(path_to_image)
  92.         img.save(out_path)
  93.  
  94.     def default_cover(self, cover_file):
  95.         '''
  96.         Create a generic cover for recipes that have a special cover img
  97.         '''
  98.         try:
  99.             try:
  100.                 from PIL import Image, ImageDraw, ImageFont
  101.                 Image, ImageDraw, ImageFont
  102.             except ImportError:
  103.                 import Image, ImageDraw, ImageFont
  104.             font_path = P('fonts/liberation/LiberationSerif-Bold.ttf')
  105.             title = self.title if isinstance(self.title, unicode) else \
  106.                     self.title.decode(preferred_encoding, 'replace')
  107.             date = strftime(self.timefmt)
  108.             app = '['+__appname__ +' '+__version__+']'
  109.  
  110.             COVER_WIDTH, COVER_HEIGHT = 590, 750
  111.             img = Image.new('RGB', (COVER_WIDTH, COVER_HEIGHT), 'white')
  112.             draw = ImageDraw.Draw(img)
  113.             # Title
  114.             font = ImageFont.truetype(font_path, 44)
  115.             width, height = draw.textsize(title, font=font)
  116.             left = max(int((COVER_WIDTH - width)/2.), 0)
  117.             top = 15
  118.             draw.text((left, top), title, fill=(0,0,0), font=font)
  119.             bottom = top + height
  120.             # Date
  121.             font = ImageFont.truetype(font_path, 32)
  122.             width, height = draw.textsize(date, font=font)
  123.             left = max(int((COVER_WIDTH - width)/2.), 0)
  124.             draw.text((left, bottom+15), date, fill=(0,0,0), font=font)
  125.             # Vanity
  126.             font = ImageFont.truetype(font_path, 28)
  127.             width, height = draw.textsize(app, font=font)
  128.             left = max(int((COVER_WIDTH - width)/2.), 0)
  129.             top = COVER_HEIGHT - height - 15
  130.             draw.text((left, top), app, fill=(0,0,0), font=font)
  131.  
  132.             # Logo
  133.             logo_file = I('library.png')
  134.             self.download_cover_img()
  135.             if getattr(self, 'cover_img_path', None) is not None:
  136.                 logo_file = self.cover_img_path
  137.             self.report_progress(1, _('using cover img from %s') % logo_file)
  138.             logo = Image.open(logo_file, 'r')
  139.             width, height = logo.size
  140.             left = max(int((COVER_WIDTH - width)/2.), 0)
  141.             top = max(int((COVER_HEIGHT - height)/2.), 0)
  142.             img.paste(logo, (left, top))
  143.             img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE)
  144.             img.convert('RGB').save(cover_file, 'JPEG')
  145.             cover_file.flush()
  146.         except Exception, e:
  147.             self.log.exception('Failed to generate default cover ', e)
  148.             return False
  149.         return True
  150.