#!/usr/bin/env python # vim:fileencoding=utf-8 # License: GPLv3 Copyright: 2020, Kovid Goyal from __future__ import absolute_import, division, print_function, unicode_literals from calibre.web.feeds.news import BasicNewsRecipe def classes(classes): q = frozenset(classes.split(' ')) return dict(attrs={ 'class': lambda x: x and frozenset(x.split()).intersection(q)}) class AdvancedUserRecipe1282101454(BasicNewsRecipe): title = 'Popular Science' language = 'en' __author__ = 'Kovid Goyal' description = 'Popular Science' publisher = 'Popular Science' max_articles_per_feed = 100 ignore_duplicate_articles = {'url'} no_stylesheets = True keep_only_tags = [ classes('Article-header Article-excerpt Article-author Article-thumbnail Article-bodyText'), ] def parse_section_index(self, slug): url = 'https://www.popsci.com/{}/'.format(slug) self.log('Section:', url) soup = self.index_to_soup(url) main = soup.find(**classes('PostsContainer')) for div in main.findAll(**classes('PostItem')): a = div.find('a', href=True, **classes('PostItem-link')) url = a['href'] title = self.tag_to_string(div.find(**classes('PostItem-title'))) desc = '' dek = div.find(**classes('PostItem-excerpt')) if dek is not None: desc = self.tag_to_string(dek) self.log(' ', title, url) yield {'title': title, 'url': url, 'description': desc} def parse_index(self): sections = [] for slug, title in { 'science': 'Science', 'technology': 'Technology', 'diy': 'DIY', 'reviews': 'Reviews', }.items(): articles = list(self.parse_section_index(slug)) if articles: sections.append((title, articles)) return sections def preprocess_html(self, soup): for img in soup.findAll('img', attrs={'data-medsrc': True}): img['src'] = img['data-medsrc'] return soup