#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import unicode_literals

import re

from calibre.web.feeds.news import BasicNewsRecipe


class AListApart(BasicNewsRecipe):
    __author__ = 'Marc Busqué <marc@lamarciana.com>'
    __url__ = 'http://www.lamarciana.com'
    __version__ = '2.0.1'
    __license__ = 'GPL v3'
    __copyright__ = '2012, Marc Busqué <marc@lamarciana.com>'
    title = 'A List Apart'
    description = 'A List Apart Magazine (ISSN: 1534-0295) explores the design, development, and meaning of web content, with a special focus on web standards and best practices. This recipe retrieve articles and columns.'  # noqa: E501
    language = 'en'
    tags = 'web development, software'
    oldest_article = 120
    remove_empty_feeds = True
    encoding = 'utf8'
    cover_url = 'https://alistapart.com/wp-content/uploads/2019/03/cropped-icon_navigation-laurel-512.jpg'

    def __init__(self, *args, **kwargs):
        BasicNewsRecipe.__init__(self, *args, **kwargs)
        d = self.recipe_specific_options.get('days')
        if d and isinstance(d, str):
            self.oldest_article = float(d)

    def get_extra_css(self):
        if not self.extra_css:
            br = self.get_browser()
            self.extra_css = (
                br
                .open_novisit('https://raw.githubusercontent.com/laMarciana/gutenweb/master/dist/gutenweb.css')
                .read()
                .decode('utf-8')
                .replace('@charset "UTF-8";', '')
            )
        return self.extra_css

    feeds = [
        ('A List Apart', 'https://alistapart.com/main/feed'),
    ]

    def image_url_processor(self, baseurl, url):
        if re.findall(r'alistapart\.com', url):
            return 'http:' + url
        else:
            return url
