#!/usr/bin/env  python
__license__   = 'GPL v3'

__copyright__ = '2010, Szing'
__docformat__ = 'restructuredtext en'

'''
globeandmail.com
'''

import re

from calibre.web.feeds.news import BasicNewsRecipe

class AdvancedUserRecipe1287083651(BasicNewsRecipe):
    title          = u'Globe & Mail'
    __author__ = 'Kovid Goyal'
    oldest_article = 2
    no_stylesheets = True
    max_articles_per_feed = 100
    encoding               = 'utf8'
    publisher              = 'Globe & Mail'
    language               = 'en_CA'
    use_embedded_content = False

    no_stylesheets = True
    auto_cleanup = True
    extra_css = 'p.meta {font-size:75%}\n .redtext {color: red;}\n .byline {font-size: 70%}'

    feeds          = [
      (u'Top National Stories', u'http://www.theglobeandmail.com/news/national/?service=rss'),
      (u'Business', u'http://www.theglobeandmail.com/report-on-business/?service=rss'),
      (u'Commentary', u'http://www.theglobeandmail.com/report-on-business/commentary/?service=rss'),
      (u'Blogs', u'http://www.theglobeandmail.com/blogs/?service=rss'),
      (u'Facts & Arguments', u'http://www.theglobeandmail.com/life/facts-and-arguments/?service=rss'),
      (u'Technology', u'http://www.theglobeandmail.com/news/technology/?service=rss'),
      (u'Investing', u'http://www.theglobeandmail.com/globe-investor/?service=rss'),
      (u'Top Polical Stories', u'http://www.theglobeandmail.com/news/politics/?service=rss'),
      (u'Arts', u'http://www.theglobeandmail.com/news/arts/?service=rss'),
      (u'Life', u'http://www.theglobeandmail.com/life/?service=rss'),
      (u'Real Estate', u'http://www.theglobeandmail.com/real-estate/?service=rss'),
      (u'Sports', u'http://www.theglobeandmail.com/sports/?service=rss'),
      (u'Drive', u'http://www.theglobeandmail.com/auto/?service=rss')
    ]

    preprocess_regexps = [
        (re.compile(r'<head.*?</head>', re.DOTALL), lambda m: '<head></head>'),
        (re.compile(r'<script.*?</script>', re.DOTALL), lambda m: ''),
        ]

    #remove_tags_before = dict(name='h1')
    #remove_tags = [
            #dict(name='div', attrs={'id':['ShareArticles', 'topStories']}),
            #dict(href=lambda x: x and 'tracking=' in x),
            #{'class':['articleTools', 'pagination', 'Ads', 'topad',
                #'breadcrumbs', 'footerNav', 'footerUtil', 'downloadlinks']}]

    def populate_article_metadata(self, article, soup, first):
        if first and hasattr(self, 'add_toc_thumbnail'):
            picdiv = soup.find('img')
            if picdiv is not None:
                self.add_toc_thumbnail(article,picdiv['src'])


    #Use the mobile version rather than the web version
    def print_version(self, url):
        return url.rpartition('?')[0] + '?service=mobile'

