import re

from calibre.web.feeds.news import BasicNewsRecipe


class CATOInstitute(BasicNewsRecipe):
    title = 'The CATO Institute'
    description = (
        'The Cato Institute is a public policy research organization — a think tank — '
        'dedicated to the principles of individual liberty, limited government, free markets and peace. '
        'Its scholars and analysts conduct independent, nonpartisan research on a wide range of policy issues.'
    )
    __author__ = '_reader'
    __date__ = '05 July 2012'
    __version__ = '1.0'
    cover_url = 'http://www.cato.org/images/logo.jpg'
    masthead_url = 'http://www.cato.org/images/logo.jpg'
    language = 'en'
    oldest_article = 30  # days
    max_articles_per_feed = 100
    needs_subscription = False
    publisher = 'CATO Institute'
    category = 'commentary'
    tags = 'commentary'
    publication_type = 'blog'
    no_stylesheets = True
    use_embedded_content = False
    encoding = None
    simultaneous_downloads = 10
    recursions = 0
    remove_javascript = True
    remove_empty_feeds = True
    auto_cleanup = True

    conversion_options = {
        'comments': description,
        'tags': tags,
        'language': language,
        'publisher': publisher,
        'authors': publisher,
        'smarten_punctuation': True,
    }

    feeds = [
        ('Cato Recent Op-Eds', 'http://feeds.cato.org/CatoRecentOpeds'),
        ('Cato Homepage Headlines', 'http://feeds.cato.org/CatoHomepageHeadlines'),
        ('Cato Media Updates', 'http://feeds.cato.org/CatoMediaUpdates'),
        ('Cato@Liberty', 'http://feeds.cato.org/Cato-at-liberty'),
        ('Cato Unbound', 'http://feeds.feedburner.com/cato-unbound'),
        ('Education and Child Policy', 'http://www.cato.org/rss/ra.xml?name=education-child-policy'),
        ('Finance, Banking & Monetary Policy', 'http://www.cato.org/rss/ra.xml?name=finance-banking-monetary-policy'),
        ('Government and Politics', 'http://www.cato.org/rss/ra.xml?name=government-politics'),
        ('International Economics & Development', 'http://www.cato.org/rss/ra.xml?name=international-economics-development'),
        ('Political Philosophy', 'http://www.cato.org/rss/ra.xml?name=political-philosophy'),
        ('Social Security', 'http://www.cato.org/rss/ra.xml?name=social-security'),
        ('Telecom, Internet & Information Policy', 'http://www.cato.org/rss/ra.xml?name=telecom-internet-information-policy'),
        ('Energy and Environment', 'http://www.cato.org/rss/ra.xml?name=energy-environment'),
        ('Foreign Policy and National Security', 'http://www.cato.org/rss/ra.xml?name=foreign-policy-national-security'),
        ('Health Care', 'http://www.cato.org/rss/ra.xml?name=health-care'),
        ('Law and Civil Liberties', 'http://www.cato.org/rss/ra.xml?name=law-civil-liberties'),
        ('Regulatory Studies', 'http://www.cato.org/rss/ra.xml?name=regulatory-studies'),
        ('Tax and Budget Policy', 'http://www.cato.org/rss/ra.xml?name=tax-budget-policy'),
        ('Trade and Immigration', 'http://www.cato.org/rss/ra.xml?name=trade-immigration'),
    ]

    def print_version(self, url):
        R_unbound = re.compile(r'(^.*cato-unbound.*)(\/\?utm_source.*$)', re.DOTALL | re.IGNORECASE)  # CATO Unbound
        R_pubs = re.compile(r'(^.*\/publications\/.*$)', re.DOTALL | re.IGNORECASE)  # CATO Publications
        if re.match(R_unbound, url):
            printURL = r'\g<1>' + '/print/'
        elif re.match(R_pubs, url):
            printURL = url + '?print'
        else:
            printURL = url + '/print/'
        return printURL
