| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
""" |
| 4 |
harpers.org |
| 5 |
""" |
| 6 |
|
| 7 |
from calibre import browser |
| 8 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 9 |
|
| 10 |
|
| 11 |
class Harpers(BasicNewsRecipe): |
| 12 |
title = 'Harper’s Magazine' |
| 13 |
__author__ = 'unkn0wn' |
| 14 |
language = 'en_US' |
| 15 |
description = ( |
| 16 |
'Harper’s Magazine, the oldest general-interest monthly in America, explores the issues that drive our ' |
| 17 |
'national conversation, through long-form narrative journalism and essays, and such celebrated ' |
| 18 |
'features as the iconic Harper’s Index. With its emphasis on fine writing and original thought ' |
| 19 |
'Harper’s provides readers with a unique perspective on politics, society, the environment, and culture.' |
| 20 |
) |
| 21 |
publisher = "Harper's Magazine " |
| 22 |
category = 'news, politics, USA' |
| 23 |
no_stylesheets = True |
| 24 |
use_embedded_content = False |
| 25 |
masthead_url = 'https://harpers.org/wp-content/themes/timber/assets/img/logo.svg' |
| 26 |
ignore_duplicate_articles = {'url'} |
| 27 |
encoding = 'utf-8' |
| 28 |
remove_attributes = ['style', 'height', 'width'] |
| 29 |
|
| 30 |
keep_only_tags = [ |
| 31 |
dict(attrs={'class': lambda x: x and ('title-header desktop ' in x or 'col-md-8 col-xl-9' in x)}), |
| 32 |
classes('article-hero-img entry-content pdf-only'), |
| 33 |
] |
| 34 |
remove_tags = [classes('header-controls')] |
| 35 |
|
| 36 |
extra_css = ''' |
| 37 |
img {display:block; margin:0 auto;} |
| 38 |
.category, .from-issue { font-size:small; color:#404040; } |
| 39 |
.wp-caption-text { font-size:small; text-align:center; } |
| 40 |
.subheading { font-style:italic; color:#202020; } |
| 41 |
.byline { font-size:small; } |
| 42 |
em, blockquote { color:#202020; } |
| 43 |
''' |
| 44 |
|
| 45 |
def preprocess_html(self, soup): |
| 46 |
sub = soup.find(attrs={'class': 'subheading'}) |
| 47 |
if sub: |
| 48 |
sub.name = 'p' |
| 49 |
for img in soup.findAll('img', attrs={'srcset': True}): |
| 50 |
for src in img['srcset'].split(','): |
| 51 |
if '768w' in src: |
| 52 |
img['src'] = src.split()[0] |
| 53 |
return soup |
| 54 |
|
| 55 |
recipe_specific_options = { |
| 56 |
'date': { |
| 57 |
'short': 'The date of the edition to download (YYYY/MM format)', |
| 58 |
'long': 'For example, 2023/08', |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
def parse_index(self): |
| 63 |
issues_soup = self.index_to_soup('https://harpers.org/issues/') |
| 64 |
a_ele = issues_soup.select_one('div.issue-card a') |
| 65 |
self.timefmt = ' [' + self.tag_to_string(a_ele.find(attrs={'class': 'issue-title'})) + ']' |
| 66 |
url = 'https://harpers.org' + a_ele['href'] |
| 67 |
|
| 68 |
edition = self.recipe_specific_options.get('date') |
| 69 |
if edition and isinstance(edition, str): |
| 70 |
url = 'https://harpers.org/archive/' + edition |
| 71 |
self.timefmt = ' [' + edition + ']' |
| 72 |
|
| 73 |
self.log('Fetching articles from:', url) |
| 74 |
soup = self.index_to_soup(url) |
| 75 |
# open('/t/raw.html', 'w').write(str(soup)) |
| 76 |
cov_div = soup.find('div', attrs={'class': 'issue-cover'}) |
| 77 |
if cov_div: |
| 78 |
self.cover_url = cov_div.find('img', attrs={'class': 'cover-img'})['src'] |
| 79 |
ans = [] |
| 80 |
for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith('/')}): |
| 81 |
if not a.find('img') and a.find(['h1', 'h2', 'h3', 'h4']): |
| 82 |
url = a['href'] |
| 83 |
url = 'https://harpers.org' + url |
| 84 |
title = self.tag_to_string(a).strip() |
| 85 |
desc = '' |
| 86 |
div = a.findParent('div').find('div', attrs={'class': 'byline'}) |
| 87 |
if div: |
| 88 |
desc = self.tag_to_string(div).strip() |
| 89 |
self.log(' ', title, '\n\t', desc[:-1], '\n\t', url) |
| 90 |
ans.append({'title': title, 'description': desc, 'url': url}) |
| 91 |
return [('Articles', ans)] |
| 92 |
|
| 93 |
# Harpers changes the content it delivers based on cookies, so the |
| 94 |
# following ensures that we send no cookies |
| 95 |
def get_browser(self, *args, **kwargs): |
| 96 |
return self |
| 97 |
|
| 98 |
def clone_browser(self, *args, **kwargs): |
| 99 |
return self.get_browser() |
| 100 |
|
| 101 |
def open_novisit(self, *args, **kwargs): |
| 102 |
br = browser() |
| 103 |
return br.open_novisit(*args, **kwargs) |
| 104 |
|
| 105 |
open = open_novisit |