From b62b38fc259e4069907f743bea1ed09202d5c614 Mon Sep 17 00:00:00 2001 From: jn809 Date: Fri, 10 Apr 2020 14:02:47 +0800 Subject: [PATCH] fix recipe after website redesign --- recipes/mit_technology_review.recipe | 31 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/recipes/mit_technology_review.recipe b/recipes/mit_technology_review.recipe index 65b2114d81..1b39475588 100644 --- a/recipes/mit_technology_review.recipe +++ b/recipes/mit_technology_review.recipe @@ -8,6 +8,7 @@ __copyright__ = '2015 Michael Marotta ' technologyreview.com ''' from calibre.web.feeds.news import BasicNewsRecipe +import re def absurl(x): @@ -37,24 +38,40 @@ class MitTechnologyReview(BasicNewsRecipe): tags = 'news, technology, science' no_stylesheets = True + """ + regex for class names + """ + articleHeaderRegex= '^.*contentHeader__wrapper.*$' + editorLetterHeaderRegex = "^.*contentHeader--vertical__wrapper.*$" + articleContentRegex = "^.*contentbody__wrapper.*$" + imagePlaceHolderRegex = "^.*image__placeholder.*$" + advertisementRegex = "^.*sliderAd__wrapper.*$" + keep_only_tags = [ - classes( - 'article-topper__topic article-topper__title article-topper__media-wrap article-body__content storyContent'), + dict(name='header', attrs={'class': re.compile(editorLetterHeaderRegex, re.IGNORECASE)}), + dict(name='header', attrs={'class': re.compile(articleHeaderRegex, re.IGNORECASE)}), + dict(name='div', attrs={'class': re.compile(articleContentRegex, re.IGNORECASE)}) ] remove_tags = [ - classes('l-article-list signup-wrapper'), - dict(name="svg") + dict(name="aside"), + dict(name="svg"), + dict(name="blockquote"), + dict(name="img", attrs={'class': re.compile(imagePlaceHolderRegex, re.IGNORECASE)}), + dict(name="div", attrs={'class': re.compile(advertisementRegex, re.IGNORECASE)}), ] def parse_index(self): soup = self.index_to_soup(self.INDEX) # find cover self.cover_url = absurl(soup.find( - 'a', attrs={'class': 'magazine-topper__cover'}).find('img', src=True)['src']) + "div", attrs={"class":lambda name: name.startswith("magazineHero__image") if name else False}).find( + "img", + src=True + )['src']) # parse articles current_articles = [] - for div in soup.findAll(attrs={'class': lambda x: x in - 'magazine-topper__title magazine-features-item__top-title magazine-features-item-title author-tz__title feed-tz__title'.split()}): + classNamePrefixes = ["magazineHero__letter--", "teaserItem__title", "teaserItem--aside__title"] + for div in soup.findAll(attrs={'class': lambda x: any(x.startswith(prefix) for prefix in classNamePrefixes) if x else False}): a = div.find('a', href=True) title = self.tag_to_string(a).strip() href = absurl(a['href'])