#!/usr/bin/env python
##
# Title:        Common Dreams
##
# License:      GNU General Public License v3 -
# http://www.gnu.org/copyleft/gpl.html

# Feb 2012: Cleaned up the output to have only the main article

__license__ = 'GNU General Public License v3 - http://www.gnu.org/copyleft/gpl.html'
'''
commondreams.org
'''

import re

from calibre.web.feeds.news import BasicNewsRecipe


class CommonDreams(BasicNewsRecipe):
    # Identify the recipe

    title = 'Common Dreams'
    description = 'Breaking News & Views for the Progressive Community.'
    cover_url = 'https://s3.amazonaws.com/s3.commondreams.org/images/common-dreams.png'
    __author__ = 'XanthanGum'
    language = 'en'

    oldest_article = 7
    max_articles_per_feed = 100

    no_stylesheets = True
    remove_javascript = True

    # Specify extra CSS - overrides ALL other CSS (IE. Added last).
    extra_css = '''body { font-family: verdana, helvetica, sans-serif; }
                   .introduction, .first { font-weight: bold; }
                   .cross-head { font-weight: bold; font-size: 125%; }
                   .cap, .caption { display: block; font-size: 80%; font-style: italic; }
                   .cap, .caption, .caption img, .caption span { display: block; margin: 5px auto; }
                   .byl, .byd, .byline img, .byline-name, .byline-title, .author-name, .author-position,
                      .correspondent-portrait img, .byline-lead-in, .name, .bbc-role { display: block;
                      font-size: 80%; font-style: italic; margin: 1px auto; }
                   .story-date, .published { font-size: 80%; }
                   table { width: 100%; }
                   td img { display: block; margin: 5px auto; }
                   ul { padding-top: 10px; }
                   ol { padding-top: 10px; }
                   li { padding-top: 5px; padding-bottom: 5px; }
                   h1 { font-size: 175%; font-weight: bold; }
                   h2 { font-size: 150%; font-weight: bold; }
                   h3 { font-size: 125%; font-weight: bold; }
                   h4, h5, h6 { font-size: 100%; font-weight: bold; }'''

    # Remove the line breaks and float left/right and picture width/height.
    preprocess_regexps = [
        (re.compile(r'<br[ ]*/>', re.IGNORECASE), lambda m: ''),
        (re.compile(r'<br[ ]*clear.*/>', re.IGNORECASE), lambda m: ''),
        (re.compile(r'float:.*?'), lambda m: ''),
        (re.compile(r'width:.*?px'), lambda m: ''),
        (re.compile(r'height:.*?px'), lambda m: ''),
        (re.compile(r'<a.*?>'), lambda m: ''),
        (re.compile(r'</a>'), lambda m: ''),
    ]

    # Main article is inside this tag
    keep_only_tags = [
        dict(name='h1', attrs={'class': 'node__title managed-node-title'}),
        dict(
            name='div',
            attrs={
                'class': [
                    'field field--name-field-article-img field--type-image field--label-hidden',
                    'field field--name-body field--type-text-with-summary field--label-hidden',
                ]
            },
        ),
    ]

    # Identify the news feeds

    feeds = [
        ('Headlines', 'https://www.commondreams.org/feed/headlines_rss'),
        ('Further News Articles', 'https://www.commondreams.org/feed/further_rss'),
        ('Views', 'https://www.commondreams.org/feed/views_rss'),
        ('Progressive Newswire', 'https://www.commondreams.org/feed/newswire_rss'),
    ]
