__license__   = 'GPL v3'
__copyright__ = '2011, Darko Miletic <darko.miletic at gmail.com>'
'''
frontlineonnet.com
'''

import re
from calibre import strftime
from calibre.web.feeds.news import BasicNewsRecipe

class Frontlineonnet(BasicNewsRecipe):
    title                = 'Frontline'
    __author__           = 'Darko Miletic'
    description          = "India's national magazine"
    publisher            = 'Frontline'
    category             = 'news, politics, India'
    no_stylesheets       = True
    delay                = 1
    INDEX                = 'http://frontlineonnet.com/'
    use_embedded_content = False
    encoding             = 'cp1252'
    language             = 'en_IN'
    publication_type     = 'magazine'
    masthead_url         = 'http://frontlineonnet.com/images/newfline.jpg'
    extra_css            = """
                              body{font-family: Verdana,Arial,Helvetica,sans-serif}
                              img{margin-top:0.5em; margin-bottom: 0.7em; display: block}
                           """

    conversion_options = {
                          'comment'          : description
                        , 'tags'             : category
                        , 'publisher'        : publisher
                        , 'language'         : language
                        , 'linearize_tables' : True
                        }

    preprocess_regexps = [
                           (re.compile(r'.*?<base', re.DOTALL|re.IGNORECASE),lambda match: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html dir="ltr" xml:lang="en-IN"><head><title>title</title><base')
                          ,(re.compile(r'<base .*?>', re.DOTALL|re.IGNORECASE),lambda match: '</head><body>')
                          ,(re.compile(r'<byline>', re.DOTALL|re.IGNORECASE),lambda match: '<div class="byline">')
                          ,(re.compile(r'</byline>', re.DOTALL|re.IGNORECASE),lambda match: '</div>')
                          ,(re.compile(r'<center>', re.DOTALL|re.IGNORECASE),lambda match: '<div class="ctr">')
                          ,(re.compile(r'</center>', re.DOTALL|re.IGNORECASE),lambda match: '</div>')
                         ]

    keep_only_tags= [
                      dict(name='font', attrs={'class':'storyhead'})
                     ,dict(attrs={'class':'byline'})
                    ]
    remove_attributes=['size','noshade','border']

    def preprocess_html(self, soup):
        for item in soup.findAll(style=True):
            del item['style']
        for item in soup.findAll('img'):
            if not item.has_key('alt'):
               item['alt'] = 'image'
        return soup

    def parse_index(self):
        articles = []
        soup = self.index_to_soup(self.INDEX)
        for feed_link in soup.findAll('a',href=True):
            if feed_link['href'].startswith('stories/'):
                url   = self.INDEX + feed_link['href']
                title = self.tag_to_string(feed_link)
                date  = strftime(self.timefmt)
                articles.append({
                                  'title'      :title
                                 ,'date'       :date
                                 ,'url'        :url
                                 ,'description':''
                                })
        return [('Frontline', articles)]

    def print_version(self, url):
        return "http://www.hinduonnet.com/thehindu/thscrip/print.pl?prd=fline&file=" + url.rpartition('/')[2]

    def image_url_processor(self, baseurl, url):
        return url.replace('../images/', self.INDEX + 'images/').strip()
