Updating for new site layout & combining Sunday ed

This commit is contained in:
bobbysteel 2017-01-02 00:31:36 +00:00 committed by GitHub
parent 998d1dbb60
commit d93116f67c

View File

@ -1,45 +1,51 @@
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009-2013, Darko Miletic <darko.miletic at gmail.com>' __copyright__ = '2010-2017, Bobby Steel <bob at xdca.com>, Darko Miletic'
''' '''
www.thetimes.co.uk www.thetimes.co.uk
''' '''
import urllib import urllib
import html5lib
from lxml import html
from calibre.web.feeds.news import BasicNewsRecipe 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)})
class TimesOnline(BasicNewsRecipe): class TimesOnline(BasicNewsRecipe):
title = 'The Times UK' title = 'The Times & Sunday Times (UK)'
__author__ = 'Darko Miletic' __author__ = 'Bobby Steel'
description = 'news from United Kingdom and World' description = 'news from United Kingdom and World'
language = 'en_GB' language = 'en_GB'
publisher = 'Times Newspapers Ltd' publisher = 'Times Newspapers Ltd'
category = 'news, politics, UK' category = 'news, politics, UK'
oldest_article = 3 excludeSections = ['Puzzles']
oldest_article = 1
max_articles_per_feed = 100 max_articles_per_feed = 100
no_stylesheets = True no_stylesheets = True
use_embedded_content = False use_embedded_content = False
encoding = 'utf-8' encoding = 'utf-8'
delay = 1 delay = 1
needs_subscription = True needs_subscription = True
auto_cleanup = False
publication_type = 'newspaper' publication_type = 'newspaper'
masthead_url = 'http://www.thetimes.co.uk/tto/public/img/the_times_460.gif' INDEX = 'http://www.thetimes.co.uk/'
INDEX = 'http://www.thetimes.co.uk' PREFIX = u'http://www.thetimes.co.uk'
PREFIX = u'http://www.thetimes.co.uk/tto/'
extra_css = """ extra_css = """
.f-ha{font-size: xx-large; font-weight: bold} .author-name,.authorName{font-style: italic}
.f-author{font-family: Arial,Helvetica,sans-serif} .published-date,.multi-position-photo-text{font-family: Arial,Helvetica,sans-serif;
.caption{font-size: small} font-size: small; color: gray;
display:block; margin-bottom: 0.5em}
body{font-family: Georgia,"Times New Roman",Times,serif} body{font-family: Georgia,"Times New Roman",Times,serif}
""" """
conversion_options = { conversion_options = {
'comment': description, 'tags': category, 'publisher': publisher, 'language': language 'comment': description, 'tags': category, 'publisher': publisher, 'language': language
} }
def get_browser(self): def get_browser(self):
br = BasicNewsRecipe.get_browser(self) br = BasicNewsRecipe.get_browser(self)
br.open('http://www.thetimes.co.uk/tto/news/') br.open('http://www.thetimes.co.uk/')
if self.username is not None and self.password is not None: if self.username is not None and self.password is not None:
data = urllib.urlencode({ data = urllib.urlencode({
'gotoUrl': self.INDEX, 'username': self.username, 'password': self.password 'gotoUrl': self.INDEX, 'username': self.username, 'password': self.password
@ -48,49 +54,51 @@ class TimesOnline(BasicNewsRecipe):
return br return br
remove_tags = [ remove_tags = [
dict(name=['object', 'link', 'iframe', 'base', 'meta']), dict( {'name': ['object', 'link', 'iframe', 'base', 'meta', 'script']},
attrs={'class': 'tto-counter'}) {'attrs': {'class': ['tools comments-parent','u-hide','Tooltip','Toolbar Toolbar--bottom','Comments Article-container','ArticlePager','Media-caption']}},
{'attrs': {'class': lambda x: x and 'Toolbar' in x}}
] ]
remove_attributes = ['lang'] remove_attributes = ['lang']
keep_only_tags = [ keep_only_tags = [
dict(attrs={'class': 'heading'}), dict(attrs={'class': 'f-author'}), dict( dict(attrs={'class': 'Article Article--default'}
attrs={'class': ['media', 'byline-timestamp']}), dict(attrs={'id': 'bodycopy'}) ), dict(attrs={'class': 'f-author'}), dict(attrs={'id': 'bodycopy'})
] ]
remove_tags_after = dict(attrs={'class': 'Article-content'})
feeds = [ feeds = [
(u'All News', u'http://www.thetimes.co.uk/')
(u'UK News', PREFIX + u'news/uk/?view=list'),
(u'World', PREFIX + u'news/world/?view=list'),
(u'Politics', PREFIX + u'news/politics/?view=list'),
(u'Health', PREFIX + u'health/news/?view=list'),
(u'Education', PREFIX + u'education/?view=list'),
(u'Technology', PREFIX + u'technology/?view=list'),
(u'Science', PREFIX + u'science/?view=list'),
(u'Environment', PREFIX + u'environment/?view=list'),
(u'Faith', PREFIX + u'faith/?view=list'),
(u'Opinion', PREFIX + u'opinion/?view=list'),
(u'Sport', PREFIX + u'sport/?view=list'),
(u'Business', PREFIX + u'business/?view=list'),
(u'Money', PREFIX + u'money/?view=list'),
(u'Life', PREFIX + u'life/?view=list'),
(u'Arts', PREFIX + u'arts/?view=list')
] ]
def preprocess_raw_html(self, raw, url):
return html.tostring(html5lib.parse(raw, treebuilder='lxml', namespaceHTMLElements=False), method='html', encoding=unicode)
def preprocess_html(self, soup):
for item in soup.findAll(style=True):
del item['style']
return self.adeify_images(soup)
def parse_index(self): def parse_index(self):
totalfeeds = [] soup = self.index_to_soup(self.INDEX)
lfeeds = self.get_feeds() totalfeeds = []
for feedobj in lfeeds: current_section = []
feedtitle, feedurl = feedobj div = []
self.report_progress(0, _('Fetching feed') + ' %s...' % for div in soup.findAll('section', attrs={'data-text': True}):
(feedtitle if feedtitle else feedurl)) current_articles = []
articles = [] self.log('in section: ', div['data-text'])
soup = self.index_to_soup(feedurl) current_section = div['data-text']
for item in soup.findAll('td', attrs={'class': 'title'}): if current_section not in self.excludeSections:
atag = item.find('a') for article in div.findAll('div', attrs={'class': 'Item-content'}):
url = self.INDEX + atag['href'] h3 = article.find('h3')
title = self.tag_to_string(atag) if h3 is not None:
articles.append({ title = self.tag_to_string(h3)
'title': title, 'date': '', 'url': url, 'description': '' aurl = h3.find('a')
}) if aurl is not None:
totalfeeds.append((feedtitle, articles)) url = aurl['href']
if url.startswith('/'):
url = 'http://www.thetimes.co.uk' + url
desc = title
self.log('section: ', current_section, 'title: ', title, 'url: ', url, 'desc: ', desc, '\n')
current_articles.append({'title': title, 'url': url, 'description': desc})
if current_articles:
totalfeeds.append((current_section, current_articles))
return totalfeeds return totalfeeds