| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
# License: GPLv3 Copyright: 2021, Alistair Francis <[email protected]> |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 6 |
|
| 7 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 8 |
|
| 9 |
|
| 10 |
def classes(classes): |
| 11 |
q = frozenset(classes.split(' ')) |
| 12 |
return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)}) |
| 13 |
|
| 14 |
|
| 15 |
class SaturdayPaper(BasicNewsRecipe): |
| 16 |
title = 'The Saturday Paper' |
| 17 |
__author__ = 'Alistair Francis' |
| 18 |
description = ( |
| 19 |
'The Saturday Paper is a weekly newspaper, dedicated to narrative journalism.' |
| 20 |
' It offers the biggest names and best writing in news, culture, and analysis, with a particular focus on Australia. ' |
| 21 |
) |
| 22 |
language = 'en_AU' |
| 23 |
no_stylesheets = True |
| 24 |
remove_javascript = True |
| 25 |
encoding = 'utf-8' |
| 26 |
|
| 27 |
keep_only_tags = [classes('article-page__content article__text article__image article-page__title article-page__image')] |
| 28 |
remove_tags = [ |
| 29 |
classes( |
| 30 |
'social-icons-article-top-container social-icons-article-bottom-container' |
| 31 |
' article-page__sidebar article-page__social__icons share-wrapper article-footer-container' |
| 32 |
) |
| 33 |
] |
| 34 |
remove_tags_after = [ |
| 35 |
{'name': 'div', 'class': 'end-matter'}, |
| 36 |
] |
| 37 |
|
| 38 |
def get_cover_url(self): |
| 39 |
soup = self.index_to_soup('https://www.thesaturdaypaper.com.au/editions/') |
| 40 |
div = soup.find('div', attrs={'class': 'article__image'}) |
| 41 |
return div.img['src'] |
| 42 |
|
| 43 |
def parse_index(self): |
| 44 |
feeds = [ |
| 45 |
('News', 'https://www.thesaturdaypaper.com.au/news'), |
| 46 |
('Opinion', 'https://www.thesaturdaypaper.com.au/opinion'), |
| 47 |
('Culture', 'https://www.thesaturdaypaper.com.au/culture'), |
| 48 |
('Life', 'https://www.thesaturdaypaper.com.au/life'), |
| 49 |
('Food', 'https://www.thesaturdaypaper.com.au/food/latest'), |
| 50 |
('Puzzles', 'https://www.thesaturdaypaper.com.au/puzzles'), |
| 51 |
('Sport', 'https://www.thesaturdaypaper.com.au/sport'), |
| 52 |
] |
| 53 |
|
| 54 |
articles = [] |
| 55 |
|
| 56 |
for feed, link in feeds: |
| 57 |
soup = self.index_to_soup(link) |
| 58 |
news = [] |
| 59 |
|
| 60 |
for a in soup.findAll(**classes('article')): |
| 61 |
title = a.find(class_='article__title') |
| 62 |
title = self.tag_to_string(title) |
| 63 |
|
| 64 |
url = a.find(class_='article__title_link') |
| 65 |
if url is None: |
| 66 |
continue |
| 67 |
url = url['href'] |
| 68 |
if url.startswith('/'): |
| 69 |
url = 'https://www.thesaturdaypaper.com.au' + url |
| 70 |
|
| 71 |
desc = a.find(class_='article__text') |
| 72 |
desc = self.tag_to_string(desc) |
| 73 |
|
| 74 |
self.log(title, ' at ', url) |
| 75 |
|
| 76 |
news.append({'title': title, 'url': url, 'description': desc}) |
| 77 |
|
| 78 |
articles.append((feed, news)) |
| 79 |
|
| 80 |
return articles |