| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
# License: GPLv3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 6 |
|
| 7 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 8 |
|
| 9 |
|
| 10 |
def absolutize(href): |
| 11 |
if href.startswith('/'): |
| 12 |
href = 'https://www.si.com' + href |
| 13 |
return href |
| 14 |
|
| 15 |
|
| 16 |
def classes(classes): |
| 17 |
q = frozenset(classes.split(' ')) |
| 18 |
return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)}) |
| 19 |
|
| 20 |
|
| 21 |
class SI(BasicNewsRecipe): |
| 22 |
title = 'Sports Illustrated' |
| 23 |
__author__ = 'Kovid Goyal' |
| 24 |
language = 'en' |
| 25 |
encoding = 'utf-8' |
| 26 |
ignore_duplicate_articles = {'title', 'url'} |
| 27 |
no_stylesheets = True |
| 28 |
compress_news_images = True |
| 29 |
compress_news_images_auto_size = 5 |
| 30 |
remove_attributes = ['style'] |
| 31 |
|
| 32 |
keep_only_tags = [ |
| 33 |
classes('m-detail-header m-detail--body'), |
| 34 |
] |
| 35 |
remove_tags = [ |
| 36 |
classes('media-video OUTBRAIN m-in-content-ad-row'), |
| 37 |
dict(name=['button', 'meta', 'source']), |
| 38 |
] |
| 39 |
|
| 40 |
def get_browser(self, *args, **kwargs): |
| 41 |
br = BasicNewsRecipe.get_browser(self, *args, **kwargs) |
| 42 |
br.addheaders += [('Accept-Language', 'en')] |
| 43 |
return br |
| 44 |
|
| 45 |
def preprocess_html(self, soup, *a): |
| 46 |
for tag in soup.findAll(attrs={'data-src': True}): |
| 47 |
tag.name = 'img' |
| 48 |
del tag.contents[:] |
| 49 |
tag['src'] = tag['data-src'] |
| 50 |
tag['height'] = tag['width'] = '' |
| 51 |
return soup |
| 52 |
|
| 53 |
def parse_index(self): |
| 54 |
soup = self.index_to_soup('https://www.si.com/') |
| 55 |
# from calibre.utils.ipython import ipython |
| 56 |
# ipython({'soup': soup}) |
| 57 |
cats = {} |
| 58 |
for ps in soup.findAll('phoenix-super-link'): |
| 59 |
h2 = ps.find('h2') |
| 60 |
title = self.tag_to_string(h2) |
| 61 |
label = ps.find(attrs={'phx-track-id': 'Label'}) |
| 62 |
category = self.tag_to_string(label) if label is not None else 'Features' |
| 63 |
url = absolutize(ps['href']) |
| 64 |
arts = cats.setdefault(category, []) |
| 65 |
arts.append({'title': title, 'url': url}) |
| 66 |
self.log('Found article', title) |
| 67 |
ans = [] |
| 68 |
for key in sorted(cats, key=lambda x: x.lower()): |
| 69 |
ans.append((key, cats[key])) |
| 70 |
return ans |