Debian Codegraph @

recipes/kirkusreviews.recipe

raw
1 from urllib.parse import urljoin
2
3 from calibre.web.feeds.news import BasicNewsRecipe
4
5
6 class KirkusReviews(BasicNewsRecipe):
7 title = 'Kirkus Reviews'
8 description = (
9 'Kirkus Reviews is an American book review magazine founded in 1933 by Virginia Kirkus.'
10 ' The magazine is headquartered in New York City. Released twice monthly on the 1st/15th.'
11 )
12 language = 'en'
13 __author__ = 'ping'
14 publication_type = 'magazine'
15 masthead_url = 'https://d1fd687oe6a92y.cloudfront.net/img/kir_images/logo/kirkus-nav-logo.svg'
16 encoding = 'utf-8'
17 remove_javascript = True
18 no_stylesheets = True
19 auto_cleanup = False
20 ignore_duplicate_articles = {'url'}
21 compress_news_images = True
22 compress_news_images_auto_size = 6
23 max_articles_per_feed = 99
24
25 keep_only_tags = [
26 dict(
27 class_=[
28 'article-author',
29 'article-author-img-start',
30 'article-author-description-start',
31 'single-review',
32 ]
33 )
34 ]
35 remove_tags = [
36 dict(
37 class_=[
38 'sidebar-content',
39 'article-social-share-desktop-first',
40 'article-social-share-desktop-pagination',
41 'article-social-share-mobile',
42 'share-review-text',
43 'like-dislike-article',
44 'rate-this-book-text',
45 'input-group',
46 'user-comments',
47 'show-all-response-text',
48 'button-row',
49 'hide-on-mobile',
50 'related-article',
51 'breadcrumb-row',
52 'shop-now-dropdown',
53 ]
54 )
55 ]
56 remove_tags_after = [dict(class_='single-review')]
57
58 extra_css = '''
59 .image-container img { max-width: 100%; height: auto; margin-bottom: 0.2rem; }
60 .photo-caption { font-size: 0.8rem; margin-bottom: 0.5rem; display: block; }
61 .book-review-img .image-container { text-align: center; }
62 .book-rating-module .description-title { font-size: 1.25rem; margin-left: 0; text-align: center; }
63 '''
64
65 def preprocess_html(self, soup):
66 h1 = soup.find(class_='article-title')
67 book_cover = soup.find('ul', class_='book-review-img')
68 if book_cover:
69 for li in book_cover.find_all('li'):
70 li.name = 'div'
71 book_cover.name = 'div'
72 if h1:
73 book_cover.insert_before(h1.extract())
74 return soup
75
76 def parse_index(self):
77 issue_url = 'https://www.kirkusreviews.com/magazine/current/'
78 soup = self.index_to_soup(issue_url)
79 issue = soup.find(name='article', class_='issue-container')
80 cover_img = issue.select('.issue-header .cover-image img')
81 if cover_img:
82 self.cover_url = cover_img[0]['src']
83
84 h1 = issue.find('h1')
85 if h1:
86 self.timefmt = f' [{self.tag_to_string(h1)}]' # edition
87
88 articles = {}
89 for book_ele in soup.find_all(name='div', class_='issue-featured-book'):
90 link = book_ele.find('a')
91 if not link:
92 continue
93 section = self.tag_to_string(book_ele.find('h3')).upper()
94 articles.setdefault(section, []).append({'url': urljoin(issue_url, link['href']), 'title': link['title']})
95 for post_ele in issue.select('div.issue-more-posts ul li div.lead-text'):
96 link = post_ele.find('a')
97 if not link:
98 continue
99 section = self.tag_to_string(post_ele.find(class_='lead-text-type')).upper()
100 articles.setdefault(section, []).append({
101 'url': urljoin(issue_url, link['href']),
102 'title': self.tag_to_string(link),
103 })
104 for section_ele in issue.select('section.reviews-section'):
105 section_articles = []
106 for review in section_ele.select('ul li.starred'):
107 link = review.select('h4 a')
108 if not link:
109 continue
110 description = review.find('p')
111 section_articles.append({
112 'url': urljoin(issue_url, link[0]['href']),
113 'title': self.tag_to_string(link[0]),
114 'description': '' if not description else self.tag_to_string(description),
115 })
116 if not section_articles:
117 continue
118 section = self.tag_to_string(section_ele.find('h3')).upper()
119 if section not in articles:
120 articles[section] = []
121 articles.setdefault(section, []).extend(section_articles)
122
123 return articles.items()