calibre/recipes/mit_technology_review.recipe
2015-04-19 09:15:00 +05:30

74 lines
3.3 KiB
Python

#!/usr/bin/env python2
from __future__ import unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2015 Michael Marotta <mikefm at gmail.net>'
# Written April 2015
# Last edited 4/18/15
'''
technologyreview.com
'''
import re
from calibre.web.feeds.news import BasicNewsRecipe
class MitTechnologyReview(BasicNewsRecipe):
title = 'MIT Technology Review Magazine'
__author__ = 'Michael Marotta'
description = ('Bi-monthly magazine version of MIT Technology Review.'
' This is different than the recipe named simply "Technology Review"'
' which downloads the rss feed with daily articles from the website.')
INDEX = 'http://www.technologyreview.com/magazine/'
language = 'en'
encoding = 'utf-8'
simultaneous_downloads = 20
tags = 'news, technology, science'
keep_only_tags = [
{'attrs':{'class':['body', 'intro', 'article-magazine', 'byline', 'view-byline', 'sticky-wrap', 'body hack']}},
]
remove_tags = [
{'name': ['meta', 'link', 'noscript', 'clearfix', 'flag']},
]
no_stylesheets = True
preprocess_regexps = [(re.compile(r'<br[ ]*/>', re.IGNORECASE), lambda m: ''),
(re.compile(r'<br[ ]*clear.*/>', re.IGNORECASE), lambda m: '')]
extra_css = 'body { font-family: helvetica, sans-serif; } \
h2 { text-align: left; font-size: 1em; font-weight: bold; }}'
def parse_index(self):
soup = self.index_to_soup(self.INDEX)
# find cover
self.cover_url = soup.find('li', attrs={'class':'cover'}).find('img', src=True)['src']
# parse articles
col = soup.find(attrs={'class':'view-content'})
current_section, current_articles = None, []
feeds = []
for tag in col.findAll(name=['section', 'a'], attrs={'class':['content-block in-this-issue no-border', None]}):
if tag.name == 'section':
if current_section and current_articles:
feeds.append((current_section, current_articles))
current_section = self.tag_to_string(tag.find('h2'))[15:].capitalize()
current_articles = []
self.log('Found section:', current_section)
elif current_section:
a=tag # since tag itself is a <a> tag use it directly instead of using find
if a is not None:
if self.tag_to_string(a.h2) == "":
title = self.tag_to_string(a.h1)
else:
title = self.tag_to_string(a.h2) + ": " + self.tag_to_string(a.h1)
if "http://www.technologyreview.com" in a['href']:
url = a['href']
else:
url = "http://www.technologyreview.com" + a['href']
if title and url:
p = tag.find('p', attrs={'class':'columns-off'})
desc = self.tag_to_string(p) if p is not None else ''
current_articles.append({'title':title, 'url':url, 'description':desc})
self.log('\tArticle:', title, '[%s]' % url)
self.log('\t\t', desc)
if current_section and current_articles:
feeds.append((current_section, current_articles))
return feeds