#!/usr/bin/env python
# vim:fileencoding=utf-8
from calibre.web.feeds.news import BasicNewsRecipe, classes
class EpochTimes(BasicNewsRecipe):
    title = 'The Epoch Times new new'
    __author__ = 'Kovid Goyal'
    description = 'US general news'
    language = 'en'
    encoding = 'utf-8'
    oldest_article = 1.2
    max_articles_per_feed = 20
    ignore_duplicate_articles = {'url'}
    remove_attributes = ['height', 'width']
    remove_empty_feeds =  True
    resolve_internal_links = True
    masthead_url = 'https://epochtimes-ny.newsmemory.com/eeLayout/epochtimes/1.0.a/images/webapp/banner.png'

#	post_caption is under leading (featured) photo; 'figcaption' is under in-body photos
    extra_css = """
        body{font-family: Arial,sans-serif }
        .post_caption{font-size: small}
        .author,.date{font-size: small}
    """

#       classes('post-main'), - moved above other classes line
    keep_only_tags = [
        classes('post-main'),
        classes('post_title featured_image author date post_content pricat_name '),
    ]
# 	shortcode is the Related Stories box
    remove_tags = [
        classes('author_wrapper bottom_row'),
        classes('print:hidden h-header'),
        classes('shortcode'),
        dict(name='button'),
    ]

	# feeds can be found at https://www.theepochtimes.com/rssfeeds

    feeds = [
        ('Special Series', 'https://feed.theepochtimes.com/health/special-series/feed'),
        ('US', 'https://feed.theepochtimes.com/us/feed'),
        ('China News', 'https://feed.theepochtimes.com/china/feed'),
        ('World', 'https://feed.theepochtimes.com/world/feed'),
        ('Opinion', 'https://feed.theepochtimes.com/opinion/feed'),
        ('Business & Markets', 'https://feed.theepochtimes.com/business/feed'),
        ('Science', 'https://feed.theepochtimes.com/science/feed'),
        ('Tech', 'https://feed.theepochtimes.com/tech/feed'),
        ('Health & Wellness', 'https://feed.theepochtimes.com/wellness/feed'),
        ('Epoch Taste', 'https://feed.theepochtimes.com/epoch-taste/feed'),
        ('Entertainment', 'https://feed.theepochtimes.com/entertainment/feed'),
    ]
    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'data-src': True}):
            img['src'] = img['data-src']
        title = soup.find(attrs={'class': 'post_title'})
        fi = soup.find(attrs={'class': 'featured_image'})
        if title is not None and fi is not None:
            title.extract()
            fi.insert_before(title)
        for div in soup.findAll(**classes('post-main'))[1:]:
            div.extract()
        return soup
    
# remove hypoerlink and retain text without formatting
# https://www.mobileread.com/forums/showthread.php?t=284965&highlight=remove+hyperlinks

    def postprocess_html(self, soup, first_fetch):
        for a in soup.findAll('a', href=True):
            del a['href']
        return soup