__license__ = 'GPL v3'
__copyright__ = '2008-2012, Darko Miletic <darko.miletic at gmail.com>'
'''
arstechnica.com
'''

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


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={
        'class': lambda x: x and frozenset(x.split()).intersection(q)})


class ArsTechnica(BasicNewsRecipe):
    title = u'Ars Technica'
    language = 'en'
    __author__ = 'Darko Miletic, Sujata Raman, Alexis Rohou, Tom Sparks'
    description = 'Ars Technica: Serving the technologist for 1.2 decades'
    publisher = 'Conde Nast Publications'
    oldest_article = 5
    max_articles_per_feed = 100
    no_stylesheets = True
    encoding = 'utf-8'
    use_embedded_content = False
    remove_empty_feeds = True
    extra_css             = '''
    body {font-family: Arial,sans-serif}
    .heading{font-family: "Times New Roman",serif}
    .byline{font-weight: bold; line-height: 1em; font-size: 0.625em; text-decoration: none}
    img{display: block}
    .caption-text{font-size:small; font-style:italic}
    .caption-byline{font-size:small; font-style:italic; font-weight:bold}
    .video, .page-numbers, .story-sidebar { display: none }
    .image { display: block }
    '''

    keep_only_tags = [
        dict(itemprop=['headline', 'description']),
        classes('post-meta article-guts standalone'),
    ]

    remove_tags = [
        classes('site-header video corner-info article-expander left-column related-stories'),
        dict(name=['object', 'link', 'embed', 'iframe', 'meta']),
        dict(id=['social-left', 'article-footer-wrap']),
        dict(name='nav', attrs={'class': 'subheading'}),
    ]
    remove_attributes = ['lang', 'style']

    # Feed are found here: http://arstechnica.com/rss-feeds/
    feeds = [
        ('Ars Technica', 'http://feeds.arstechnica.com/arstechnica/index'),
        ('Features', 'http://feeds.arstechnica.com/arstechnica/features'),
        ('Technology Lab', 'http://feeds.arstechnica.com/arstechnica/technology-lab'),
        ('Gear &amp; Gadgets', 'http://feeds.arstechnica.com/arstechnica/gadgets'),
        ('Ministry of Innovation', 'http://feeds.arstechnica.com/arstechnica/business'),
        ('Risk Assessment', 'http://feeds.arstechnica.com/arstechnica/security'),
        ('Law &amp; Disorder', 'http://feeds.arstechnica.com/arstechnica/tech-policy'),
        ('Infinite Loop', 'http://feeds.arstechnica.com/arstechnica/apple'),
        ('Opposable Thumbs', 'http://feeds.arstechnica.com/arstechnica/gaming'),
        ('Scientific Method', 'http://feeds.arstechnica.com/arstechnica/science'),
        ('The Multiverse', 'http://feeds.arstechnica.com/arstechnica/multiverse'),
        ('Cars Technica', 'http://feeds.arstechnica.com/arstechnica/cars'),
        ('Staff', 'http://feeds.arstechnica.com/arstechnica/staff-blogs'),
        ('Open Source', 'http://feeds.arstechnica.com/arstechnica/open-source'),
        ('microsoft', 'http://feeds.arstechnica.com/arstechnica/microsoft'),
        ('software', 'http://feeds.arstechnica.com/arstechnica/software'),
        ('telecom', 'http://feeds.arstechnica.com/arstechnica/telecom'),
        ('Internet', 'http://feeds.arstechnica.com/arstechnica/web'),
    ]

    recursions = 1

    def is_link_wanted(self, url, tag):
        return re.search(r'/[0-9]/$', url) is not None

    def postprocess_html(self, soup, first_fetch):
        if not first_fetch:
            for x in soup.findAll(itemprop=['headline', 'description']):
                x.extract()
            for x in soup.findAll(**classes('post-meta')):
                x.extract()
        return soup
