__license__ = 'GPL v3'
__copyright__ = '2010, Hans Donner <hans.donner at pobox.com>'
'''
www.standardmedia.co.ke
'''

from calibre.web.feeds.news import BasicNewsRecipe


class NationKeRecipe(BasicNewsRecipe):

    __author__ = 'Hans Donner'

    title = u'Sunday Nation'
    description = 'News from Kenia'
    language = 'en'
    country = 'KE'
    publication_type = 'newspaper'
    publisher = 'nation.co.ke'
    category = 'news, politics, Kenia'

    masthead_url = 'http://www.nation.co.ke/image/view/-/465228/medRes/33884/-/maxh/85/-/12e8pptz/-/Sunday_Logo.gif'

    max_articles_per_feed = 200
    oldest_article = 2

    use_embedded_content = False
    remove_empty_feeds = True

    no_stylesheets = True
    extra_css = ' body{font-family: Verdana,Arial,Helvetica,sans-serif; font-size:0.7em } ' + \
                ' .image{margin-bottom: 1em} '

    keep_only_tags = dict(id='articlebody')

    feeds = [(u'News', u'http://www.nation.co.ke/News/-/1056/1056/-/view/asFeed/-/14nfs48z/-/index.xml'),
             (u'Business', u'http://www.nation.co.ke/business/-/996/996/-/view/asFeed/-/14lpkvc/-/index.xml'),
             (u'InDepth', u'http://www.nation.co.ke/InDepth/-/452898/452898/-/view/asFeed/-/14ndbk6/-/index.xml'),
             (u'Sports', u'http://www.nation.co.ke/sports/-/1090/1090/-/view/asFeed/-/hlukmj/-/index.xml'),
             (u'Magazines', u'http://www.nation.co.ke/magazines/-/1190/1190/-/view/asFeed/-/fcxm6jz/-/index.xml'),
             (u'Op/Ed', u'http://www.nation.co.ke/oped/-/1192/1192/-/view/asFeed/-/unsp8mz/-/index.xml'),
             (u'Blogs', u'http://www.nation.co.ke/blogs/-/620/620/-/view/asFeed/-/28ia05z/-/index.xml')]

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

    def print_version(self, url):
        from calibre.ebooks.BeautifulSoup import BeautifulSoup
        soup = BeautifulSoup(self.browser.open(url).read())
        printversion = soup.find('a', text='Print')
        if printversion is None:
            return url
        else:
            return 'http://www.nation.co.ke' + printversion.parent['href']

    def preprocess_html(self, soup):
        return self.adeify_images(soup)
