| 1 |
# Copyright (c) 2023 https://github.com/ping/ |
| 2 |
# |
| 3 |
# This software is released under the GNU General Public License v3.0 |
| 4 |
# https://opensource.org/licenses/GPL-3.0 |
| 5 |
|
| 6 |
from collections import OrderedDict |
| 7 |
from urllib.parse import urljoin |
| 8 |
|
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes |
| 10 |
|
| 11 |
_issue_url = '' |
| 12 |
|
| 13 |
|
| 14 |
class ProspectMagazineUKFree(BasicNewsRecipe): |
| 15 |
title = 'Prospect Magazine (Free)' |
| 16 |
__author__ = 'ping' |
| 17 |
description = ( |
| 18 |
'Prospect is Britain’s leading current affairs monthly magazine. ' |
| 19 |
'It is an independent and eclectic forum for writing and thinking—in ' |
| 20 |
'print and online. Published every month with two double issues in ' |
| 21 |
'the summer and winter, it spans politics, science, foreign affairs, ' |
| 22 |
'economics, the environment, philosophy and the arts.' |
| 23 |
) |
| 24 |
language = 'en_GB' |
| 25 |
category = 'news, UK' |
| 26 |
publication_type = 'magazine' |
| 27 |
masthead_url = 'https://media.prospectmagazine.co.uk/prod/images/gm_grid_thumbnail/358ffc17208c-f4c3cddcdeda-prospect-masthead.png' |
| 28 |
encoding = 'utf-8' |
| 29 |
remove_javascript = True |
| 30 |
no_stylesheets = True |
| 31 |
ignore_duplicate_articles = {'url'} |
| 32 |
INDEX = 'https://www.prospectmagazine.co.uk/issues' |
| 33 |
|
| 34 |
keep_only_tags = [dict(class_='prop-book-article-panel_main')] |
| 35 |
remove_tags = [ |
| 36 |
dict( |
| 37 |
class_=[ |
| 38 |
'prop-book-review-header-wrapper_magazine', |
| 39 |
'prop-mobile-social-share_header', |
| 40 |
'prop-magazine-link-block', |
| 41 |
'pros-article-body__img-credit', |
| 42 |
'pros-article-topics__wrapper', |
| 43 |
'pros-article-author__image-wrapper', |
| 44 |
'prop-book-review-promo_details-buy-mobile', |
| 45 |
] |
| 46 |
), |
| 47 |
dict(id=['disqus_thread', 'newsletter_wrapper']), |
| 48 |
prefixed_classes('dfp-slot-'), |
| 49 |
] |
| 50 |
|
| 51 |
extra_css = ''' |
| 52 |
h1 { font-size: 1.8rem; margin-bottom: 0.4rem; } |
| 53 |
.prop-book-review-header-wrapper_standfirst { font-size: 1.2rem; font-style: italic; font-weight: normal; margin-bottom: 0.5rem; } |
| 54 |
.prop-book-review-header-wrapper_details { margin-top: 1rem; margin-bottom: 1rem; } |
| 55 |
.prop-book-review-header-wrapper_details-byline { |
| 56 |
display: inline-block; font-weight: bold; color: #444; margin-right: 0.5rem; } |
| 57 |
.prop-book-review-header-wrapper_details-date { display: inline-block; } |
| 58 |
.gd-picture img { display: block; max-width: 100%; height: auto; } |
| 59 |
.pros-article-body__img-caption { |
| 60 |
font-size: 0.8rem; display: block; margin-top: 0.2rem; |
| 61 |
} |
| 62 |
.pullquote, blockquote { text-align: center; margin-left: 0; margin-bottom: 0.4rem; font-size: 1.25rem; } |
| 63 |
.prop-book-review-article_author { margin: 1.5rem 0; font-style: italic; } |
| 64 |
.prop-book-review-promo { margin-bottom: 1rem; } |
| 65 |
''' |
| 66 |
|
| 67 |
def preprocess_html(self, soup): |
| 68 |
# re-position lede image |
| 69 |
lede_img = soup.find('img', class_='prop-book-review-header-wrapper_image') |
| 70 |
meta = soup.find('div', class_='prop-book-review-header-wrapper_details') |
| 71 |
if lede_img and meta: |
| 72 |
lede_img = lede_img.extract() |
| 73 |
meta.insert_after(lede_img) |
| 74 |
|
| 75 |
for img in soup.find_all('img', attrs={'data-src': True}): |
| 76 |
img['src'] = img['data-src'] |
| 77 |
del img['data-src'] |
| 78 |
|
| 79 |
for byline_link in soup.find_all('a', attrs={'data-author-name': True}): |
| 80 |
byline_link.unwrap() |
| 81 |
for author_link in soup.find_all('a', class_='pros-article-author'): |
| 82 |
author_link.unwrap() |
| 83 |
|
| 84 |
return soup |
| 85 |
|
| 86 |
def parse_index(self): |
| 87 |
if not _issue_url: |
| 88 |
issues_soup = self.index_to_soup(self.INDEX) |
| 89 |
curr_issue_a_ele = issues_soup.find('a', class_='pros-collection-landing__item') |
| 90 |
curr_issue_url = urljoin(self.INDEX, curr_issue_a_ele['href']) |
| 91 |
else: |
| 92 |
curr_issue_url = _issue_url |
| 93 |
|
| 94 |
soup = self.index_to_soup(curr_issue_url) |
| 95 |
issue_name = self.tag_to_string(soup.find(class_='magazine-lhc__issue-name')).replace(' issue', '').strip() |
| 96 |
self.timefmt = f' [{issue_name}]' |
| 97 |
|
| 98 |
self.cover_url = soup.find('img', class_='magazine-lhc__cover-image')['data-src'].replace('portrait_small_fit', 'portrait_large_fit') |
| 99 |
|
| 100 |
articles = OrderedDict() |
| 101 |
sections = soup.find_all('div', class_='pro-magazine-section') |
| 102 |
for section in sections: |
| 103 |
section_name = self.tag_to_string(section.find(class_='pro-magazine-section__name')) |
| 104 |
for sect_article in section.find_all(class_='pro-magazine-section__article'): |
| 105 |
articles.setdefault(section_name, []).append({ |
| 106 |
'url': urljoin(self.INDEX, sect_article.find('a')['href']), |
| 107 |
'title': self.tag_to_string(sect_article.find(class_='pro-magazine-section__article-headline')), |
| 108 |
}) |
| 109 |
|
| 110 |
return articles.items() |