mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Updating for new site layout & combining Sunday ed
This commit is contained in:
parent
998d1dbb60
commit
d93116f67c
@ -1,45 +1,51 @@
|
||||
|
||||
__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
|
||||
'''
|
||||
import urllib
|
||||
import html5lib
|
||||
from lxml import html
|
||||
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):
|
||||
title = 'The Times UK'
|
||||
__author__ = 'Darko Miletic'
|
||||
title = 'The Times & Sunday Times (UK)'
|
||||
__author__ = 'Bobby Steel'
|
||||
description = 'news from United Kingdom and World'
|
||||
language = 'en_GB'
|
||||
publisher = 'Times Newspapers Ltd'
|
||||
category = 'news, politics, UK'
|
||||
oldest_article = 3
|
||||
excludeSections = ['Puzzles']
|
||||
oldest_article = 1
|
||||
max_articles_per_feed = 100
|
||||
no_stylesheets = True
|
||||
use_embedded_content = False
|
||||
encoding = 'utf-8'
|
||||
delay = 1
|
||||
needs_subscription = True
|
||||
auto_cleanup = False
|
||||
publication_type = 'newspaper'
|
||||
masthead_url = 'http://www.thetimes.co.uk/tto/public/img/the_times_460.gif'
|
||||
INDEX = 'http://www.thetimes.co.uk'
|
||||
PREFIX = u'http://www.thetimes.co.uk/tto/'
|
||||
INDEX = 'http://www.thetimes.co.uk/'
|
||||
PREFIX = u'http://www.thetimes.co.uk'
|
||||
extra_css = """
|
||||
.f-ha{font-size: xx-large; font-weight: bold}
|
||||
.f-author{font-family: Arial,Helvetica,sans-serif}
|
||||
.caption{font-size: small}
|
||||
.author-name,.authorName{font-style: italic}
|
||||
.published-date,.multi-position-photo-text{font-family: Arial,Helvetica,sans-serif;
|
||||
font-size: small; color: gray;
|
||||
display:block; margin-bottom: 0.5em}
|
||||
body{font-family: Georgia,"Times New Roman",Times,serif}
|
||||
"""
|
||||
|
||||
conversion_options = {
|
||||
'comment': description, 'tags': category, 'publisher': publisher, 'language': language
|
||||
}
|
||||
|
||||
def 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:
|
||||
data = urllib.urlencode({
|
||||
'gotoUrl': self.INDEX, 'username': self.username, 'password': self.password
|
||||
@ -48,49 +54,51 @@ class TimesOnline(BasicNewsRecipe):
|
||||
return br
|
||||
|
||||
remove_tags = [
|
||||
dict(name=['object', 'link', 'iframe', 'base', 'meta']), dict(
|
||||
attrs={'class': 'tto-counter'})
|
||||
{'name': ['object', 'link', 'iframe', 'base', 'meta', 'script']},
|
||||
{'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']
|
||||
keep_only_tags = [
|
||||
dict(attrs={'class': 'heading'}), dict(attrs={'class': 'f-author'}), dict(
|
||||
attrs={'class': ['media', 'byline-timestamp']}), dict(attrs={'id': 'bodycopy'})
|
||||
dict(attrs={'class': 'Article Article--default'}
|
||||
), dict(attrs={'class': 'f-author'}), dict(attrs={'id': 'bodycopy'})
|
||||
]
|
||||
remove_tags_after = dict(attrs={'class': 'Article-content'})
|
||||
|
||||
feeds = [
|
||||
|
||||
(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')
|
||||
(u'All News', u'http://www.thetimes.co.uk/')
|
||||
]
|
||||
|
||||
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):
|
||||
totalfeeds = []
|
||||
lfeeds = self.get_feeds()
|
||||
for feedobj in lfeeds:
|
||||
feedtitle, feedurl = feedobj
|
||||
self.report_progress(0, _('Fetching feed') + ' %s...' %
|
||||
(feedtitle if feedtitle else feedurl))
|
||||
articles = []
|
||||
soup = self.index_to_soup(feedurl)
|
||||
for item in soup.findAll('td', attrs={'class': 'title'}):
|
||||
atag = item.find('a')
|
||||
url = self.INDEX + atag['href']
|
||||
title = self.tag_to_string(atag)
|
||||
articles.append({
|
||||
'title': title, 'date': '', 'url': url, 'description': ''
|
||||
})
|
||||
totalfeeds.append((feedtitle, articles))
|
||||
soup = self.index_to_soup(self.INDEX)
|
||||
totalfeeds = []
|
||||
current_section = []
|
||||
div = []
|
||||
for div in soup.findAll('section', attrs={'data-text': True}):
|
||||
current_articles = []
|
||||
self.log('in section: ', div['data-text'])
|
||||
current_section = div['data-text']
|
||||
if current_section not in self.excludeSections:
|
||||
for article in div.findAll('div', attrs={'class': 'Item-content'}):
|
||||
h3 = article.find('h3')
|
||||
if h3 is not None:
|
||||
title = self.tag_to_string(h3)
|
||||
aurl = h3.find('a')
|
||||
if aurl is not None:
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user