calibre/recipes/telegraph_uk.recipe
2018-04-10 07:33:30 +05:30

91 lines
3.2 KiB
Plaintext

__license__ = 'GPL v3'
__copyright__ = '2008-2010, Darko Miletic <darko.miletic at gmail.com>'
'''
telegraph.co.uk
'''
import json
from calibre import random_user_agent
from calibre.web.feeds.news import BasicNewsRecipe
def classes(classes):
q = frozenset(classes.split(' '))
return dict(
attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)}
)
def absolutize(url):
if url.startswith('/'):
url = 'http://www.telegraph.co.uk' + url
return url
class TelegraphUK(BasicNewsRecipe):
title = 'Telegraph.co.uk'
__author__ = 'Darko Miletic and Sujata Raman'
description = 'News from United Kingdom'
oldest_article = 2
category = 'news, politics, UK'
publisher = 'Telegraph Media Group ltd.'
max_articles_per_feed = 100
no_stylesheets = True
language = 'en_GB'
encoding = 'utf-8'
ignore_duplicate_articles = {'title', 'url'}
remove_empty_feeds = True
use_embedded_content = False
feeds = [
(u'UK News', u'http://www.telegraph.co.uk/news/uknews/rss'),
(u'World News', u'http://www.telegraph.co.uk/news/worldnews/rss'),
(u'Politics', u'http://www.telegraph.co.uk/news/newstopics/politics/rss'),
(u'Finance', u'http://www.telegraph.co.uk/finance/rss'),
(u'Technology News', u'http://www.telegraph.co.uk/scienceandtechnology/technology/technologynews/rss'),
(u'UK News', u'http://www.telegraph.co.uk/scienceandtechnology/technology/technologyreviews/rss'),
(u'Science News', u'http://www.telegraph.co.uk/scienceandtechnology/science/sciencenews/rss'),
(u'Sport', u'http://www.telegraph.co.uk/sport/rss'),
(u'Earth News', u'http://www.telegraph.co.uk/earth/earthnews/rss'),
(u'Comment', u'http://www.telegraph.co.uk/comment/rss'),
(u'Travel', u'http://www.telegraph.co.uk/travel/rss'),
(u'How about that?', u'http://www.telegraph.co.uk/news/newstopics/howaboutthat/rss')
]
keep_only_tags = [
classes('lead-asset-image-container headline__heading footer-author'),
dict(itemprop='articleBody'),
]
remove_tags = [
dict(name=['link', 'meta', 'style']),
classes('videoPlayer'),
]
remove_attributes = 'width height'.split()
def get_browser(self):
return BasicNewsRecipe.get_browser(
self, user_agent=random_user_agent(allow_ie=False)
)
def get_article_url(self, article):
url = article.get('link', None)
if 'picture-galleries' in url or 'pictures' in url or 'picturegalleries' in url:
url = None
return url
def preprocess_html(self, soup):
for img in soup.findAll(attrs={'data-frz-src-array': True}):
img['style'] = ''
img.name = 'img'
d = json.loads(img['data-frz-src-array'].replace("'", '"'))
for item in d:
if int(item.get('width', 0)) > 700:
img['src'] = absolutize(item['src'])
break
for img in soup.findAll('div', attrs={'data-js': 'LazyImage'}):
img['style'] = ''
img.name = 'img'
srcs = [x.split(' ')[0].strip() for x in img['data-srcset'].split(',')]
img['src'] = srcs[1 if len(srcs) > 0 else 0]
return soup