| 1 |
from urllib.parse import urljoin |
| 2 |
|
| 3 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 4 |
|
| 5 |
_issue_url = '' |
| 6 |
|
| 7 |
|
| 8 |
class BookforumMagazine(BasicNewsRecipe): |
| 9 |
title = 'Bookforum' |
| 10 |
description = 'Bookforum is an American book review magazine devoted to books and the discussion of literature. https://www.bookforum.com/print' |
| 11 |
language = 'en' |
| 12 |
__author__ = 'ping' |
| 13 |
publication_type = 'magazine' |
| 14 |
encoding = 'utf-8' |
| 15 |
remove_javascript = True |
| 16 |
no_stylesheets = True |
| 17 |
auto_cleanup = False |
| 18 |
compress_news_images = True |
| 19 |
compress_news_images_auto_size = 8 |
| 20 |
|
| 21 |
keep_only_tags = [dict(class_='blog-article')] |
| 22 |
remove_tags = [dict(name=['af-share-toggle', 'af-related-articles'])] |
| 23 |
|
| 24 |
extra_css = ''' |
| 25 |
.blog-article__header { font-size: 1.8rem; margin-bottom: 0.4rem; } |
| 26 |
.blog-article__subtitle { font-size: 1.2rem; font-style: italic; margin-bottom: 1rem; } |
| 27 |
.blog-article__writer { font-size: 1rem; font-weight: bold; color: #444; } |
| 28 |
.blog-article__book-info { margin: 1rem 0; } |
| 29 |
.article-image-container img, .blog-article__publication-media img { |
| 30 |
display: block; max-width: 100%; height: auto; |
| 31 |
} |
| 32 |
.blog-article__caption { font-size: 0.8rem; display: block; margin-top: 0.2rem; } |
| 33 |
''' |
| 34 |
|
| 35 |
def preprocess_html(self, soup): |
| 36 |
# strip away links that's not needed |
| 37 |
for ele in soup.select('.blog-article__header a'): |
| 38 |
ele.unwrap() |
| 39 |
return soup |
| 40 |
|
| 41 |
def parse_index(self): |
| 42 |
soup = self.index_to_soup(_issue_url or 'https://www.bookforum.com/print') |
| 43 |
meta_ele = soup.find('meta', property='og:title') |
| 44 |
if meta_ele: |
| 45 |
self.timefmt = f' [{meta_ele["content"]}]' |
| 46 |
|
| 47 |
cover_ele = soup.find('img', class_='toc-issue__cover') |
| 48 |
if cover_ele: |
| 49 |
self.cover_url = urljoin( |
| 50 |
'https://www.bookforum.com', |
| 51 |
soup.find('img', class_='toc-issue__cover')['src'], |
| 52 |
) |
| 53 |
|
| 54 |
articles = {} |
| 55 |
for sect_ele in soup.find_all('div', class_='toc-articles__section'): |
| 56 |
section_name = self.tag_to_string(sect_ele.find('a', class_='toc__anchor-links__link')) |
| 57 |
for article_ele in sect_ele.find_all('article'): |
| 58 |
title_ele = article_ele.find('h1') |
| 59 |
sub_title_ele = article_ele.find(class_='toc-article__subtitle') |
| 60 |
articles.setdefault(section_name, []).append({ |
| 61 |
'title': self.tag_to_string(title_ele), |
| 62 |
'url': article_ele.find('a', class_='toc-article__link')['href'], |
| 63 |
'description': self.tag_to_string(sub_title_ele) if sub_title_ele else '', |
| 64 |
}) |
| 65 |
return articles.items() |