| 1 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 2 |
|
| 3 |
|
| 4 |
class TheBaffler(BasicNewsRecipe): |
| 5 |
title = 'The Baffler' |
| 6 |
__author__ = 'flobotnik and unkn0wn' |
| 7 |
description = 'This magazine contains left-wing criticism, cultural analysis, shorts stories, poems and art. They publish six print issues annually.' |
| 8 |
language = 'en' |
| 9 |
encoding = 'UTF-8' |
| 10 |
no_javascript = True |
| 11 |
no_stylesheets = True |
| 12 |
remove_attributes = ['style', 'height', 'width'] |
| 13 |
|
| 14 |
extra_css = ''' |
| 15 |
.entry-subtitle{color:#202020; font-style:italic; text:align:left;} |
| 16 |
blockquote{color:gray;} |
| 17 |
em{color:#404040;} |
| 18 |
.wp-caption-text{font-size:small; text-align:center;} |
| 19 |
.lg:text-xs{color:gray; font-size:small; text-align:center;} |
| 20 |
.author-meta{font-size:small; color:gray;} |
| 21 |
''' |
| 22 |
|
| 23 |
keep_only_tags = [dict(name='main', attrs={'id': 'main'})] |
| 24 |
|
| 25 |
remove_tags = [ |
| 26 |
classes('entry-date issue-number-segment single-article-vertical donation-footer story-footer ml-4 mt-14'), |
| 27 |
dict(name='footer'), |
| 28 |
dict(name='a', class_='ml-4 pr-px font-sans text-sm lg:text-xs whitespace-nowrap'), |
| 29 |
] |
| 30 |
|
| 31 |
def get_cover_url(self): |
| 32 |
soup = self.index_to_soup('https://shop.exacteditions.com/us/the-baffler') |
| 33 |
tag = soup.find('div', attrs={'class': 'row'}) |
| 34 |
if tag: |
| 35 |
self.cover_url = tag.find('img')['src'] |
| 36 |
return getattr(self, 'cover_url', self.cover_url) |
| 37 |
|
| 38 |
def parse_index(self): |
| 39 |
soup = self.index_to_soup('https://thebaffler.com/issues') |
| 40 |
issue = soup.find('article') |
| 41 |
edition = self.tag_to_string(issue.find('h2')).strip().split('—')[1] |
| 42 |
if edition: |
| 43 |
self.log('Downloading Issue: ', edition) |
| 44 |
self.title = 'The Baffler : ' + edition |
| 45 |
self.timefmt = ' [' + self.tag_to_string(issue.find('div', **classes('font-lion'))).strip() + ']' |
| 46 |
a = issue.find('a') |
| 47 |
|
| 48 |
self.description = '\n\n' + self.tag_to_string(a).strip() + '\n\n' + self.description |
| 49 |
|
| 50 |
soup = self.index_to_soup(a['href']) |
| 51 |
ans = [] |
| 52 |
main = soup.find('main', attrs={'id': 'main'}) |
| 53 |
for section in main.findAll('section'): |
| 54 |
current_section = self.tag_to_string(section.h3).strip() |
| 55 |
self.log(current_section) |
| 56 |
articles = [] |
| 57 |
for h4 in section.findAll('h4'): |
| 58 |
title = self.tag_to_string(h4) |
| 59 |
url = h4.a['href'] |
| 60 |
desc = '' |
| 61 |
span = h4.findNext('span') |
| 62 |
if span: |
| 63 |
desc = self.tag_to_string(span).strip() |
| 64 |
span2 = span.findNext('span') |
| 65 |
if span2: |
| 66 |
desc = self.tag_to_string(span2).strip() + ' | ' + desc |
| 67 |
self.log('\t', title, '\n\t', desc, '\n\t\t', url) |
| 68 |
articles.append({'title': title, 'url': url, 'description': desc}) |
| 69 |
if articles: |
| 70 |
ans.append((current_section, articles)) |
| 71 |
return ans |
| 72 |
|
| 73 |
def preprocess_html(self, soup): |
| 74 |
div = soup.find('div', **classes('entry-title')) |
| 75 |
if div: |
| 76 |
div.name = 'h1' |
| 77 |
for p in soup.findAll('p', attrs={'class': 'parasectionhed'}): |
| 78 |
p.name = 'h4' |
| 79 |
return soup |