home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / nation_ke.recipe < prev    next >
Text File  |  2011-09-09  |  6KB  |  157 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. from calibre.web.feeds.news import BasicNewsRecipe
  10. from calibre.constants import preferred_encoding
  11. from calibre.utils.magick import Image
  12.  
  13. class NationKeRecipe(BasicNewsRecipe):
  14.  
  15.     __author__ = 'Hans Donner'
  16.  
  17.     title = u'Sunday Nation'
  18.     description = 'News from Kenia'
  19.     language = 'en'
  20.     country = 'KE'
  21.     publication_type = 'newspaper'
  22.     publisher = 'nation.co.ke'
  23.     category = 'news, politics, Kenia'
  24.  
  25.     cover_img_url = 'http://www.nation.co.ke/image/view/-/465228/medRes/33884/-/maxh/85/-/12e8pptz/-/Sunday_Logo.gif'
  26.     masthead_url = cover_img_url
  27.  
  28.     max_articles_per_feed = 200
  29.     oldest_article = 2
  30.  
  31.     use_embedded_content = False
  32.     remove_empty_feeds = True
  33.  
  34.     no_stylesheets = True
  35.     extra_css = ' body{font-family: Verdana,Arial,Helvetica,sans-serif; font-size:0.7em } ' + \
  36.                 ' .image{margin-bottom: 1em} '
  37.  
  38.     keep_only_tags = dict(id='articlebody')
  39.  
  40.     feeds = [(u'News', u'http://www.nation.co.ke/News/-/1056/1056/-/view/asFeed/-/14nfs48z/-/index.xml'),
  41.              (u'Business', u'http://www.nation.co.ke/business/-/996/996/-/view/asFeed/-/14lpkvc/-/index.xml'),
  42.              (u'InDepth', u'http://www.nation.co.ke/InDepth/-/452898/452898/-/view/asFeed/-/14ndbk6/-/index.xml'),
  43.              (u'Sports', u'http://www.nation.co.ke/sports/-/1090/1090/-/view/asFeed/-/hlukmj/-/index.xml'),
  44.              (u'Magazines', u'http://www.nation.co.ke/magazines/-/1190/1190/-/view/asFeed/-/fcxm6jz/-/index.xml'),
  45.              (u'Op/Ed', u'http://www.nation.co.ke/oped/-/1192/1192/-/view/asFeed/-/unsp8mz/-/index.xml'),
  46.              (u'Blogs', u'http://www.nation.co.ke/blogs/-/620/620/-/view/asFeed/-/28ia05z/-/index.xml')]
  47.  
  48.     conversion_options = {
  49.                           'comment'   : description
  50.                         , 'tags'      : category
  51.                         , 'publisher' : publisher
  52.                         , 'language'  : language
  53.                         }
  54.  
  55.  
  56.     def print_version(self, url):
  57.         from calibre.ebooks.BeautifulSoup import BeautifulSoup
  58.         soup = BeautifulSoup(self.browser.open(url).read())
  59.         printversion = soup.find('a', text='Print')
  60.         if printversion is None:
  61.             return url
  62.         else:
  63.             return 'http://www.nation.co.ke' + printversion.parent['href']
  64.  
  65.     def preprocess_html(self, soup):
  66.         return self.adeify_images(soup)
  67.  
  68.     def get_cover_img_url(self):
  69.         return getattr(self, 'cover_img_url', None)
  70.  
  71.     def _download_cover_img(self):
  72.         # hack to reuse download_cover
  73.         old_cu = None
  74.         try:
  75.             old_cu = self.get_cover_ur()
  76.         except:
  77.             pass
  78.         new_cu = self.get_cover_img_url()
  79.         self.cover_url = new_cu
  80.         self._download_cover()
  81.  
  82.         outfile = os.path.join(self.output_dir, 'cover_img.jpg')
  83.         self.prepare_masthead_image(self.cover_path, outfile)
  84.  
  85.         self.cover_url = old_cu
  86.         self.cover_img_path = outfile
  87.  
  88.     def download_cover_img(self):
  89.         try:
  90.             self._download_cover_img()
  91.             self.report_progress(1, _('Downloaded cover to %s') % self.cover_img_path)
  92.         except:
  93.             self.log.exception('Failed to download cover img')
  94.             self.cover_img_path = None
  95.  
  96.     def prepare_cover_image(self, path_to_image, out_path):
  97.         img = Image()
  98.         img.open(path_to_image)
  99.         img.save(out_path)
  100.  
  101.     def default_cover(self, cover_file):
  102.         '''
  103.         Create a generic cover for recipes that have a special cover img
  104.         '''
  105.         try:
  106.             try:
  107.                 from PIL import Image, ImageDraw, ImageFont
  108.                 Image, ImageDraw, ImageFont
  109.             except ImportError:
  110.                 import Image, ImageDraw, ImageFont
  111.             font_path = P('fonts/liberation/LiberationSerif-Bold.ttf')
  112.             title = self.title if isinstance(self.title, unicode) else \
  113.                     self.title.decode(preferred_encoding, 'replace')
  114.             date = strftime(self.timefmt)
  115.             app = '['+__appname__ +' '+__version__+']'
  116.  
  117.             COVER_WIDTH, COVER_HEIGHT = 590, 750
  118.             img = Image.new('RGB', (COVER_WIDTH, COVER_HEIGHT), 'white')
  119.             draw = ImageDraw.Draw(img)
  120.             # Title
  121.             font = ImageFont.truetype(font_path, 44)
  122.             width, height = draw.textsize(title, font=font)
  123.             left = max(int((COVER_WIDTH - width)/2.), 0)
  124.             top = 15
  125.             draw.text((left, top), title, fill=(0,0,0), font=font)
  126.             bottom = top + height
  127.             # Date
  128.             font = ImageFont.truetype(font_path, 32)
  129.             width, height = draw.textsize(date, font=font)
  130.             left = max(int((COVER_WIDTH - width)/2.), 0)
  131.             draw.text((left, bottom+15), date, fill=(0,0,0), font=font)
  132.             # Vanity
  133.             font = ImageFont.truetype(font_path, 28)
  134.             width, height = draw.textsize(app, font=font)
  135.             left = max(int((COVER_WIDTH - width)/2.), 0)
  136.             top = COVER_HEIGHT - height - 15
  137.             draw.text((left, top), app, fill=(0,0,0), font=font)
  138.  
  139.             # Logo
  140.             logo_file = I('library.png')
  141.             self.download_cover_img()
  142.             if getattr(self, 'cover_img_path', None) is not None:
  143.                 logo_file = self.cover_img_path
  144.             self.report_progress(1, _('using cover img from %s') % logo_file)
  145.             logo = Image.open(logo_file, 'r')
  146.             width, height = logo.size
  147.             left = max(int((COVER_WIDTH - width)/2.), 0)
  148.             top = max(int((COVER_HEIGHT - height)/2.), 0)
  149.             img.paste(logo, (left, top))
  150.             img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE)
  151.             img.convert('RGB').save(cover_file, 'JPEG')
  152.             cover_file.flush()
  153.         except Exception, e:
  154.             self.log.exception('Failed to generate default cover ', e)
  155.             return False
  156.         return True
  157.