#!/usr/bin/env python2
# vim:fileencoding=utf-8
#
# Copyright 2014 - 2016 Martin Račák <rakyi@riseup.net>
# Copyright 2011 Miroslav Vasko <zemiak@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__license__ = 'GPL v3'
__copyright__ = ('2014 - 2015 Martin Račák <martin.racak@riseup.net>,'
                 '2011 Miroslav Vasko <zemiak@gmail.com>')

'''
.týždeň - iný pohľad na spoločnosť
'''

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


class Tyzden(BasicNewsRecipe):
    title = u'.týždeň'
    __author__ = u'Martin Račák, zemiak'
    description = u'Politicko-spoločenský týždenník.'
    publisher = 'www.tyzden.sk'
    publication_type = 'magazine'
    language = 'sk'
    needs_subscription = 'optional'
    use_embedded_content = False
    no_stylesheets = True
    base_url = 'http://www.tyzden.sk'
    piano_param = '?piano_d=1'
    issue_url = base_url + '/casopis/'
    keep_only_tags = [
        dict(name='div', attrs={'class': 'detail__title article__title'}),
        dict(name='div', attrs={'class': 'article'}),
    ]
    extra_css = """*, *::before, *::after {
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }

    .detail__content h2::before {
        color: #000;
        content: ".";
        display: inline;
    }

    .highlight {
        color: #bf1f10;
    }

    .content-photo__image-credit,
    .photo__image-credit {
        font-size: 11px;
        font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
        text-transform: uppercase;
    }

    .image-title {
        border-bottom: 3px solid #bf1f10;
        display: block;
        padding-bottom: 3px;
        line-height: 22px;
        font-size: 16px;
        font-family: 'TheMix_Bold', 'Georgia', 'Times', 'Times New Roman', serif;
        font-weight: 500;
    }

    .teaser--mag-feature {
        margin-top: 25px;
        padding: 10px 0 10px;
        width: 100%;
        box-sizing: content-box;
        border-top: 2px dotted #555;
        border-bottom: 2px dotted #555;
        font-size: 20px;
    }

    .teaser__wrapper {
        display: block;
    }

    .teaser a {
        outline: none;
        text-decoration: none;
        color: inherit;
    }

   .teaser__title {
        font-size: 26px;
    }"""

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open(self.base_url + '/' + self.piano_param)
        br.set_cookie('_t', '9bcb7dc397cf9516cbc504b700cf14e', '.tyzden.sk')
        br.set_cookie('pianovisitkey', '', '.tyzden.sk')
        if self.username is not None and self.password is not None:
            br.select_form(nr=2)
            br['email'] = self.username
            br['password'] = self.password
            br.submit()
        return br

    def find_sections(self):
        soup = self.index_to_soup(self.issue_url)
        img_wrapper = soup.find('div', 'mag__title-img-wrapper')
        if img_wrapper is not None:
            self.cover_url = img_wrapper.img['src']

        for section in soup.findAll('div', 'mag__section'):
            section_title = section.find('span', 'mag__section-title')
            yield (self.tag_to_string(section_title), section)

    def find_articles(self, soup):
        for title in soup.findAll('h1', 'teaser__title'):
            yield {
                'title': self.tag_to_string(title.a),
                'url': title.a['href'],
                'date': strftime(' %a, %d %b'),
            }

    def parse_index(self):
        feeds = []
        for title, section in self.find_sections():
            feeds.append((title, list(self.find_articles(section))))
        return feeds
