| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
www.theweek.com |
| 4 |
""" |
| 5 |
|
| 6 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 7 |
|
| 8 |
|
| 9 |
class TheWeek(BasicNewsRecipe): |
| 10 |
title = 'The Week' |
| 11 |
__author__ = 'unkn0wn' |
| 12 |
description = ( |
| 13 |
"The Week is for readers who want to know what's going on in the world, without having to read " |
| 14 |
'several daily newspapers or get wrapped up in the endless news cycle. For every important story, ' |
| 15 |
'our editors carefully select commentary from all sides of the debate and artfully stitch them together ' |
| 16 |
'into one concise read. By showing you every perspective, we enable you to form your own opinion.' |
| 17 |
) |
| 18 |
language = 'en_GB' |
| 19 |
encoding = 'utf-8' |
| 20 |
no_stylesheets = True |
| 21 |
remove_javascript = True |
| 22 |
remove_attributes = ['width', 'height', 'style'] |
| 23 |
|
| 24 |
ignore_duplicate_articles = {'title', 'url'} |
| 25 |
remove_empty_feeds = True |
| 26 |
resolve_internal_links = True |
| 27 |
|
| 28 |
extra_css = ''' |
| 29 |
img {display:block; margin:0 auto;} |
| 30 |
.caption__text--hero, .credit { font-size:small; text-align:center; } |
| 31 |
.header__strapline, em, i { color:#202020; } |
| 32 |
.article-type__breadcrumb { color:grey; } |
| 33 |
.author-byline__author-text {font-size:small; } |
| 34 |
''' |
| 35 |
|
| 36 |
def get_cover_url(self): |
| 37 |
import json |
| 38 |
|
| 39 |
url = 'https://ukmagazine.theweek.com/timelines.json' |
| 40 |
data = json.loads(self.index_to_soup(url, raw=True)) |
| 41 |
for x in data['timelines'][:5]: |
| 42 |
if 'image' in x: |
| 43 |
if '-cover-' in x['image']: |
| 44 |
return 'https://ukmagazine.theweek.com' + x['image'][1:] |
| 45 |
|
| 46 |
keep_only_tags = [classes('article-type__breadcrumb header__title header__strapline image image--hero author-byline__author-text article__body')] |
| 47 |
|
| 48 |
remove_tags = [dict(name=['aside', 'source']), classes('blueconic-article__wrapper ad-unit van_vid_carousel tag-links')] |
| 49 |
|
| 50 |
def preprocess_html(self, soup): |
| 51 |
for img in soup.findAll('img', attrs={'data-pin-media': True}): |
| 52 |
img['src'] = img['data-pin-media'].replace('.jpg', '-768-80.jpg') |
| 53 |
return soup |
| 54 |
|
| 55 |
def parse_index(self): |
| 56 |
soup = self.index_to_soup('https://theweek.com/archive') |
| 57 |
list = soup.find('ul', attrs={'class': 'archive__list'}) |
| 58 |
|
| 59 |
feeds = [] |
| 60 |
|
| 61 |
for li in list.findAll('li', **classes('archive__item--heading'))[:7]: |
| 62 |
section = self.tag_to_string(li) |
| 63 |
self.log(section) |
| 64 |
|
| 65 |
articles = [] |
| 66 |
|
| 67 |
ul = li.findNext('li').ul |
| 68 |
for a in ul.findAll('a', href=True): |
| 69 |
url = a['href'] |
| 70 |
if '/puzzles/' in url: |
| 71 |
continue |
| 72 |
title = self.tag_to_string(a) |
| 73 |
self.log(' ', title, '\n\t', url) |
| 74 |
articles.append({'title': title, 'url': url}) |
| 75 |
feeds.append((section, articles)) |
| 76 |
return feeds |
| 77 |
|
| 78 |
def populate_article_metadata(self, article, soup, first): |
| 79 |
desc = soup.find(**classes('header__strapline')) |
| 80 |
if desc: |
| 81 |
article.summary = self.tag_to_string(desc) |
| 82 |
article.text_summary = article.summary |