#!/usr/pkg/bin/python3.13

__license__ = 'GPL v3'
'''
sciencenews.org
'''

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


class ScienceNewsIssue(BasicNewsRecipe):
    title = u'Science News'
    __author__ = u'Darko Miletic, Sujata Raman and Starson17'
    description = u'''Science News is an award-winning bi-weekly
    newsmagazine covering the most important research in all fields of science.
    This recipe downloads the last 2 weeks of articles.'''
    category = u'Science, Technology, News'
    publisher = u'Society for Science & the Public'
    oldest_article = 14
    language = 'en'
    max_articles_per_feed = 50
    no_stylesheets = True
    use_embedded_content = False
    timefmt = ' [%A, %d %B, %Y]'
    auto_cleanup = False

    keep_only_tags = [
        dict(
            attrs={
                'class':
                lambda x: x and (
                    'single__content___' in x or 'header-default__title___' in x or
                    'header-default__deck___' in x or 'header-default__figure___' in
                    x
                )
            }
        )
    ]
    remove_tags = [
        dict(
            attrs={'class': lambda x: x and ('newsletter-signup__wrapper___' in x)}
        )
    ]

    feeds = [(u"Articles", u'https://www.sciencenews.org/feed')]

    def get_cover_url(self):
        d = datetime.date(2022, 3, 26)
        t = datetime.date.today()
        ld = None
        while d <= t:
            ld = d
            d += datetime.timedelta(days=14)
        url = ld.strftime(
            "https://www.sciencenews.org/wp-content/uploads/%Y/%m/%m%d%y_cover.jpg"
        )
        return url
